【问题标题】:MailApp.sendEmail function runs without errors but no emails are sentMailApp.sendEmail 函数运行没有错误,但没有发送电子邮件
【发布时间】:2020-04-21 15:26:15
【问题描述】:

以下脚本运行时没有任何明显错误,但未发送电子邮件。关于为什么的任何想法?

function sendEmailLoop() {

  var sheets =   SpreadsheetApp.getActiveSpreadsheet().getSheets();
  sheets.forEach(function(sheet) {
    var range = sheet.getDataRange();

    if (sheet.getName() == "Summary") //Disregard tab named 'Summary' 
    {      
    }
    else {    
      var range = sheet.getDataRange(); //to set the range as array
      var values = range.getDisplayValues(); //to get the value in the array
      var lastRow = range.getLastRow();
      var ss = SpreadsheetApp.getActiveSpreadsheet();  //declare the spreadsheet
      var sheet = ss.getSheetByName("Sheet1");
      var Title = values[0][0];         //[Title] cell A1
      var URL = values[0][1];           //[URL] cell B1
      var i;
      var logContent = '';

      for (i = 3; i < lastRow; i++) {   
        var Name = values[i][0];     //[Name] cell A++
        var Email = values[i][1];        // [Email] cell B++

        Logger.log('to: ' + Email);
        Logger.log('subject: ' + Name + Title + 'Test');
        Logger.log('message: ' + 'This is a test message for the training that can be found at ' + URL);
        /*
        MailApp.sendEmail({
          to: Email,
          subject: Name + Title + 'Test',
          message: 'This is a test message for the training that can be found at ' + URL});
        */
      }; //end for loop - email tab data
    };   // end 'else'
  }); // end function(sheet)       
} // end SendEmailLoop()  

这是成功执行的 Stackdriver 日志(成功意味着没有错误,但仍然没有发送电子邮件):

与脚本相关的电子表格的结构:

注意 - 此脚本的早期版本不包括 sheets.forEach() 方法调用(即循环遍历电子表格的每个选项卡)并且电子邮件发送正常。

没有发送或接收电子邮件是否与我有很多工作表并且此功能循环通过它们有关?

【问题讨论】:

  • 请注意您可能知道 - 小心 quotas 就像您所做的一样 (M - 1) * (N - 3) 每次运行发送(其中 M 是工作表数,N 是行数) ) 会很快失去控制。
  • 是的 - 感谢格式修复和配额说明。我的用例应该完全符合配额 - 不过请注意。
  • 只是一条信息(尽管在文档中相对模糊)来跟踪(尽管配额通常足以满足一般用例)。顺便说一句,另一个旁注(关于代码样式) - 请不要使用句子大小写的 var 名称(通常他们指定“类”[因为 JS 有它们]) - 在这里不相关,但以后可能会成为混淆的来源游戏

标签: email google-apps-script google-sheets


【解决方案1】:
function sendEmailLoop() {
  var exclA=['Summary'];
  var ss=SpreadsheetApp.getActive();
  var sheets=ss.getSheets();
  sheets.forEach(function(sheet) {
    if (exclA.indexOf(sheet.getName())==-1) {      
      var range=sheet.getDataRange();
      var values=range.getDisplayValues(); 
      var lastRow=range.getLastRow();
      var Title=values[0][0];
      var URL=values[0][1];  
      var logContent='';
      for (var i=3; i <values.length; i++) {   
        var Name=values[i][0];
        var Email=values[i][1];
        Logger.log('to: %s\nsubject: %s %s Test\nmessage: %s This is a test message for the training that can be found at %s',Email,Name,Title,URL);
        /*
        MailApp.sendEmail({
        to: Email,
        subject: Name + Title + 'Test',
        message: 'This is a test message for the training that can be found at ' + URL});
        */
      }
    }
  });
}

【讨论】:

    【解决方案2】:

    答案:

    您需要取消注释您的 MailApp 代码。

    代码更改:

    我测试了您的代码,它对我来说似乎运行没有问题,包括收到电子邮件,只是需要删除您的 MailApp 调用周围的代码 cmets(/**/) .

    如果.getDataRange() 获得看似空的行,我还建议在发送电子邮件之前添加一个条件行:

    if (Email == "") {
      continue;
    };
    MailApp.sendEmail({
      to: Email,
      subject: Name + Title + 'Test',
      message: 'This is a test message for the training that can be found at ' + URL});
    

    参考资料:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-14
      • 2021-06-19
      • 1970-01-01
      • 2020-01-08
      • 2013-08-22
      • 2013-10-10
      • 2016-10-10
      • 1970-01-01
      相关资源
      最近更新 更多