Google Sheets – 需要脚本来更改文件名称

huangapple go评论54阅读模式
英文:

Google Sheets - Script required to change name of files

问题

I can help you with the translation of the text you provided. Here's the translated version:

我正在寻找一种在特定时间更改文件名称的方法,可能是在凌晨1点,但这需要使用触发器来完成,需要进行配置。

Google Drive中的文件将根据Google表格中的数据进行重命名 - 请参见下面的示例数据。

示例数据的截图

我希望脚本能够引用列F中文件的URL(在实际使用时可能是不同的列),并根据引用数据列的查找范围来更改文件名称,将列D和E中的数据合并到文件名中(或引用的列中)。

由于Google Drive中的每个文件都不同,无法引用旧文件名。

有人能帮忙吗?

我尝试了以下方法,但我觉得这是错误的。

const SPREADSHEET_ID = "SPREADSHEET_ID"; // 根据您的喜好进行更改
const SHEET_NAME = "Sheet1"; // 根据您的喜好进行更改
const FOLDER_ID = "DRIVE_FOLDER_ID"; // 根据您的喜好进行更改

function changeFolderNames() {
  const ss = SpreadsheetApp.openById(SPREADSHEET_ID);
  const sheet = ss.getSheetByName(SHEET_NAME);
  const folder = DriveApp.getFolderById(FOLDER_ID);
  const values = sheet.getRange("A2:B" + sheet.getLastRow()).getValues(); // 如果没有标题行,请使用"A:B"代替
  values.forEach(row => {
    const [oldName, newName] = row;
    const fileIter = folder.getFilesByName(oldName);
    while (fileIter.hasNext()) {
      const file = fileIter.next();
      file.setName(newName);
    }
  });
}

如果您需要进一步的帮助,请随时提出问题。

英文:

I'm looking for a way to rename files at a specific time of day, maybe 1am but this can be done with triggers which will need configuring.

The files in Google Drive will be renamed from data in a Google Sheet - see below as an example.

Screenshot of example data

I'm hoping a script will be able to reference the URL of the file in Column F (this maybe a different column when i come to make this live) and somehow change the name of the file based on a lookup range referencing the columns of data, bringing together the data in column D and E into the file name (or the referenced columns).

Referencing the old file name is not possible as each file in Google Drive will be different.

Is anyone able to help?

I've tried the following but i have a feeling that this wrong

const SPREADSHEET_ID = "SPREADSHEET_ID"; // Change according to your preferences
const SHEET_NAME = "Sheet1"; // Change according to your preferences
const FOLDER_ID = "DRIVE_FOLDER_ID"; // Change according to your preferences

function changeFolderNames() {
  const ss = SpreadsheetApp.openById(SPREADSHEET_ID);
  const sheet = ss.getSheetByName(SHEET_NAME);
  const folder = DriveApp.getFolderById(FOLDER_ID);
  const values = sheet.getRange("A2:B" + sheet.getLastRow()).getValues(); // If there's no header row, use "A:B" instead
  values.forEach(row => {
    const [oldName, newName] = row;
    const fileIter = folder.getFilesByName(oldName);
    while (fileIter.hasNext()) {
      const file = fileIter.next();
      file.setName(newName);
    }
  });
}

答案1

得分: 0

我相信你的目标如下。

  • 你想从列 "D" 到列 "F" 中检索数值。

  • 列 "F" 包含文件的URL。从你展示的图像中可以看出,所有URL的格式都是 https://drive.google.com/open?id={fileId}

  • 你想使用列 "D" 和 "E" 的值来重命名文件URL的文件名。新文件名如下。

    如果列 D 显示 Name,列 E 显示 Folder,我希望文件名被改为 "Name Folder.pdf" 或 "Name Folder.jpg"。

修改要点:

  • 在你的脚本中,似乎从列 "A" 和 "B" 中检索了值。但是,你似乎想要从列 "D" 到 "F" 中检索值。
  • 在你的脚本中,似乎没有使用URL。而是使用了文件名。我认为这可能不是你期望的结果。

当这些要点在你的脚本中得到反映时,以下是如何修改的建议:

修改后的脚本:

在使用此脚本之前,请设置你的表格ID和表格名称。

