我有一个电子表格,可以在工作表1上存储任务。当任务完成时,会发送一封电子邮件。表2包含发送至,cc和回复的特定电子邮件地址。我能够遍历表2并获取每列的电子邮件地址。我希望能够将这些电子邮件地址放入MailApp中发送电子邮件的选项中。我无法从表2的循环中获取电子邮件地址。请参阅下面的代码。任何帮助都会很感激。
var EMAIL_SENT = "EMAIL_SENT";
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheets()[0];
var sheet2 = ss.getSheets()[1];
var startRow = 2;
var lastRow1 = sheet1.getLastRow()-1;
var lastCol1 = sheet1.getLastColumn();
var lastRow2 = sheet2.getLastRow()-1;
var lastCol2 = sheet2.getLastColumn();
var sh1Range = sheet1.getRange(startRow, 1, lastRow1, lastCol1).getValues();
var sh2Range = sheet2.getRange(startRow, 1, lastRow2, lastCol2).getValues();
var subject = "Test Email";
//sheet2 Email Loop
for (var i = 0; i < sh2Range.length; ++i){
var emails = sh2Range[i];
var to = emails[0];
var cc = emails[1];
var replyTo = emails[2];
}
//sheet1 Data Loop and Send Email
for (var j = 0; j < sh1Range.length; ++j){
var data = sh1Range[j];
var pName = data[0];
var pID = data[1];
var pm = data[2];
var dd = Utilities.formatDate(new Date(data[3]), "America/New_York", "MMMM dd, yyyy");
var team1 = data[4];
var status = data[7];
if (team1 == "Task Completed" && status !== EMAIL_SENT){
var htmlBody;
htmlBody = "Project Name: "+pName+"<br>"+"Project ID: "+pID+"<br>"+"Project Manager: "+pm+"<br>"+"Due Date: "+dd+"<br>";
var optAdvancedArgs = {replyTo: replyTo, cc: cc, name: "Test Email", htmlBody: htmlBody};
//Logger.log(htmlBody);
//Logger.log(optAdvancedArgs);
}
}
}
答案 0 :(得分:0)
问题有点广泛,有很多方法可以做你想要的事情。当你的申请细节不清楚时,很难说你应该选择什么,比如第2页上会有多少地址,如果你真的需要经历一个&#39 ;如果&#39;声明,但我会提供两种可能有用的潜在解决方案。
解决方案1:我在上面的评论中试图确定的是,电子邮件地址列表的长度是否与项目列表的长度相同(例如,如果表单上有10个项目1,这是否意味着表2中有10行电子邮件地址。它仍然不清楚,因为你的CC&#39;中有一个电子邮件地址列表。地址,但只有一个在你的发送到&#39;并且&#39;回复&#39;地址(所以你不需要在整个范围内循环,但也许这可能是项目其他部分所需要的)。但是,如果是这种情况,那么你实际上可以在一个&#39;中实现所有这一切。循环:
//sheet1 Data Loop and Send Email
for (var j = 0; j < sh1Range.length; ++j){
var data = sh1Range[j];
var pName = data[0];
var pID = data[1];
var pm = data[2];
var dd = Utilities.formatDate(new Date(data[3]), "America/New_York", "MMMM dd, yyyy");
var team1 = data[4];
var status = data[7];
if (team1 == "Task Completed" && status !== EMAIL_SENT){
var emails = sh2Range[i];
var to = emails[0];
var cc = emails[1];
var replyTo = emails[2];
var htmlBody;
htmlBody = "Project Name: "+pName+"<br>"+"Project ID: "+pID+"<br>"+"Project Manager: "+pm+"<br>"+"Due Date: "+dd+"<br>";
var optAdvancedArgs = {replyTo: replyTo, cc: cc, name: "Test Email", htmlBody: htmlBody};
//Logger.log(htmlBody);
//Logger.log(optAdvancedArgs);
}
}
解决方案2:否则,您可以在循环播放时将电子邮件地址放入数组中:
var addresses = [];
for (var i = 0; i < sh2Range.length; ++i){
var emails = sh2Range[i];
var to = emails[0];
var cc = emails[1];
var replyTo = emails[2];
addresses.push([to,cc,replyTo])
}
然后,您可以使用括号表示法#[0] [0]&#39;地址[0] [0]&#39;稍后在剧本中:
var optAdvancedArgs = {replyTo: addresses[0][2], cc: addresses[j][1], name: "Test Email", htmlBody: htmlBody};
这些解决方案都会对您正在做的事情做出很多假设,但它们可能会引导您找到您正在寻找的答案,或者可能让您更具体地了解问题类型你在问。