
How this page was reviewed
在独立的 Apps Script 项目中测试。示例记录有限的诊断信息,不会在日志中暴露数据或秘密。
安全实现
重现最小失败案例,记录非敏感诊断,并在修改生产触发器前验证服务和权限。
失败情况
授权失败、未定义值、服务配额和触发器上下文差异都很常见。将日志视为敏感信息,并在修复后删除临时诊断。
Implementation note
Tested example
function debugActiveSheet() {
const sheet = SpreadsheetApp.getActiveSheet();
if (!sheet) throw new Error('No active sheet');
console.log(JSON.stringify({ name: sheet.getName(), rows: sheet.getLastRow() }));
}- Required scope
- https://www.googleapis.com/auth/spreadsheets.currentonly
- Expected output
- 执行日志会记录活动工作表名称及其最后一个已填充行。
- Watch for
- 触发器中可能没有活动电子表格上下文;调试非绑定脚本时请传入明确的电子表格 ID。
Official sources
Read the original documentation before changing a production workflow.