google-apps-script - 谷歌应用脚本触发器不发送电子邮件
问题描述
请有人可以在这里告诉我错误(我认为对于有任何编码知识的人来说这将是非常明显的)?我从各种来源拼凑了这个脚本,但它不起作用 - 逻辑是正确的,因为它适用于标准的 Google 触发器(我认为我不能使用它,因为我只想在周一至周五的办公时间发送电子邮件) . 提前致谢;
`enter code here`function startCustomTrigger()
{
ScriptApp.newTrigger('StartProcess').timeBased().everyHours(1).create();
}
function StartProcess() {
var date = new Date();
var day = date.getDay();
var hrs = date.getHours();
if ((day >= 2) && (day <= 6) && (hrs >= 8) && (hrs <= 18)) {
// Get the sheet where the data is, in sheet 'Mail'
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Mail")
var startRow = 2; // First row of data to process since there is a header row
var numRows = sheet.getRange(1,5).getValue(); // Number of rows to process is set by a formula which
counts rows
// Fetch the range of cells A2:B6 where the emails and messages are
var dataRange = sheet.getRange(startRow, 1, numRows, 2)
// Fetch values for each row in the Range to input into the mailing system
var data = dataRange.getValues();
// This processes the emails you want to send
for (i in data) {
var row = data[i];
var emailAddress = row[0]; // First column is the email address
var message = row[1]; // Second column is the message
var subject = row[1]; // This is the subject of the email
// This parses the data for the email to send
MailApp.sendEmail(emailAddress, subject, message);
}
}
}
解决方案
你有这两个声明:
var message = row[1]; // Second column is the message
var subject = row[1]; // This is the subject of the email
一个一定是不正确的
试试这种方式:
function StartProcess(e) {
var day = e['day-of-week'];//available from event object but a little different than that which comes from Date().getDay()
var hrs = e.hour;//available from event object
if ((day >= 1) && (day <= 5) && (hrs >= 8) && (hrs <= 18)) {
const ss=SpreadsheetApp.getActive();
// Get the sheet where the data is, in sheet 'Mail'
var sheet=ss.getSheetByName("Mail")
var startRow=2; // First row of data to process since there is a header row
var numRows=sheet.getRange(1,5).getValue(); // Number of rows to process is set by a formula which counts rows
// Fetch the range of cells A2:B6 where the emails and messages are
var dataRange = sheet.getRange(startRow, 1, numRows, 2)
// Fetch values for each row in the Range to input into the mailing system
var data = dataRange.getValues();
// This processes the emails you want to send
for (var i=0;i<data.length;i++) {
var row = data[i];
var emailAddress = row[0]; // First column is the email address
var message = row[1]; // Second column is the message
var subject = row[2]; // This is the subject of the email
// This parses the data for the email to send
MailApp.sendEmail(emailAddress, subject, message);
}
}
}
如果您仍然有问题,请提供 Mail Sheet 的图片。
推荐阅读
- python - 如何执行二维神经网络?
- javascript - ReferenceError:未定义响应。我不明白
- android - Android:如何进行多模块匕首组件注入?
- r - 将两列组合在一起并将其原始列名保留为单独的列
- python - Dash 应用程序没有错误,但没有绘图
- java - 如何在 JavaFX FXML 的窗格中编辑文本/图像/按钮
- wordpress - 显示 ACF 组中 Post Object 字段的数据
- elasticsearch - Elasticsearch 和 CAP 定理
- vbscript - vbscript:替换搜索字符串后的所有内联文本
- c++ - 无法从媒体会话中检索 IMFAudioPolicy