Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script sendEmail脚本有时只发送邮件_Google Apps Script_Google Sheets - Fatal编程技术网

Google apps script sendEmail脚本有时只发送邮件

Google apps script sendEmail脚本有时只发送邮件,google-apps-script,google-sheets,Google Apps Script,Google Sheets,有时我可以让它在时间改变时自动发送,有时脚本运行但不发送电子邮件。如果第O行中的一个单元格显示“TRUE”(来自公式),但它不起作用,我可以手动键入“TRUE”,它将突然在所有值为“TRUE”的单元格上起作用,即使其他单元格中有公式填充的“TRUE”。它看起来很随机,我不知道什么有效,什么无效。它已经连续好几天完美了,然后突然停止了。我不知道 我使用基于时间的触发器。因为我希望它在特定的时间发送,所以我使用30分钟计时器(这足够近了)。我引用的单元格只有在x行中的单元格没有=“EMAIL\u S

有时我可以让它在时间改变时自动发送,有时脚本运行但不发送电子邮件。如果第O行中的一个单元格显示“TRUE”(来自公式),但它不起作用,我可以手动键入“TRUE”,它将突然在所有值为“TRUE”的单元格上起作用,即使其他单元格中有公式填充的“TRUE”。它看起来很随机,我不知道什么有效,什么无效。它已经连续好几天完美了,然后突然停止了。我不知道

我使用基于时间的触发器。因为我希望它在特定的时间发送,所以我使用30分钟计时器(这足够近了)。我引用的单元格只有在x行中的单元格没有=“EMAIL\u SENT”时才变为真,我想这是一种奇怪的接近方式。但它仍然有效,因为如果它确实发送了电子邮件,它会变成“FALSE”。这个公式非常有效。我的问题是让它在值=TRUE时始终发送

我的剧本:

function sendEmails(e) {
  var spreadsheet = SpreadsheetApp.openById("some id")
  var sheet = SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
  var startRow = 1;

  var range = sheet.getDataRange();
  var values = range.getDisplayValues();

  var lastrow = sheet.getLastRow();
  var lastcolumn = sheet.getLastColumn();

  var dataRange = sheet.getRange(startRow, 1, lastrow, lastcolumn);
  var data = dataRange.getValues();

  var EMAIL_SENT = 'EMAIL_SENT';
  var T = 'TRUE'

  var i = 1;
  for (i = 1; i < data.length; i++) {
    var row = data[i];
    var emailAddress = row[1];
    var msgHtml = '<HTML><BODY>' + 'Hello again,<p>This is a reminder that you are scheduled to participate in our paid driving study! The upcoming driving session that you are scheduled for is<b> ' + values[i][3] + ' </b>at<b> ' + values[i][4] + ' </b>at ' + 'ADDRESS' + '. The session will last approximately 4.5 hours, but be prepared for an additional 60 minutes of time required. Please plan to stay for the entire session.' + '<b>You are required to bring proof of insurance and your valid drivers license with you to your session.</b></p>' + '<font color="blue"><mark>Please respond to this email as soon as possible to confirm your attendance at this session.</mark></font>' + ' If you must cancel or reschedule your session, we require at least 24 hours advanced notice. Failure to give 24 hours advanced notice will result in a suspension from further participation. Your participation is very valuable to this study, and each driving session is crucial to our research. </p><p> Please be aware that cancellations may be made by our team for extraneous circumstances(e.g., inclement weather, construction,etc). If any of these circumstances do occur, we will let you know as soon as possible. We will do our best to rescheule you, but we cannot make any guarantees. If you have any questions, please let me know.</p>' + '<font color="red"><b>Please refer to the specific instructions provided in the scheduling email (i.e., "scheduled for L2 Driving Sessions") AND review these reminders:</b></font>' + '<p>1. Get plenty of rest and receive a healthy amount of sleep the night before your driving session<br>2. Do not drink alcohol or use any substance that may impair your driving the night before and morning of your driving session <br>3. Wash your hair within 12 hours before your session (i.e., if your session is at 1pm, you should wash your hair after 1am the previous night and before your 1pm session) <br>4. Do not put on makeup or any heavy skin products <br>5. Wear a loose-fitting shirt <br>6. Leave early so that you have enough time to find parking, go to the bathroom, etc. before your session starts</br></p>' + 'See you on<b> ' + values[i][3] + '</b>!' + '</BODY></HTML>';
    var subject = 'Driving Study ' + values[i][3] + ' at ' + values[i][4];

    if (values[i][14] == T) { 
      MailApp.sendEmail({ to: emailAddress, subject: subject, htmlBody: msgHtml});
      sheet.getRange(startRow + i, 8).setValue(EMAIL_SENT);
      SpreadsheetApp.flush();
    }  
  }
}

您没有提到如何运行此脚本,但听起来好像您已经为它安装了编辑触发器。考虑删除编辑触发器,然后重新添加它。请注意,这样的触发器仅在人工修改图纸文件时执行,而不是在公式更改时执行。这是故意的,不会改变

。如果您需要在没有用户交互的情况下发送电子邮件,您可能会发现基于时间的触发器更有用。请注意,您的脚本当前未使用电子邮件发送后在每行中设置的标志-您可能希望在查看该行时检查该标志,例如

if (values[i][7] == EMAIL_SENT)
  continue;

对不起,我忘了提到我使用基于时间的触发器。因为我希望它在特定的时间发送,所以我使用30分钟计时器(这足够近了)。我引用的单元格只会变为真,我想x行中的单元格不会=“EMAIL\u SENT”,我想这是一种奇怪的处理方式。但它仍然有效,因为如果它确实发送了电子邮件,它会变成“FALSE”。这个公式非常有效。我的问题是让它在值=trueThank时始终发送谢谢,我更新了我的问题以包含该信息。当您检查Stackdriver日志记录时,是否看到任何错误消息?(您可以调整日期范围以覆盖一个月左右)脚本运行时,我收到错误“发送电子邮件失败”。这是有道理的,因为它会遍历整个电子表格,当它到达第一行时没有数据就无法发送电子邮件。到目前为止,我没有收到任何错误。我将把执行转录本添加到原来的问题——它“完成”,但它没有到达实际上发送EMAIL的if语句。考虑添加一行来记录您正在检查的值:<代码>控制台。log({消息:)值[]值[14 ]值,V14:Value.map(函数(行){返回行(14);})};<开始
for
循环之前,请先执行code>。这会将数组发送到Stackdriver,以便您可以验证触发器是否正在运行,以及值是否符合您的预期。这些值是我第一次预期的值,但当我添加更多行并再次触发时,这些值都是FALSE,即使存在TRUE。所以电子邮件没有发送。我不明白发生了什么。基于更新时间的触发器不起作用,但在更改和编辑时就可以了。我以前使用过基于时间的触发器,所以我不知道发生了什么变化。
if (values[i][7] == EMAIL_SENT)
  continue;