You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Apps Script将Drive中含指定词的Google Docs信息录入Google Sheet

Google Drive Docs页眉页脚关键词匹配导出方案

核心实现逻辑

  • 调用DriveApp接口遍历账号下所有Google Docs格式文件
  • 对每个可访问的Docs文件,读取全部页眉、页脚的文本内容
  • 匹配预设关键词,匹配成功则收集文档元信息
  • 批量将匹配结果写入指定Google Sheet

完整可运行代码

// 配置项,使用前请修改为你自己的参数
const CONFIG = {
  // 要匹配的关键词列表
  TARGET_KEYWORDS: ['alice', 'bob', 'carol'],
  // 存放结果的Google Sheet ID
  RESULT_SHEET_ID: '替换为你自己的Sheet ID',
  // 匹配是否忽略大小写,true为忽略,false为严格匹配
  CASE_INSENSITIVE: true
}

function scanDocsForKeywords() {
  // 初始化结果表
  const sheet = SpreadsheetApp.openById(CONFIG.RESULT_SHEET_ID).getActiveSheet()
  // 清空表中原有内容并写入表头
  sheet.clearContents()
  const headerRow = ['Google Doc ID', '文档名', '文档链接', '匹配关键词', '文档所有者']
  const resultRows = [headerRow]

  // 遍历所有Google Docs文件
  const docsFiles = DriveApp.getFilesByType(MimeType.GOOGLE_DOCS)
  while (docsFiles.hasNext()) {
    const file = docsFiles.next()
    let doc = null
    // 处理无权限打开的文档
    try {
      doc = DocumentApp.openById(file.getId())
    } catch (e) {
      continue
    }

    // 收集所有页眉、页脚的文本内容
    let allHeaderFooterText = ''
    // 读取各类页眉
    const headerSections = [
      doc.getHeader(),
      doc.getFirstPageHeader(),
      doc.getOddPageHeader(),
      doc.getEvenPageHeader()
    ]
    headerSections.forEach(section => {
      if (section) allHeaderFooterText += section.getText()
    })
    // 读取各类页脚
    const footerSections = [
      doc.getFooter(),
      doc.getFirstPageFooter(),
      doc.getOddPageFooter(),
      doc.getEvenPageFooter()
    ]
    footerSections.forEach(section => {
      if (section) allHeaderFooterText += section.getText()
    })

    // 关键词匹配
    const matchKeyword = CONFIG.TARGET_KEYWORDS.find(keyword => {
      if (CONFIG.CASE_INSENSITIVE) {
        return allHeaderFooterText.toLowerCase().includes(keyword.toLowerCase())
      } else {
        return allHeaderFooterText.includes(keyword)
      }
    })

    // 匹配成功则写入结果行
    if (matchKeyword) {
      const ownerName = file.getOwner() ? file.getOwner().getName() : '无公开所有者信息'
      resultRows.push([
        file.getId(),
        file.getName(),
        file.getUrl(),
        `"${matchKeyword}"`,
        ownerName
      ])
    }
  }

  // 批量写入结果到Sheet
  sheet.getRange(1, 1, resultRows.length, resultRows[0].length).setValues(resultRows)
}

使用步骤

  • 打开你用来存储结果的Google Sheet,点击顶部菜单栏「扩展程序」->「Apps Script」进入脚本编辑器
  • 删除编辑器中默认的空代码,粘贴上述完整代码
  • 修改代码开头CONFIG配置项里的参数:替换TARGET_KEYWORDS为你要匹配的关键词,替换RESULT_SHEET_ID为当前Sheet的ID(ID是Sheet URL中d/和/edit之间的字符串)
  • 点击编辑器顶部的保存按钮,自定义项目名称后,选择运行函数scanDocsForKeywords
  • 首次运行需要按照弹窗提示完成授权,授权完成后脚本自动执行,执行完成后返回Sheet即可看到匹配结果

注意事项

  • Apps Script单次运行最长时限为6分钟,如果你账号下Docs文件数量过万,建议加分批遍历逻辑,避免超时中断
  • 默认匹配逻辑忽略大小写,若需要严格区分大小写,可将CONFIG中的CASE_INSENSITIVE改为false
  • 脚本会自动跳过你没有访问权限的Docs文件,不会报错中断

内容的提问来源于stack exchange,提问作者Justin Troutman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 14:24:03