每月15日锁定Google Sheets指定月度列数据技术问询
解决方案:每月15日锁定Google Sheets对应月度列并固化数据
一、替换实时同步为静态快照(彻底解决用户修改同步问题)
IMPORTRANGE是实时拉取数据,用户改多少主表就更多少,所以要改成每月固定时间把数据复制成静态值,之后用户再修改也不会影响主表:
- 打开你的Google表格,点「扩展程序」>「Apps脚本」
- 粘贴下面的脚本,记得把里面的标签页名称、数据范围改成你自己的:
function copyMonthlyData() { // 获取当前月份(1-12),转成对应列字母(A=1月,B=2月...) const currentMonth = new Date().getMonth() + 1; const columnLetter = String.fromCharCode(64 + currentMonth); // 替换成你的用户填写标签页和主表名称 const sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("用户填写页"); const targetSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("主表"); // 复制对应月度列的静态数据(这里假设数据从第2行到第100行,可按需调整) const sourceRange = sourceSheet.getRange(`${columnLetter}2:${columnLetter}100`); const staticValues = sourceRange.getValues(); targetSheet.getRange(`${columnLetter}2:${columnLetter}100`).setValues(staticValues); }
- 设置定时自动执行:在Apps脚本界面点左侧「触发器」>「添加触发器」,按以下配置:
- 选择函数:
copyMonthlyData - 事件源:时间驱动
- 类型:每月计时器
- 日期:15日
- 时间:选你合适的时段(比如上午9点)
- 选择函数:
二、定时锁定用户填写页的对应月度列
复制完静态数据后,把用户填写页里的对应月度列锁起来,防止后续修改:
- 继续在Apps脚本里添加下面的函数,同样替换标签页名称:
function lockMonthlyColumn() { const currentMonth = new Date().getMonth() + 1; const columnLetter = String.fromCharCode(64 + currentMonth); const targetSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("用户填写页"); // 给对应列添加保护 const protection = targetSheet.getRange(`${columnLetter}:${columnLetter}`).protect(); protection.setDescription(`已锁定${currentMonth}月数据`); // 只保留你自己的编辑权限(自动获取当前登录账号) const admin = Session.getEffectiveUser(); protection.addEditor(admin); protection.removeEditors(protection.getEditors()); if (protection.canDomainEdit()) { protection.setDomainEdit(false); } }
- 给这个函数也设置每月15日的触发器,确保先复制数据再锁定。
三、额外优化建议
- 提前设置用户填写页的编辑权限:只开放当前月份的列让用户填写,其他月份列提前锁定,减少误改历史数据的概率
- 主表备份:每月锁定后,在主表新建对应月份的备份标签页,把静态数据复制过去,防止主表数据意外丢失
内容的提问来源于stack exchange,提问作者Nischay Soni
相关产品推荐
相关产品推荐

