用于多个选项卡的MailApp.SendEmail脚本

时间:2018-11-27 21:57:34

标签: google-apps-script

我一直在寻找如何编写脚本来检查并在多个选项卡上运行。现在,我需要一一复制/粘贴并设置执行触发器。

我拥有的脚本:

function SendEmail() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("1");
  var value = sheet.getRange("F3").getValue();
  var subject = "subject";
  var message = "message";

  if (value >0){      
    MailApp.sendEmail("email", subject, message);
  }
}

我每8小时运行一次(触发运行)。

我希望添加更多选项卡,以便如果每个选项卡的值都大于0,则脚本可以为每个选项卡检查并发送电子邮件。

关于如何执行此操作的任何想法?

每个选项卡中的值都在F3下。标签名称,例如1,2,3,4 ...

谢谢!

1 个答案:

答案 0 :(得分:0)

如果我正确理解了这个问题,这应该可以满足您的需求。

function SendEmail() {
  //Get the Spreadsheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  //Get all Sheets in the Spreadsheet
  var sheets = ss.getSheets();

  //Create a variable for the email address, subject and message
  var email = "";
  var subject = "";
  var message = "";

  //Can change the starting sheet by changing the value of "i" - 0 will be the first sheet.
  for (var i = 0; i < sheets.length; i++){

  var sheet = sheets[i];
  var value = sheet.getRange("F3").getValue();

  switch(sheet.getSheetName) {
    case "Sheet1":
       email = "email1@domain.com";
        break;
    case "Sheet2":
        email = "email2@domain.com";
        break;
    default:
        value = 1
        email = "yourEmailAddress@domain.com";
        subject = "Message Failed to Send";
        message = "There was an error sending email, please investigate";
}

  if (value >0){      
    MailApp.sendEmail(email, subject, message);

}//END OF IF STATEMENT
}//END OF FOR LOOP
}//END OF FUNCTION