有没有办法批量更改 Google 电子表格中的一系列文件的名称?

问题描述 投票:0回答:2

我有一个 Google 电子表格,其中的单元格中

B4:B
有一个 Google Drive URL 列表,每个 URL 末尾都有文件的 ID(不是文件的名称和扩展名)。在
N4:N
列中,我有一个文件名列表(不带扩展名),用于重命名单元格中
B4:B
中的 URL 指向的文件。
我想知道是否有一个脚本可以将每个相应ID的文件名批量重命名为位于单元格中的名称
N4:N
,同时保留每个文件的文件扩展名
当该列连续 5 行变空时,脚本应该停止。
非常感谢。

google-sheets google-apps-script
2个回答
0
投票

建议

您可以尝试这个示例脚本:

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
    }
  }
}

脚本将循环遍历链接数组,并为每个指定的链接设置一个新的文件名;链接列上的每个空白单元格都会增加一个计数器,并且:

  1. 如果在找到 5 个连续空白之前找到链接,将重置,或者;

  2. 一旦找到 5 个连续的空格,将终止脚本。

输出

参考文献


0
投票

试试这个:

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]));
}
© www.soinside.com 2019 - 2024. All rights reserved.