[英]Google Form to pull data from spreadsheet and email to user
I have a spreadsheet containing all of the annual leave balances for each staff member. 我有一个电子表格,其中包含每个工作人员的所有年假余额。 I have a form that the staff will fill out, and I would like it to pull the their data from the spreadsheet and send them an email with the result.
我有一个表格供员工填写,我希望它从电子表格中提取他们的数据,并向他们发送一封包含结果的电子邮件。
I am having trouble with the matching of form data to the spreadsheet data, and extracting the related cells. 我在将表单数据与电子表格数据匹配以及提取相关单元格时遇到麻烦。 Here is the code:
这是代码:
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());
}
}
I tried this: How do I search for and find the coordinates of a row in Google Spreadsheets 我尝试了这一点: 如何在Google Spreadsheets中搜索并找到行的坐标
But couldn't get it to work... 但是无法正常工作...
Any suggestions would be greatly appreciated! 任何建议将不胜感激!
When you write var emaildata = sheet.getRange("A6:A").getValues();
当您编写
var emaildata = sheet.getRange("A6:A").getValues();
, you are getting an array of arrays with strings. ,您将获得一个包含字符串的数组数组。 You will need to iterate in a
for loop
to test each value. 您将需要在
for loop
进行迭代以测试每个值。
Something like that should work : 这样的事情应该工作:
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);
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.