简体   繁体   English

仅向新的 google 表单提交发送电子邮件

[英]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.

相关问题 如何只向上次提交的谷歌表单发送电子邮件? - How to only send email to the last google form submission? 提交表单后发送电子邮件 - send email after form submission 提交Google表单电子邮件通知 - Google Form Email Notification On Submission 从表单提交创建并通过电子邮件发送 google doc - Create and email google doc from form submission 将表单发送至提交时的一封电子邮件,但发送至不同提交时的另一电子邮件? - Send a Form to one email on submit but to a different email on a different submission? 通过电子邮件服务(例如Mailchimp)提交表单后发送电子邮件 - Send Email after form submission via email service such as Mailchimp 如何使用Django表单发送有关表单提交的电子邮件? - How to send email on form submission using django forms? 如何在不提交任何表单的情况下在 php 中发送电子邮件? - How to Send email in php without any submission of form? 如何通过手机发送消息以及在提交联系表时通过电子邮件发送给客户 - How to send messages in mobile as well as email on contact form submission for the client 如何在表单提交时发送电子邮件并在数据库中保存数据 - How to send email while form submission with saving data in database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM