从一列文件夹 ID 中获取最新更新

Get Latest Updated from a column of folder IDs

您好,我有一列包含 Google 驱动器文件夹 link 的列表,我想获取文件夹中包含的所有文件的最新更新日期。 Example

通过环顾四周,我能够构建以下脚本,通过给我文件夹中包含的所有文件及其最新更新的列表,这些脚本运行良好。

function onOpen() {
  var spreadsheet = SpreadsheetApp.getActive();
  var menuItems = [
    {name: 'UpdateCheck', functionName: 'files'},
  ]; 
  spreadsheet.addMenu('Menu', menuItems);
}


function files() {
  var ui = SpreadsheetApp.getUi();
  var result  = ui.prompt("Paste the ID of Google Drive Folder");
  var folderId = result.getResponseText();

  var folder = DriveApp.getFolderById(folderId);
  var files = folder.getFiles();
  var output = []; 
  while (files.hasNext()) {
    var file = files.next();
    output.push([file.getName(), file.getLastUpdated()]);
  }
  output.sort(function(a, b) {
    return a[0] == b[0] ? 0 : a[0] < b[0] ? -1 : 1;
  });
   SpreadsheetApp.getActiveSpreadsheet().insertSheet().getRange(2, 1, output.length, output[0].length).setValues(output);

但是我必须使用 UI 打开每个 link,将 URL 中的 ID 复制并粘贴到输入字段中。我只想读取列范围,从没有重复的字符串和 运行 我的脚本中获取 ID,并将结果放在新选项卡中。任何帮助都会很棒。

我相信你的现状和目标如下。

  • 您有 sheet 文件夹 link 被放入您提供的图像中的“D”列。

  • 您想从 link 中检索文件夹 ID,并希望检索文件夹中所有文件的文件名和 getLastUpdated() 的值。

  • 您想将检索到的数据放在新的 sheet 上。

修改后的脚本:

请设置 srcSheetNamedstSheetName 的 sheet 名称。

function myFunction() {
  var srcSheetName = "Sheet1"; // Please set the source sheet name.
  var dstSheetName = "Sheet2"; // Please set the destination sheet name.

  // Retrieve folder links from "D2:D" and check the duplication.
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName(srcSheetName);
  var folderIds = [...new Set(sheet.getRange("D2:D" + sheet.getLastRow()).getRichTextValues().flatMap(([d]) => {
    var url = d.getLinkUrl();
    if (url && /^https:\/\/drive\.google\.com\/drive\/folders\/.+/.test(url)) {
      return [url.split("/")[5].trim()];
    }
    return [];
  }))];
  
  // Create an array for putting to sheet.
  var res = folderIds.flatMap(id => {
    var folder = DriveApp.getFolderById(id);
    var files = folder.getFiles();
    var temp = [];
    while (files.hasNext()) {
      var file = files.next();
      temp.push([file.getName(), file.getLastUpdated()]);
    }
    return temp;
  });
  
  // Put the array to the destination sheet.
  ss.insertSheet(dstSheetName).getRange(1, 1, res.length, 2).setValues(res);
}

关于一些不清楚的地方,我猜测如下。

  • 我无法理解单元格中文件夹 link 的格式。在这个修改中,我猜测文件夹 link 就像 https://drive.google.com/drive/folders/###.

  • 没看懂get the IDs from the strings without duplicates。在此修改中,检查了检索到的文件夹 ID 的重复。

参考文献:

已添加:

从以下您的回复中,

Quick question, when I run it I get an Unexpected error while getting the method or property getFolderById on object DriveApp at line var res = folderIds.flatMap(id => { var folder = DriveApp.getFolderById(id); it's solvable or should I open another thread?

the folder link was indeed incorrect instead of https://drive.google.com/drive/folders/### the format was https://drive.google.com/corp/drive/u/0/folders/####?resourcekey=#### so I correct the code with /^https://drive.google.com/corp/drive/u/0/folders/.+/.test(url)) { return [url.split("/")[8].trim()]; but now I get 1CHKEThZJ0P-ISv0g0MoGjCCqdZuuJJyA?resourcekey=0-1gzYTJvJv1XDwF2Hx4HvEQ and I need to find a way to get rid of everything right before the "?"

为此,我修改了我建议的脚本如下。

修改后的脚本:

function myFunction() {
  var srcSheetName = "Sheet1"; // Please set the source sheet name.
  var dstSheetName = "Sheet2"; // Please set the destination sheet name.

  // Retrieve folder links from "D2:D" and check the duplication.
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName(srcSheetName);
  var folderIds = [...new Set(sheet.getRange("D2:D" + sheet.getLastRow()).getRichTextValues().flatMap(([d]) => {
    var url = d.getLinkUrl();
    if (url) {
      if (/^https:\/\/drive\.google\.com\/drive\/folders\/.+/.test(url)) {
        return [url.split("/")[5].trim()];
      } else if (/^https:\/\/drive\.google\.com\/.+\/drive\/u\/0\/folders\/.+/.test(url)) {
        return [url.split("/")[8].trim().split("?")[0]];
      }
    }
    return [];
  }))];
  
  // Create an array for putting to sheet.
  var res = folderIds.flatMap(id => {
    try {
      var folder = DriveApp.getFolderById(id);
      var files = folder.getFiles();
      var temp = [];
      while (files.hasNext()) {
        var file = files.next();
        temp.push([file.getName(), file.getLastUpdated()]);
      }
      return temp;
    } catch (e) {
      console.log({id, msg: e.message});
    }
  });
  
  // Put the array to the destination sheet.
  ss.insertSheet(dstSheetName).getRange(1, 1, res.length, 2).setValues(res);
}
  • 修改后可以使用https://drive.google.com/drive/folders/###https://drive.google.com/corp/drive/u/0/folders/####?resourcekey=####2种URL,从文件夹中取文件出错时,可以看到文件夹ID在日志。而且,脚本不会停止。