长功能后无法使MailApp.sendEmail在Google应用程序脚本中工作

问题描述 投票:0回答:3

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

该代码还从表单中获取了三个条目(员工姓名,沙龙信息和生效日期),并替换了另一个表单中的值以形成'主体'

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

我对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);

}
google-apps-script google-sheets google-apps-script-editor
3个回答
1
投票

您真的想要这个:

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

每当属实时,您不会发送电子邮件。


1
投票

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

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

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


0
投票

如果条件returndata[i][0] == sname,也就是说,如果true名称与salon中的数据匹配,则您正在使用GM Match,这(如果我理解正确的话)正是您想要发送电子邮件。

© www.soinside.com 2019 - 2024. All rights reserved.