やり方
まず listWithId でフォルダの中のファイルの ID と今の名前を書き出し、C列に新しい名前(例:2-1-05_理科レポート.pdf)を書きます。renameByList を実行すると、一覧のとおりに名前が変わります。名前順に並べるだけで番号順になり、採点や返却が楽になります。
コピペ用
function listWithId() {
const it = DriveApp.getFolderById('フォルダのID').getFiles();
const out = [['ファイルID', '今の名前', '新しい名前']];
while (it.hasNext()) {
const f = it.next();
out.push([f.getId(), f.getName(), '']);
}
const sh = SpreadsheetApp.getActiveSheet();
sh.clearContents();
sh.getRange(1, 1, out.length, 3).setValues(out);
}
function renameByList() {
const sh = SpreadsheetApp.getActiveSheet();
const rows = sh.getDataRange().getValues(); // A:ID B:今の名前 C:新しい名前
for (let i = 1; i < rows.length; i++) {
const [id, , newName] = rows[i];
if (!id || !newName) continue;
DriveApp.getFileById(id).setName(String(newName));
sh.getRange(i + 1, 4).setValue('変更済み');
}
}
listWithId は開いているシートの中身を消してから書き込むので、専用の空のシートで実行します。名前を変えられるのは編集の権限があるファイルだけ。拡張子(.pdf など)も新しい名前に書いておきます。ファイル名に実名を入れるかどうかは学校の決まりに合わせてください。