如何打开Google表格时自动跳转至当日日期对应工作表?
实现Google表格打开自动跳转当日工作表并保护表名
1. 编写Google Apps Script代码
打开你的Google表格,点击顶部菜单栏「扩展程序」→「Apps 脚本」,清空默认代码后粘贴以下内容:
function onOpen() { // 获取当日日期的日份(1-31) const today = new Date().getDate(); const targetSheetName = `${today}Day`; const spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); const targetSheet = spreadsheet.getSheetByName(targetSheetName); // 定位到当日对应的工作表 if (targetSheet) { spreadsheet.setActiveSheet(targetSheet); } else { // 当日无对应工作表时弹出提示(可按需删除) SpreadsheetApp.getUi().alert(`未找到「${targetSheetName}」工作表`); } // 保护所有工作表名称不被修改(同时保护工作表结构,如行列增删) const sheets = spreadsheet.getSheets(); sheets.forEach(sheet => { const protection = sheet.protect(); // 开启强制保护(而非仅警告) protection.setWarningOnly(false); // 移除普通编辑器的结构修改权限 const editors = protection.getEditors(); protection.removeEditors(editors); // 保留表格所有者的结构修改权限(可按需调整) protection.addEditor(spreadsheet.getOwner()); // 不限制单元格内容编辑,仅保护工作表结构(包括名称) protection.setProtectedRanges([]); protection.setDescription('禁止修改工作表名称及结构'); }); }
2. 配置触发器(可选,内置onOpen已自动生效)
onOpen是Google Apps Script的内置简单触发器,保存代码后,下次打开表格就会自动执行。如果需要确认触发器配置:
- 在Apps脚本编辑器左侧点击「触发器」图标(时钟样式)
- 点击「添加触发器」,按以下参数配置:
- 运行函数:
onOpen - 部署类型:
Head - 事件源:
从电子表格 - 事件类型:
打开时
- 运行函数:
- 点击「保存」,按提示完成授权即可。
关键说明
- 代码会在表格打开时自动定位到当日对应的「XDay」工作表,比如1号打开就跳转到「1Day」。
- 保护逻辑通过锁定工作表结构实现,确保无法修改工作表名称,同时不影响单元格内容的编辑操作。
- 如果当日日期超出1-31范围(如不存在的日期),会弹出提示,你可以根据需求删除提示代码段。
内容的提问来源于stack exchange,提问作者user19395735
相关产品推荐
相关产品推荐

