首页 > 解决方案 > 谷歌应用脚​​本触发器不发送电子邮件

问题描述

请有人可以在这里告诉我错误(我认为对于有任何编码知识的人来说这将是非常明显的)?我从各种来源拼凑了这个脚本,但它不起作用 - 逻辑是正确的,因为它适用于标准的 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); 
  }
 }
}   

标签: google-apps-script

解决方案


你有这两个声明:

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); 
    }
  }
}   

JavaScript getDay()

基于时间的事件对象

如果您仍然有问题,请提供 Mail Sheet 的图片。


推荐阅读