[英]Send email to the new google form submission only
I'm new to the Google apps script.我是 Google Apps 脚本的新手。 I wrote a script to send emails when there is a new submission from google forms using data and template from a spreadsheet.
当使用电子表格中的数据和模板从谷歌表单提交新的提交时,我编写了一个脚本来发送电子邮件。 However, it sends an email to not just the new submission but also to all of the previous submissions.
但是,它不仅会向新提交的内容发送电子邮件,还会向所有以前的提交内容发送电子邮件。 The whole script is quite long, so I only copy a short part of it.
整个剧本很长,所以我只复制了一小部分。 Is there any way to fix it?
有什么办法可以解决吗?
Here is the link to the spreadsheet https://docs.google.com/spreadsheets/d/1fhuwEndIS3khg3W19jpQnBAaCp_MMrD_bfATrdf2-4I/edit?usp=sharing这是电子表格的链接https://docs.google.com/spreadsheets/d/1fhuwEndIS3khg3W19jpQnBAaCp_MMrD_bfATrdf2-4I/edit?usp=sharing
Thank you.谢谢你。
function sendEmail() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Calculation");
var lr = ss.getLastRow();
for (var i = 3; i<=lr;i++){
var currentEmail = ss.getRange(i, 1).getValue();
var currentName = ss.getRange(i, 3).getValue();
var currentScore1 = ss.getRange(i, 4).getValue();
MailApp.sendEmail(
currentEmail,
subjectline,
"HTML",
{ htmlBody: messageBody }
);
}
}
Instead of reading the values from the spreadsheet take advantage of the form submit event object.而不是从电子表格中读取值,而是利用表单提交事件对象。 This event object has two properties including the form submission values, one is an Array of form submission values in the same order than the sheet columns, the other is an object having a property for each question each of them having an Array of values.
这个事件对象有两个属性,包括表单提交值,一个是表单提交值的数组,其顺序与工作表列相同,另一个是每个问题都有一个属性的对象,每个问题都有一个值数组。 Ref.
参考。 https://developers.google.com/apps-script/guides/triggers/events
https://developers.google.com/apps-script/guides/triggers/events
This shows the changes that need to done to your script:这显示了需要对脚本进行的更改:
function sendEmail(e) {
var currentEmail = e.values[0];
var currentName = e.values[2];
var currentScore1 = e.values[3];
MailApp.sendEmail(
currentEmail,
subjectline,
"HTML",
{ htmlBody: messageBody }
);
}
Note: In order to make the event object work, the function should be called by a Google Sheets form submit trigger.注意:为了使事件对象起作用,该函数应由 Google 表格表单提交触发器调用。
Related有关的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.