我是google脚本的新手,所以请多多包涵。我正在使用Google表单和Google表格自动进行2个条件研究注册的交流。本质上,当参与者提交表单时,我需要发送一封确认电子邮件(触发信息是表单提交),并附上他们选择的日期,并根据他们的状况提供一个新的唯一ID(我需要使用两个单独的预制ID列表) )。现在,我有了主要的表单响应表,然后使用=查询根据调查中的多项选择题(“早晨”和“晚上”)将调查回答分为两种情况的表。我将ID列表填充到相应的工作表中,以便在=查询对响应进行排序时,它们会在行填充时自动“分配”一个ID。我的代码可以发送不带ID的电子邮件,仅使用电子表格第一页(表单响应表)中的名称和日期,但是在从另一个对应的表中获取ID并放入电子邮件中时遇到了问题。
这就是我所拥有的:
function IntialEmails(e){
var ss = SpreadsheetApp.openById('MyownSpreadsheetID'); //not including real ID for privacy reasons
var type = e.values[4];
if (type == 'Condition A') {
var sheet = ss.getSheets()[1];
ss.setActiveSheet(sheet);
}
if (type == 'Condition B') {
var sheet = ss.getSheets()[2];
ss.setActiveSheet(sheet);
}
var userName = sheet.values[0];
var userEmail = sheet.values[2];
var date = sheet.values[4]; //Using date from corresponding spreadsheet based on survey format
var ID = sheet.values[5]; // Should be from corresponding sheet
var subject = "Study Signup Confirmation and Participant ID";
var templ = HtmlService
.createTemplateFromFile('My html'); //not including real ID for privacy reasons
templ.date = date;
templ.ID = ID;
templ.userName = userName;
var message = templ.evaluate().getContent();
MailApp.sendEmail({
to: userEmail,
subject: subject,
htmlBody: message
});
}
我非常困惑,并且不断出错。这段代码抛出错误“ TypeError:无法读取未定义的属性'0'”如果有人可以帮助我,将不胜感激!
答案 0 :(得分:0)
如果要从工作表中获取值,可以执行以下操作:(即使我通常不会以这种方式进行操作,我也会遵循您使用的相同方法。我将假设您有自己的ID或行中有什么数据。但是显然我不知道,因为您没有提供该信息。
function initEmails(e) {
var ss=SpreadsheetApp.getActive();
var shts=ss.getSheets();
var sh=(e.values[4]=="Condition A")?shts[1]:shts[2];
var vs=sh.getRange(1,1,sh.getLastRow(),7).getValues();
for(var i=0;i<vs.length;i++) {
if(vs[i][7]!='USED') {
var userName=vs[i][0];
var userEmail=vs[i][2];
var date=vs[i][4];
var ID=vs[i][5];
var subject = "Study Signup Confirmation and Participant ID";
sh.getRange(i+1,8).setValue('USED');//this provides a way to keep track of which row was used last
}
/*.............I have no idea what your trying to do beyond this point.
}
但是我不认为您可以通过以下方式将这些值插入到html模板中:
templ.date = date;
templ.ID = ID;
templ.userName = userName;
通常,在评估过程中,通过使用脚本,模板中会填充所需的数据。