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

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 12:16:09