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

如何在Google Apps Script自动邮件脚本中排除指定工作表

解决Google Apps Script排除指定工作表发送邮件的问题

你的代码里排除工作表的逻辑出错了——sheet是Sheet对象,不是字符串,直接和"Data"比较永远不会相等,所以排除无效。要改成获取工作表名称来判断,同时加入对"Carpet"的排除,修改后的代码如下:

function autoemail(e) {
  let sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  // 获取当前工作表名称
  let sheetName = sheet.getName();
  // 定义需要排除的工作表数组
  const excludedSheets = ["Data", "Carpet"];

  let range = e.range;
  let row = e.range.getRow();
  let col = e.range.getColumn();
  let cellValue = sheet.getActiveCell().getValue();

  let address = sheet.getRange(row,1).getValue();
  let ordernumber = sheet.getRange(row,2).getValue();
  let installdate = sheet.getRange(row,3).getValue();
  let jobtype = sheet.getRange(row,4).getValue();
  let fieldsuper = sheet.getRange(row,5).getValue();
  let notes = sheet.getRange(row,11).getValue();
  let holdstatus = sheet.getRange(row,12).getValue();
  let user = Session.getActiveUser().getEmail();
  let cellLocation = sheet.getActiveCell().getA1Notation();
  let url = "MyURL"

  // 修改判断条件:列是11,且当前工作表不在排除列表中
  if(col == 11 && !excludedSheets.includes(sheetName)) {
    MailApp.sendEmail(
      'MyEmail',
      address + ' has been updated🔥',
      'This an automated notice that ' + fieldsuper + ' has updated ' + address + ' (' + ordernumber + ') with the notes ' + notes + ', the current status is ' + holdstatus + ' for the install date of ' + installdate + '. For more information, please check the carpet sheet directly at ' + url
    );
  };
}

关键修改点:

  • 用sheet.getName()获取当前工作表的名称字符串,这才是能和"Data""Carpet"直接比较的内容
  • 把需要排除的表名放进数组excludedSheets,用!excludedSheets.includes(sheetName)判断当前表是否不在排除列表里,逻辑更清晰,后续要新增排除表只需修改数组即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 22:25:41