【发布时间】:2021-10-03 14:37:21
【问题描述】:
我想做一个谷歌表单向提交者发送电子邮件,我使用此代码指令使用应用脚本方法如下...表单(提交)>工作表(响应)>应用脚本>获取文档文件作为电子邮件模板 > 发送电子邮件
我的问题是提交应用程序会重复发送多封电子邮件。不管我做什么 这是脚本的应用程序是从这里获取的
var EMAIL_TEMPLATE_DOC_URL = 'https://docs.google.com/document/d/1M2n7iOkl4IeKmuTjU95ibQN6W6UqYtkA9d99fccPd_Y/edit?usp=sharing';
var EMAIL_SUBJECT = 'title';
/**
* Installs a trigger on the Spreadsheet for when a Form response is submitted.
*/
function installTrigger() {
ScriptApp.newTrigger('onFormSubmit')
.forSpreadsheet(SpreadsheetApp.getActive())
.onFormSubmit()
.create();
}
/**
* Sends a customized email for every response on a form.
*
* @param {Object} e - Form submit event
*/
function onFormSubmit(e) {
var responses = e.namedValues;
// If the question title is a label, it can be accessed as an object field.
// If it has spaces or other characters, it can be accessed as a dictionary.
var timestamp = responses.Timestamp[0];
var email = responses['Email Address'][0].trim();
var name = responses.Name[0].trim();
// If there is at least one topic selected, send an email to the recipient.
var status = 'Email Sent';
// Append the status on the spreadsheet to the responses' row.
var sheet = SpreadsheetApp.getActiveSheet();
var row = sheet.getActiveRange().getRow();
var column = e.values.length + 1;
sheet.getRange(row, column).setValue(status);
MailApp.sendEmail({
to: email,
subject: EMAIL_SUBJECT,
htmlBody: createEmailBody(name),
});
Logger.log('status=' + status + '; responses=' + JSON.stringify(responses));
}
/**
* Creates email body and includes the links based on topic.
*
* @param {string} name - The recipient's name.
* @param {string[]} topics - List of topics to include in the email body.
* @return {string} - The email body as an HTML string.
*/
function createEmailBody(name) {
// Make sure to update the emailTemplateDocId at the top.
var docId = DocumentApp.openByUrl(EMAIL_TEMPLATE_DOC_URL).getId();
var emailBody = docToHtml(docId);
emailBody = emailBody.replace(/{{NAME}}/g, name);
return emailBody;
}
/**
* Downloads a Google Doc as an HTML string.
*
* @param {string} docId - The ID of a Google Doc to fetch content from.
* @return {string} The Google Doc rendered as an HTML string.
*/
function docToHtml(docId) {
// Downloads a Google Doc as an HTML string.
var url = 'https://docs.google.com/feeds/download/documents/export/Export?id=' +
docId + '&exportFormat=html';
var param = {
method: 'get',
headers: {'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()},
muteHttpExceptions: true,
};
return UrlFetchApp.fetch(url, param).getContentText();
}
我试图这样做。在发送电子邮件之前检查最后一列是否为空。发送后,Email Sent 将失败,我的事情是限制电子邮件重复。但没有。
if(sheet.getRange(row, column).getValue() == "")
{
sheet.getRange(row, column).setValue(status);
MailApp.sendEmail({
to: email,
subject: EMAIL_SUBJECT,
htmlBody: createEmailBody(name),
});
}
我试着统计一下它发送了多少封电子邮件。最后一个单元格显示 0,它发送了重复的电子邮件。
if(sheet.getRange(row, column).getValue() == "")
{
sheet.getRange(row, column).setValue(0);
}
else
{
sheet.getRange(row, column).setValue(sheet.getRange(row, column).getValue + 1);
MailApp.sendEmail({
to: email,
subject: EMAIL_SUBJECT,
htmlBody: createEmailBody(name),
});
}
编辑
使用后...
ScriptApp.getProjectTriggers().forEach(trigger => Logger.log(trigger.getHandlerFunction() + ' - ' + trigger.getEventType()))
【问题讨论】:
-
检查您的触发器页面,确保您只分配了一个触发器。
标签: forms email google-apps-script google-sheets