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

借助通用触发器通过Apps Script批量自动化多表格保护与解锁

批量保护/解锁多个Google电子表格的脚本实现

核心逻辑

  • 先把所有目标表格的ID整理成列表,脚本会逐个遍历处理
  • 定义统一的保护规则(比如要保护的范围、允许编辑的用户)
  • 实现批量保护、批量解锁两个核心函数,再配合触发器按需执行

完整实现脚本

// 替换成你的10个电子表格ID列表
const SPREADSHEET_IDS = [
  "你的表格ID1",
  "你的表格ID2",
  // ... 补充剩余表格ID
];

// 自定义保护规则(示例:保护指定子表的特定范围、所有子表的表头行)
const PROTECTION_RULES = [
  {
    sheetName: "Sheet1", // 指定子表名称,留空则匹配所有子表
    rangeA1: "A1:C10", // 要保护的A1格式范围,留空则保护整个子表
    description: "核心数据区保护",
    editors: {
      users: ["你的邮箱@xxx.com"], // 允许编辑的用户邮箱
      groups: [], // 允许编辑的用户组(可选)
      domainEditors: false // 是否允许整个域名用户编辑(可选)
    }
  },
  {
    sheetName: "", // 匹配所有子表
    rangeA1: "1:1", // 保护表头行
    description: "表头保护",
    editors: {
      users: ["你的邮箱@xxx.com"],
      groups: []
    }
  }
];

/**
 * 批量为所有目标表格应用保护设置
 */
function batchProtectSpreadsheets() {
  SPREADSHEET_IDS.forEach(spreadsheetId => {
    try {
      const ss = SpreadsheetApp.openById(spreadsheetId);
      applyProtectionToSpreadsheet(ss);
      console.log(`已完成表格 ${ss.getName()} 的保护配置`);
    } catch (e) {
      console.error(`处理表格 ${spreadsheetId} 出错: ${e.message}`);
    }
  });
}

/**
 * 为单个表格应用保护规则
 * @param {Spreadsheet} ss 目标电子表格对象
 */
function applyProtectionToSpreadsheet(ss) {
  // 可选:先移除旧保护,若需要保留原有保护可注释此行
  // removeAllProtections(ss);
  
  const sheets = ss.getSheets();
  sheets.forEach(sheet => {
    PROTECTION_RULES.forEach(rule => {
      // 匹配子表名称(规则指定了sheetName才触发匹配)
      if (rule.sheetName && sheet.getName() !== rule.sheetName) return;
      
      let range;
      if (rule.rangeA1) {
        range = sheet.getRange(rule.rangeA1);
      } else {
        range = sheet.getDataRange();
      }
      
      // 创建或更新保护
      let protection = range.getProtection(SpreadsheetApp.ProtectionType.RANGE);
      if (!protection) {
        protection = range.protect();
      }
      
      // 设置保护描述
      protection.setDescription(rule.description);
      
      // 更新编辑权限
      const existingEditors = protection.getEditors();
      protection.removeEditors(existingEditors); // 清空现有编辑者
      if (rule.editors.users.length > 0) {
        protection.addEditors(rule.editors.users);
      }
      if (rule.editors.groups.length > 0) {
        protection.addEditorGroups(rule.editors.groups);
      }
      protection.setDomainEdit(rule.editors.domainEditors || false);
      
      // 设置严格保护(false=禁止编辑,true=仅警告)
      protection.setWarningOnly(false);
    });
  });
}

/**
 * 批量移除所有目标表格的保护设置
 */
function batchUnprotectSpreadsheets() {
  SPREADSHEET_IDS.forEach(spreadsheetId => {
    try {
      const ss = SpreadsheetApp.openById(spreadsheetId);
      removeAllProtections(ss);
      console.log(`已移除表格 ${ss.getName()} 的所有保护`);
    } catch (e) {
      console.error(`处理表格 ${spreadsheetId} 出错: ${e.message}`);
    }
  });
}

/**
 * 移除单个表格的所有保护
 * @param {Spreadsheet} ss 目标电子表格对象
 */
function removeAllProtections(ss) {
  const sheets = ss.getSheets();
  sheets.forEach(sheet => {
    // 移除范围保护
    const rangeProtections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
    rangeProtections.forEach(protection => {
      if (protection.canEdit()) {
        protection.remove();
      }
    });
    // 移除工作表级保护
    const sheetProtection = sheet.getProtection(SpreadsheetApp.ProtectionType.SHEET);
    if (sheetProtection && sheetProtection.canEdit()) {
      sheetProtection.remove();
    }
  });
}

使用步骤

  1. 替换表格ID:把SPREADSHEET_IDS数组里的占位符换成你的10个表格ID(表格ID在URL的d/和/edit之间)
  2. 调整保护规则:修改PROTECTION_RULES数组,按需调整子表名称、保护范围、允许编辑的用户
  3. 设置触发器:在Google脚本编辑器中,点击「编辑」→「当前项目的触发器」,添加新触发器:
    • 选择触发函数(比如batchProtectSpreadsheets)
    • 选择触发方式(时间驱动按周期执行,或手动触发按需运行)
  4. 授权运行:首次运行脚本需要授权,确保你的账号拥有所有目标表格的编辑权限

注意要点

  • 若不需要清空旧保护,可注释applyProtectionToSpreadsheet里的removeAllProtections(ss)调用
  • setWarningOnly(false)是严格保护,用户无法编辑受保护区域;设为true仅弹出警告,用户仍可编辑
  • 10个表格的操作完全在Google脚本配额范围内,不用担心触发限制

内容的提问来源于stack exchange,提问作者Nikhil Saini

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 23:26:08