Google表单,用于将电子表格和电子邮件中的数据提取给用户

时间:2014-08-08 03:19:36

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

我有一张电子表格,其中包含每位员工的所有年假余额。我有一个工作人员将填写的表格,我希望它从电子表格中提取他们的数据并向他们发送包含结果的电子邮件。

我遇到了将表单数据与电子表格数据匹配以及提取相关单元格的问题。这是代码:

function SendGoogleForm(e) {  
  try {      

    var subject = "Leave Balance Request";  
    var s = SpreadsheetApp.openByUrl("url").getSheetByName("Check Leave");

    var email = e.namedValues["BCA National email address"];
    var sheet =  SpreadsheetApp.openByUrl("url").getSheetByName("Leave Summary");
    var cell = sheet.getRange("F3:F3");
    var getcell = cell.getCell(1,1);
    var pdate = getcell.setValue(e.namedValues['END date of leave requested']);
    var annualvalue = sheet.getRange("H6:H").getValues();
    var emaildata = sheet.getRange("A6:A").getValues();
    var username = e.namedValues["Username"];

    var message = "Your" + " " + e.namedValues["Type of leave requested"] + " " + "balance        is below" + "\n\n";   

    if (username == emaildata)  { message += "Annual Leave" + " : " +    annualvalue               + "\n\n"; }

    MailApp.sendEmail(email, subject, message);

  } 

  catch (e) {
    Logger.log(e.toString());
  }
}

我试过这个:How do I search for and find the coordinates of a row in Google Spreadsheets

但是无法让它发挥作用......

任何建议都将不胜感激!

1 个答案:

答案 0 :(得分:0)

当您编写var emaildata = sheet.getRange("A6:A").getValues();时,您将获得一个包含字符串的数组数组。您需要迭代for loop来测试每个值。

这样的事情应该有效:

var message = "Your" + " " + e.namedValues["Type of leave requested"] + " " + "balance        is below" + "\n\n";   
for(var n in emaildata){
  if (username == emaildata[n][0])  { message += "Annual Leave" + " : " +    annualvalue               + "\n\n"; }
}
MailApp.sendEmail(email, subject, message);

}