我是一个完整的初学者,但我一步一步地开始理解Apps脚本的基础知识。我在Google Dev上发现了这个示例邮件合并脚本:https://developers.google.com/apps-script/articles/mail_merge
这是我正在讨论的剧本的一部分:
function sendEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = ss.getSheets()[0];
var dataRange = dataSheet.getRange(2, 1, dataSheet.getMaxRows() - 1, 4);
var templateSheet = ss.getSheets()[1];
var emailTemplate = templateSheet.getRange("A1").getValue();
// Create one JavaScript object per row of data.
var objects = getRowsData(dataSheet, dataRange);
// For every row object, create a personalized email from a template and send
// it to the appropriate person.
for (var i = 0; i < objects.length; ++i) {
// Get a row object
var rowData = objects[i];
// Generate a personalized email.
// Given a template string, replace markers (for instance ${"First Name"}) with
// the corresponding value in a row object (for instance rowData.firstName).
var emailText = fillInTemplateFromObject(emailTemplate, rowData);
var emailSubject = "Tutorial: Simple Mail Merge";
MailApp.sendEmail(rowData.emailAddress, emailSubject, emailText);
}
}
我试图更改脚本,因此只有当特定列中的单元格值与给定值匹配时才会运行。我给你举个例子:
在电子表格中使用公式,E列中的单元格值为“是”和“#”。如果条件得到满足。我希望邮件合并脚本只在值为&#39;是&#39;时才发送电子邮件。此外,我想脚本自动运行。
我正在考虑使用触发器来实现这一目标,但现在我不知所措。你们能给我一些关于在哪里看的指示吗?我的一个朋友说这可能是不可能的,但我喜欢第二种意见。提前谢谢!
答案 0 :(得分:0)
我也是一个初学者。可能有更好的方法来实现你的目标,但这是我的解决方案:
我在电子表格中命名了您的列E&#34;条件&#34;,这是&#34;是&#34;或者&#34; no&#34;被放置。
您引用的代码,只有两个不同之处:
function sendEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = ss.getSheets()[0];
// here is the first difference:
// change the dataRange, last number from 4 to 5, because we want to catch five columns:
var dataRange = dataSheet.getRange(2, 1, dataSheet.getMaxRows() - 1, 5);
var templateSheet = ss.getSheets()[1];
var emailTemplate = templateSheet.getRange("A1").getValue();
// Create one JavaScript object per row of data.
var objects = getRowsData(dataSheet, dataRange);
// For every row object, create a personalized email from a template and send
// it to the appropriate person.
for (var i = 0; i < objects.length; ++i) {
// Get a row object
var rowData = objects[i];
// Generate a personalized email.
// Given a template string, replace markers (for instance ${"First Name"}) with
// the corresponding value in a row object (for instance rowData.firstName).
var emailText = fillInTemplateFromObject(emailTemplate, rowData);
var emailSubject = "Tutorial: Simple Mail Merge";
// here comes the second difference:
// test if the rowData named "condition" is "yes", if true, the mail is send
if (rowData.condition == "yes"){
GmailApp.sendEmail(rowData.mail, emailSubject, emailText);
}
}
}
我已更改为GmailApp,因为MailApp不适合我。
脚本可以自主运行,您可以设置触发器:在Ressources下找到它,从实际项目触发,选择函数&#34; sendmail&#34;,触发&#34;在表单提交&#34;。当条件来自公式时,您应该看一下Addon&#34; CopyDown&#34;。在提交表单时,它会将公式从第二行复制到新行。