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

如何配置onEdit触发器仅在Google表格的「Mirror」工作表编辑时触发

Fix: Trigger Script Only When "Mirror" Sheet is Edited

Hey there! Awesome that your script's core features are working perfectly—let's tackle that over-triggering issue quickly. The solution is straightforward: we'll add a check to only run your buildReport function when the edit happens specifically on the Mirror sheet.

Here's how to adjust your code:

Keep your existing buildReport function exactly as it is, then add a new trigger function that validates the edited sheet name:

// Runs automatically whenever the spreadsheet is edited
function onEdit(e) {
  // Get the sheet that was just modified
  const editedSheet = e.source.getActiveSheet();
  
  // Only execute the report logic if the edited sheet is "Mirror"
  if (editedSheet.getName() === 'Mirror') {
    buildReport();
  }
}

// Your original core function (no changes needed!)
function buildReport() { 
  const ss = SpreadsheetApp.getActive(); 
  let data = ss.getSheetByName('Mirror').getRange("A1:B13").getValues(); 
  let payload = buildAlert(data); 
  sendAlert(payload); 
}

// Keep your existing buildAlert, sendAlert, and other supporting functions here

Key Details to Note:

  • The onEdit function is a simple trigger in Google Apps Script that fires automatically on any spreadsheet edit. The e parameter is an event object that gives us context about the edit—we use it to target the specific sheet that was modified.
  • If your sendAlert function requires elevated permissions (like sending emails, calling external APIs, or accessing other Google services), the simple onEdit trigger might fail due to permission restrictions. In that case, set up an installable edit trigger instead:
    1. Open your script editor, click the clock-shaped "Triggers" icon in the left sidebar.
    2. Click "Add Trigger".
    3. Select your trigger function (feel free to rename it to onMirrorEdit for clarity), set the event source to "From spreadsheet", event type to "On edit", then save and complete the authorization flow.
  • Be sure to delete any old triggers you had set directly for buildReport to avoid duplicate, unintended runs!

That's all—your script will now only execute when changes are made to the Mirror sheet, eliminating unnecessary triggers while preserving all your existing functionality.

内容的提问来源于stack exchange,提问作者Scott Shimer

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 02:47:29