我有一个 Google 电子表格,其中的单元格中
B4:B
有一个 Google Drive URL 列表,每个 URL 末尾都有文件的 ID(不是文件的名称和扩展名)。在 N4:N
列中,我有一个文件名列表(不带扩展名),用于重命名单元格中 B4:B
中的 URL 指向的文件。N4:N
,同时保留每个文件的文件扩展名您可以尝试这个示例脚本:
function bulkUpdateNames(){
var sheets = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var links = sheets.getRange("B4:B").getValues(); // arrays of links
var names = sheets.getRange("N4:N").getValues(); // arrays of new file names
var blank_ctr = 0 // counter to check blank entries on the links column
for (i = 0; i < links.length; i++){
// if a link is found, changes the file name and resets the counter to 0
if(links[i] != ""){
DriveApp.getFileById(links[i]).setName(names[i]);
blank_ctr = 0; // resets the counter
}
else{
blank_ctr++;
}
// terminates the loop if 5 consecutive blanks are found
if(blank_ctr == 5){
break
}
}
}
脚本将循环遍历链接数组,并为每个指定的链接设置一个新的文件名;链接列上的每个空白单元格都会增加一个计数器,并且:
如果在找到 5 个连续空白之前找到链接,将重置,或者;
一旦找到 5 个连续的空格,将终止脚本。
试试这个:
function bulkUpdateNames(){
const ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
sh.getRange("B4:N" + sh.getLastRow()).getValues().filter(r => r[0]).forEach(r => DriveApp.getFileById(r[0]).setName(r[12]));
}