无法使用GAS向单元格正确添加价值

时间:2018-09-04 14:58:15

标签: javascript google-apps-script google-sheets for-in-loop

我采用了以下脚本,但遇到两个问题。

  1. “电子邮件已发送”值未在正确的行中更新。假设我向第5行的收件人发送了一封电子邮件,而不是出现在第5行的第10列中,而是在第21行的第10列中显示了“已发送电子邮件”。
  2. 即使我知道电子邮件已丢失,有时也根本不会出现“已发送电子邮件”。

我已经尝试了所有可以想到的方法,但是我无法使其正常工作。

//Send approval or non-approval of time off request
function sendLeaveRequestDecisions() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Form Responses 2");
  var dataRange = sheet.getRange(2,1,sheet.getLastRow()-1,sheet.getLastColumn());
  var data = dataRange.getValues();
  for (i in data) {

    var startRow = 2; //First row of data to process
    var rowData = data [i];
    var startdate = rowData [2];
    var enddate = rowData [3];
    var type = rowData [4];
    var email = rowData [5];
    var recipient = rowData [1];
    var decision = rowData [7];
    var comments = rowData [8];
    var emailstatus = rowData [9]
    var emailPattern = /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|aero|asia|biz|com|coop|edu|gov|info|int|jobs|mil|mobi|name|museum|name|net|org|pro|tel|travel)\b/;
    var validEmailAddress = emailPattern.test(email); 

    if (validEmailAddress == true && emailstatus != "Email Sent") {    


        var message = "<HTML><BODY>"
        + "<P>Dear " + recipient + ","
        + "<br /><br />"
        + "<P>The following request:"
        + "<br /><br />"
        + "<b>Type:  </b>" + type + "<br />" 
        + "<b>From:  </b>" + startdate + "<br />" 
        + "<b>To:  </b>" + enddate + "<br />" 
        + "<br /><br />" 
        + "<b>is </b>" + decision + "<br />"   
        + "<b>Comments </b>" + comments + "<br />"
        + "<br /><br />"    
        + "Diane"
        + "<br /><br />" 
        + "</HTML></BODY>";      
      MailApp.sendEmail(email, "Regarding your leave request", "", {htmlBody: message});   
   sheet.getRange(i + 2,10).setValue("Email Sent"); 
      // Make sure the cell is updated right away in case the script is interrupted
      SpreadsheetApp.flush();
    }
}
  }

2 个答案:

答案 0 :(得分:0)

尝试一下:

  

最主要的问题是您没有逃避正则表达式中包含的正斜杠。我很惊讶它完全运行了。对我来说还可以。您可能想看看Utilities.formatString()来构建您的消息。只需单击“帮助/ API参考”。从那里开始,它在左下角显示脚本服务/实用工具。

就我个人而言,我不会像这样循环使用同花顺。我也将startRow和emailPattern移出了循环。他们没有改变。您放入循环的次数越少,运行速度就越快。我将MailApp.sendmail注释掉了,因为我实际上并不希望发送电子邮件,并且我认为您没错。

function sendLeaveRequestDecisions() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Index Generation");
  var dataRange = sheet.getRange(2,1,sheet.getLastRow()-1,sheet.getLastColumn());
  var data = dataRange.getValues();
  var startRow = 2; //First row of data to process
  var emailPattern = /^[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|aero|asia|biz|com|coop|edu|gov|info|int|jobs|mil|mobi|name|museum|name|net|org|pro|tel|travel)\b/;
  for (var i=0;i<data.length;i++){
    var rowData = data [i];
    var startdate = rowData[2];
    var enddate = rowData[3];
    var type = rowData[4];
    var email = rowData[5];
    var recipient = rowData[1];
    var decision = rowData[7];
    var comments = rowData[8];
    var emailstatus = rowData[9];
    var validEmailAddress = emailPattern.test(email); 
    if (validEmailAddress == true && emailstatus != "Email Sent") {
      var message = "<HTML><BODY>" + "<P>Dear " + recipient + "," + "<br /><br />" + "<P>The following request:" + "<br /><br />" + "<b>Type:  </b>" + type + "<br />" + "<b>From:  </b>" + startdate + "<br />" + "<b>To:  </b>" + enddate + "<br />"  + "<br /><br />" + "<b>is </b>" + decision + "<br />"   + "<b>Comments </b>" + comments + "<br />"+ "<br /><br />" + "Diane"+ "<br /><br />" + "</HTML></BODY>";          
      //MailApp.sendEmail(email, "Regarding your leave request", "", {htmlBody: message});   
      sheet.getRange(i + 2,10).setValue("Email Sent"); 
    }
  }                                             
  //SpreadsheetApp.flush();                                             
}

答案 1 :(得分:0)

我做过类似但完全不同的事情,而且我个人过去认为这种反应无济于事,所以如果您有这种感觉,请告诉我不要理会。

如果,您希望我尝试写一些您和其他人可以运行的东西,那么我会这样做。我这样做的例子是个人的,甚至比您个人的更是如此。