Google表单提交后,关联Sheet的onChange脚本未触发问题排查
问题:Google Form提交后,Apps Script触发器未触发
我有一个Google Form,提交后会将支出记录同步到Google Sheet的expenses工作表中。我编写了一段脚本,预期每次expenses工作表发生变更时,自动发送包含另一个summary工作表数值的邮件。单独测试sendSpendingSummary()功能正常,邮件能成功发送且格式无误,但触发器完全未运行——既无报错,触发器页面也无执行记录(截图如下)。
当前仅需排查:为何Google Form提交成功向expenses工作表添加行后,脚本触发器完全不运行?
脚本代码
function onChange(e) { var sheetName = "expenses" var sheet = e.source.getSheetByName(sheetName) if (!sheet) { Logger.log("Sheet not found: " + sheetName) return } var currentRowCount = sheet.getLastRow() var properties = PropertiesService.getScriptProperties() var lastRowCount = properties.getProperty("lastRowCount") if (!lastRowCount) { properties.setProperty("lastRowCount", currentRowCount) return } lastRowCount = parseInt(lastRowCount) if (currentRowCount > lastRowCount) { sendSpendingSummary() Logger.log("Sent spending summary by email") } properties.setProperty("lastRowCount", currentRowCount) } function sendSpendingSummary() { var sheet = SpreadsheetApp.getActive().getSheetByName('summary') var range = sheet.getRange("A1") var spending = range.getValue().toLocaleString("en-US", { style: "currency", currency: "USD"}) var recipient = "michael@example.com" var subject = "Month-to-Date Spending Summary" var body = "Month-to-Date Spending as of " + Utilities.formatDate(new Date(), "EST", "MMM d, yyyy") + ": " + spending MailApp.sendEmail(recipient, subject, body) }
触发器状态截图

内容的提问来源于stack exchange,提问作者Michael A
相关产品推荐
相关产品推荐

