MailApp.sendEmail无法使用onEdit函数(Google表格脚本编辑器)

时间:2018-01-16 15:27:50

标签: javascript google-apps-script google-sheets

我试图在编辑电子表格时超过某些每日限制时创建弹出式和电子邮件通知。

当与今天相关的值超出限制并运行脚本时,会发送弹出通知和电子邮件通知。但是当使用onEdit函数时,即编辑编辑范围中定义的列..只会弹出通知,不会发送任何电子邮件。

有谁知道为什么onEdit函数适用于SpreadsheetApp.getUi()。alert而不是MailApp.sendEmail?为什么它在代码运行时有效,而不是onEdit?

  function onEdit(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Lab Analysis');

 // define edit range
  var editRange = sheet.getActiveRange();
  var editRow = editRange.getRow();
  var editCol = editRange.getColumn();
  var range = sheet.getRange("AG6:AG");
  var rangeRowStart = range.getRow();
  var rangeRowEnd = rangeRowStart + range.getHeight();
  var rangeColStart = range.getColumn();
  var rangeColEnd = rangeColStart + range.getWidth();

  // if cells lie within the edit range, run the following script
   if (editRow >= rangeRowStart && editRow <= rangeRowEnd
      && editCol >= rangeColStart && editCol <= rangeColEnd) {

   // set today's date and store a date object for today
    var date = ss.getSheetByName('Daily Process 
Limits').getRange("B1").setValue(new Date()).getValue();

   // get values in date range
    var daterange = sheet.getRange("A6:A").getValues();

   // iterate the values in the range object
    for(var i=0; i<daterange.length; i++) {

    // compare only month/day/year in the date objects
      if (new Date(daterange[i]).setHours(0,0,0,0) == 
date.setHours(0,0,0,0)) {

    // if there's a match, set the row
    // i is 0 indexed, add 6 to get correct row
      var today_row = (i+6);
      var today_set = ss.getSheetByName('Daily Process 
Limits').getRange("D1").setValue(today_row);  
      var today_fos_tac_f1 = sheet.getRange("AE"+today_row).getValue();
      var today_fos_tac_f2 = sheet.getRange("AF"+today_row).getValue();
      var today_fos_tac_pf = sheet.getRange("AG"+today_row).getValue();

    // pop up notifications to operator
    if (today_fos_tac_f1 > 0.3) {
        SpreadsheetApp.getUi().alert('pop up notification content'); }
    if (today_fos_tac_f2 > 0.3) {
        SpreadsheetApp.getUi().alert('pop up notification content'); }
    if (today_fos_tac_pf > 0.3) {
        SpreadsheetApp.getUi().alert('pop up notification content'); }

     // Set email addresses
    var emails = ['emailaddress@gmail.com'];

     // send email notification to site manager
    if (today_fos_tac_f1 > 0.3) {
      MailApp.sendEmail(emails, 'High FOS:TAC in Fermenter 1', 'email content');}
    if (today_fos_tac_f2 > 0.3){
      MailApp.sendEmail(emails, 'High FOS:TAC in Fermenter 2', 'email content');}
    if (today_fos_tac_pf > 0.3){
      MailApp.sendEmail(emails, 'High FOS:TAC in Post Fermenter', 'email content');}
    }
    }
   }}

2 个答案:

答案 0 :(得分:3)

我认为问题在于,由于简单的触发器会自动触发而不需要授权,因此无法访问需要授权的服务。 Reference

答案 1 :(得分:2)

我同意Cooper。您可以在try中捕获代码并捕获以查看错误(下面的代码)消息。

你可以绕过这个设置时间驱动的触发器。假设它设置为一分钟,它会查看数据以检查您要查找的条件,然后在满足条件时发送电子邮件。

  function onEdit(e) {

     // Set email address
  var emails = ['some@email.com'];

     // send email notification to site manager
   try{
      MailApp.sendEmail(emails, 'High FOS:TAC in Fermenter 1', 'email content')
    } catch (e){
      Logger.log(e)
    }
  }

这是记录器错误。

enter image description here