[英]Google Form. Confirmation Email Script. With edit url
I have two working trigger functions in Google Script that fire when a form response spreadsheet gets a new submission. 我在Google脚本中有两个工作触发功能,当表单响应电子表格获取新的提交时会触发。 One inserts the "edit your submission" url into the spreadsheet.
一个将“编辑您的提交” URL插入电子表格。 The other looks up the response's email and sends them a confirmation.
另一个查询响应的电子邮件,并向他们发送确认。
What I'm having a hard time understanding is how to populate the url first , and then send an email containing that url. 就是我有一个很难理解的是如何先填充URL, 然后发送包含该URL的电子邮件。
( Google Script is different to debug than js in the browser :\\ ) ( Google脚本与浏览器中的js调试不同:\\ )
Setup triggers 设置触发器
function Initialize() {
var triggers = ScriptApp.getScriptTriggers();
for (var i in triggers) {
ScriptApp.deleteTrigger(triggers[i]);
}
ScriptApp.newTrigger("SendConfirmationMail")
.forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
.onFormSubmit()
.create();
assignEditUrls();
}
On form submit, searches for column titled "Clients Email", and sends a formatted email to them. 在提交表单时,搜索标题为“客户电子邮件”的列,然后向他们发送格式化的电子邮件。
function SendConfirmationMail(e) {
try {
var ss, cc, sendername, subject, columns;
var message, value, textbody, sender;
var url;
// This is your email address and you will be in the CC
cc = Session.getActiveUser().getEmail();
// This will show up as the sender's name
sendername = "XXXX";
// Optional but change the following variable
// to have a custom subject for Google Docs emails
subject = "Form Complete: Mobile App - Client Questionnaire";
// This is the body of the auto-reply
message = "Confirmation text here.<br><br>Thanks!<br><br>";
ss = SpreadsheetApp.getActiveSheet();
columns = ss.getRange(1, 1, 1, ss.getLastColumn()).getValues()[0];
// This is the submitter's email address
sender = e.namedValues["Clients Email"].toString();
// Only include form values that are not blank
for ( var keys in columns ) {
var key = columns[keys];
if ( e.namedValues[key] ) {
message += key + ' :: '+ e.namedValues[key] + "<br />";
}
}
textbody = message.replace("<br>", "\n\n");
GmailApp.sendEmail(sender, subject, textbody,
{cc: cc, name: sendername, htmlBody: message});
} catch (e) {
Logger.log(e.toString());
}
}
Separate function. 分开的功能。 Looks up form and applies the edit URL to the 26th column.
查找表单并将编辑URL应用于第26列。
function assignEditUrls() {
var form = FormApp.openById('10BVYipGhDa_AthabHE-xxxxxx-hg');
//enter form ID here
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form Responses');
//Change the sheet name as appropriate
var data = sheet.getDataRange().getValues();
var urlCol = 26; // column number where URL's should be populated; A = 1, B = 2 etc
var responses = form.getResponses();
var timestamps = [], urls = [], resultUrls = [];
for (var i = 0; i < responses.length; i++) {
timestamps.push(responses[i].getTimestamp().setMilliseconds(0));
urls.push(responses[i].getEditResponseUrl());
}
for (var j = 1; j < data.length; j++) {
resultUrls.push([data[j][0]?urls[timestamps.indexOf(data[j][0].setMilliseconds(0))]:'']);
}
sheet.getRange(2, urlCol, resultUrls.length).setValues(resultUrls);
}
Programmers are provided no direct control over the order that trigger functions will be invoked, when they are dependent on the same event. 当程序员依赖于同一事件时,不会直接控制触发函数的调用顺序。
In your situation though, there are a couple of options available. 不过,根据您的情况,有几个可用的选项。
Use only one trigger function for the event, and have it invoke the current functions in the order you require. 仅对事件使用一个触发函数,并使其按所需顺序调用当前函数。 (Alternatively, set
assignEditUrls()
up as the only trigger function, and have it call SendConfirmationMail()
after completing its task. (或者,将
assignEditUrls()
设置为唯一的触发函数,并在完成任务后调用SendConfirmationMail()
。
Have SendConfirmationMail()
check for the availability of the Edit URL, and call Utilities.sleep()
if the URL isn't ready yet. 让
SendConfirmationMail()
检查Edit URL的可用性,如果URL还没有准备好,请调用Utilities.sleep()
。 (In a loop, until ready.) (循环执行,直到准备好。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.