我想做一个谷歌表单向提交者发送电子邮件,我使用这个代码指令使用应用程序脚本方法如下...表单(提交)>工作表(响应)>应用程序脚本>获取文档文件作为电子邮件模板 > 发送电子邮件
我的问题是提交应用会发送多封重复的电子邮件。不管我做什么 这是从这里获取脚本的应用程序
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()))
答案 0 :(得分:0)
正如 Cooper 在评论中提到的,您可能安装了 1 个以上的触发器。
您可以检查项目的触发器页面以查看重复项,或运行此行。
querying policy failed: No such file or directory (2)
只需删除重复的,只保留其中的 1 个,这样您的邮件就不会再发送重复的邮件了。