长时间运行后无法让 MailApp.sendEmail 在 google 应用程序脚本中工作

Can not get MailApp.sendEmail to work in google app script after a long function

我写了一段代码,它从原始形式 sheet 中获取一个条目,然后将其与第二个 sheet 匹配以找到它所在的行。然后我使用该行将列中单元格中的电子邮件拉到右侧。

该代码还采用 sheet 形式的三个条目(员工姓名、沙龙信息和生效日期),并替换另一个 sheet 中的值以形成 'body'

当我尝试调试代码时,所有变量(电子邮件、主题、正文)都是正确的,但我没有收到电子邮件

我是 Google App Script 的新手,我知道这一定与我处理 for/if 语句并将电子邮件存储为变量有关,但我不知所措此时。

/*
 script that fires on sheet edit(form entry)
 reads the salon details from the 'Salon Information' column of the most recent form entry, last row
 function then matches the 'Salon Information' to the "GM Match:sheet" and pulls the GM email.
 final action is taking 'Employee Name','Salon Information' and 'Effective Date' and replacing 
 the values in the "Email Sheet:Sheet" and asigning it to the body 
*/

function sendAutomatedEmail() {

  //setting active spreadsheet
  SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form Responses 1").activate();
  var sh = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();  // Get main Database Sheet
  var emailsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Email Sheet");  // Get Sheet with the Email Content
  var lrow = sh.getLastRow();   // User which last filled the form

  var eename = sh.getRange(lrow, 2).getValue();     // Get name value. (Row - Last Row, Column - 2)
  var salon = sh.getRange(lrow, 3).getValue();    // Get salon value. (Row - Last Row, Column - 3)
  var effdate = sh.getRange(lrow, 4).getValue();   // Get effective termination date value. (Row - Last Row, Column - 4)
  var subject = "Employee termination notification";

  var body = emailsheet.getRange(1, 1).getValue();   // Get Email Content
  /* 
  Replace variable 'name' with terminated employee's name
  Replace variable 'salon' with salon information
  Replace variable 'effdate' with effective date
  */
  body = body.replace("{eename}", eename).replace("{salon}", salon).replace("{effdate}",effdate);  


  var gmsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("GM Match"); //Get sheet with GM email and salon match
  var data = gmsheet.getDataRange().getValues();
  var sname = salon                         //reassigning variable for salon
  for(var i = 0; i < data.length; i++){     //for loop for data in "GM Match" sheet
    var row = i + 1 //adding a row variable to be called later
    var email = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("GM Match").getRange(row, 2).getValue();
    if(data[i][0] == sname){                //[0] because column A
      Logger.log((i+1))
      return i;
    }
  }

  MailApp.sendEmail(email, subject, body);

}

你真的想要这个吗:

if(data[i][0]==sname){ return i; }

只要是真的,您就不会发送电子邮件。

检查它是否被您代码中的范围授权,

https://www.googleapis.com/auth/script.send_mail

必须包含范围才能通过该方法发送电子邮件。

如果条件 data[i][0] == snametrue,即如果 salon 名称与 GM Match 中的数据匹配,则您正在使用 return,这(如果我没理解错的话)正是你要发送电子邮件的时间。

return keyword 结束函数执行,因此如果 GM Match 中的任何行匹配来自另一个 sheet 的 salon 数据,函数将停止并且将不会发送电子邮件。

所以在这种情况下,你应该改变这个:

    if(data[i][0] == sname){                //[0] because column A
      Logger.log((i+1))
      return i;
    }
  }

  MailApp.sendEmail(email, subject, body);

为此:

    if(data[i][0] == sname) {
      MailApp.sendEmail(email, subject, body);
    }

希望对您有所帮助。