function changeFolderNames() {
  const SPREADSHEET_ID = "###"; // 根据你的偏好进行更改
  const SHEET_NAME = "Sheet1"; // 根据你的偏好进行更改

  const ss = SpreadsheetApp.openById(SPREADSHEET_ID);
  const sheet = ss.getSheetByName(SHEET_NAME);
  const values = sheet.getRange("D2:F" + sheet.getLastRow()).getValues();
  values.forEach(([d, e, f]) => {
    const fileId = f.match(/id\=(.*?)(&|$)/);
    if (fileId) {
      try {
        const file = DriveApp.getFileById(fileId[1]);
        const ext = file.getName().split(".");
        const extension = ext.length > 1 ? `.${ext.pop()}` : "";
        const newName = `${d} ${e}${extension}`; // 来自 "我希望文件名被改为 "Name Folder.pdf" 或 "Name Folder.jpg""。
        file.setName(newName);
      } catch (e) {
        console.log(e.message); // 如果发生错误,你可以在日志中看到它。
      }
    } else {
      console.log(`无法从 "${f}" 中检索到文件ID。`);
    }
  });
}
  • 运行此脚本后,列 "F" 中的URL的文件名将由列 "D" 和 "E" 的值进行更改。

注意:

  • 在这个示例脚本中,新的文件名是 const newName = ${d} ${e}${extension};。如果你想更改它,请进行修改。

  • 根据你展示的示例图像,我相信所有URL的格式都是 https://drive.google.com/open?id={fileId}。但是,如果使用了其他格式,这个脚本将无法使用。请注意这一点。在这种情况下,错误消息将显示在日志中。

  • 如果无法检索到文件并且文件名无法更改,你可以在日志中看到错误消息。在这种情况下,请检查权限和文件ID。再次确认。

英文:

I believe your goal is as follows.

  • You want to retrieve the values from columns "D" to "F".

  • Column "F" has the URLs of files. From your showing image, the format of all URLs is only https://drive.google.com/open?id={fileId}.

  • You want to rename the file of file URL using the values of columns "D" and "E". The new filename is as follows.

    > If column D says Name and Column E says Folder, i want the file name to be changed to "Name Folder.pdf" or "Name Folder.jpg".

Modification points:

  • In your script, it seems that the values are retrieved from columns "A" and "B". But, it seems that you want to retrieve the values from columns "D" to "F".
  • In your script, it seems that the URL is not used. The filename is used. I thought that this might not be your expected result.

When these points are reflected in your script, how about the following modification?

Modified script:

Before you use this script, please set your Spreadsheet ID and sheet name.

function changeFolderNames() {
  const SPREADSHEET_ID = "###"; // Change according to your preferences
  const SHEET_NAME = "Sheet1"; // Change according to your preferences

  const ss = SpreadsheetApp.openById(SPREADSHEET_ID);
  const sheet = ss.getSheetByName(SHEET_NAME);
  const values = sheet.getRange("D2:F" + sheet.getLastRow()).getValues();
  values.forEach(([d, e, f]) => {
    const fileId = f.match(/id\=(.*?)(&|$)/);
    if (fileId) {
      try {
        const file = DriveApp.getFileById(fileId[1]);
        const ext = file.getName().split(".");
        const extension = ext.length > 1 ? `.${ext.pop()}` : "";
        const newName = `${d} ${e}${extension}`; // From "i want the file name to be changed to "Name Folder.pdf" or "Name Folder.jpg"".
        file.setName(newName);
      } catch (e) {
        console.log(e.message); // If an error occurs, you can see it in the log.
      }
    } else {
      console.log(`File ID couldn't retrieved from "${f}".`);
    }
  });
}
  • When this script is run, the filenames of URLs of column "F" are changed by the values of columns "D" and "E".

Note:

  • In this sample script, the new filename is const newName = ${d} ${e}${extension};. If you want to change this, please modify it.

  • From your showing sample image, I believe the format of all URLs is only https://drive.google.com/open?id={fileId}. But, if another format is used, this script cannot be used. Please be careful about this. In that case, the error message is shown in the log.

  • If the file couldn't be retrieved and the filename cannot be changed, you can see the error message in the log. In that case, please check the permission and the file ID, again.

huangapple
  • 本文由 发表于 2023年6月30日 03:38:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76584155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定