使用 Google 云端硬盘中包含特定字词的文档填充 Google Sheet

Populate Google Sheet with Google Docs in Drive that contain certain words

以下是我要实现的目标的摘要:

  1. 运行 搜索驱动器中所有 Google 文档的 Apps 脚本。
  2. 如果脚本找到包含三个词中至少一个的文档(总是在页眉或页脚中),那么它会将其输出到 Google Sheet 内的一行中,捕获Google 文档 ID、文档名称、link 以及它在搜索中找到的单词(可选,包括所有者姓名会很好)。请注意,header/footer 只会包含几个单词中的一个 - 绝不会包含它们的组合。

例如,查找页眉或页脚中包含“alice”、“bob”或“carol”的所有文件;在 Google Sheet.

中将这些条目记录为单独的行

Google 文档编号 |爱丽丝项目总结 2021 | Link 给医生 | “爱丽丝”

我觉得我已经在 Apps 脚本中找到了我需要的东西 API,并找到了一些类似的搜索结果,但无法完全拼凑起来。

如果有帮助或不清楚,非常乐意详细说明!

欢迎任何指点! (虽然只是指出我的搜索不够全面。)

我相信你的目标如下。

  • 你有一个Google点差sheet。当您打开 Google Spreadsheet 时,您想要 运行 一个脚本来搜索 Google 驱动器中的所有 Google 文档文件,方法是检查 [=83] =] 和文档的页脚,并将搜索结果放入 Google Spreadsheet.
  • 中的规范 sheet
  • 您要搜索 Google header 和页脚中包含“alice”、“bob”或“carol”的值的文档。而且,您想将值放在 Spreadsheet.
  • 您的 Google 驱动器中有 hundreds/thousands 个文档 Google 个。
  • 您想使用 Google Apps 脚本实现此目的。

在这种情况下,我想提出以下流程。

  1. 使用 Drive [=67] 的搜索查询搜索 Google 文档文件,header 和页脚中包含“alice”、“bob”或“carol”的值=].
    • 通过这种方法,我觉得搜索成本可能会降低一些。
  2. 检查每个 Google 文档的 header 和页脚。当找到“alice”、“bob”或“carol”的值时,将文档 ID、标题、link 和搜索到的值放入一个数组。
  3. 将数组放在 Spread 上sheet。

示例脚本:

请将以下脚本复制并粘贴到Google Spreadsheet 的脚本编辑器中并设置sheet 名称。并且,在此示例脚本中,使用了 Drive API。所以 please enable Drive API at Advanced Google services.

还有,please install OnOpen trigger to the function of myFunction as the installable trigger。这样,当您打开 Spreadsheet 时,脚本会自动 运行。在可安装触发器的情况下,最长执行时间为 6 分钟,可以使用 Drive API。另一方面,简单的触发器是 30 秒。请注意这一点。

function myFunction() {
  const searchTexts = ["alice", "bob", "carol"];

  // 1. Search the Google Document files that the values of "alice", "bob", or "carol" are included in the header ahd footer using the search query of Drive API.
  const q = `mimeType = '${MimeType.GOOGLE_DOCS}' and (` + searchTexts.map(s => `fullText contains '${s}'`).join(" or ") + ")";
  let ar = [];
  let pageToken = "";
  do {
    const res = Drive.Files.list({q, pageToken, maxResults: 1000, fields: "items(id, title, alternateLink)"});
    if (res.items.length > 0) {
      ar = ar.concat(res.items);
    }
    pageToken = res.nextPageToken;
  } while (pageToken);

  // 2. Check the header and footer of each Google Document. When the values of "alice", "bob", or "carol" are found, the Document ID, title, link and searched value are put to an array.
  const values = ar.reduce((ar, {id, title, alternateLink}) => {
    const doc = DocumentApp.openById(id);
    let temp = [];
    const header = doc.getHeader();
    if (header) {
      const hText = header.getText();
      temp = temp.concat(searchTexts.filter(e => hText.includes(e)));
    }
    const footer = doc.getFooter();
    if (footer) {
      const fText = footer.getText();
      temp = temp.concat(searchTexts.filter(e => fText.includes(e)));
    }
    if (temp.length > 0) ar.push([id, title, alternateLink, temp.join(",")]);
    return ar;
  }, []);

  // 3. Put the array to Spreadsheet.
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1"); // Please set the sheetname.
  sheet.getRange(sheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
}

注:

  • 在我的环境中,我没有 hundreds/thousands 的 Google 文档。而且,我无法测试你的情况。所以请在你的实际情况下测试上面的脚本。

参考文献: