有时候,我可以让它在时间变化时自动发送,有时脚本会运行,但不会发送电子邮件。如果行O中的某个单元格显示“ TRUE”(根据公式),但它不起作用,我可以手动键入“ TRUE”,它将突然对所有值为“ TRUE”的单元格起作用,即使其他单元格也是如此由公式填充“ TRUE”。似乎太随意了,我不知道什么有效,什么无效。连续好几天都是完美的,然后突然停了下来。我不知道。
我使用基于时间的触发器。因为我希望它在特定时间发送,所以我使用30分钟计时器(足够接近)。仅当x行中的单元格不等于“ EMAIL_SENT”时,我引用的单元格才变为true,我认为这是接近它的一种奇怪方法。但是它可以工作,因为如果确实发送了电子邮件,它将变成“ 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语句。
[18-08-28 10:59:45:401 PDT] Starting execution
[18-08-28 10:59:45:510 PDT] SpreadsheetApp.openById([10RCcnc22RYRaf3wQhMzPp_q3f1_-LBsm2tQjYrd0h1s]) [0.099 seconds]
[18-08-28 10:59:45:511 PDT] Spreadsheet.getSheets() [0 seconds]
[18-08-28 10:59:45:512 PDT] SpreadsheetApp.setActiveSheet([Sheet]) [0 seconds]
[18-08-28 10:59:45:883 PDT] Sheet.getDataRange() [0.37 seconds]
[18-08-28 10:59:46:096 PDT] Range.getDisplayValues() [0.212 seconds]
[18-08-28 10:59:46:188 PDT] Sheet.getLastRow() [0.088 seconds]
[18-08-28 10:59:46:373 PDT] Sheet.getLastColumn() [0.184 seconds]
[18-08-28 10:59:46:374 PDT] Sheet.getRange([1, 1, 447, 15]) [0 seconds]
[18-08-28 10:59:46:692 PDT] Range.getValues() [0.317 seconds]
[18-08-28 10:59:46:708 PDT] Execution succeeded [1.294 seconds total runtime]
答案 0 :(得分:0)
您没有提到如何运行此脚本,但是听起来您已经为其安装了编辑触发器。考虑删除编辑触发器,然后重新添加。请注意,此类触发器仅在人员修改表格文件时执行,而不是在公式更改时执行。这是设计使然,不会改变。
Review more about Google Apps Script & triggers in the documentation。如果您需要在没有用户交互的情况下发送电子邮件,则基于时间的触发器可能会更有用。请注意,您的脚本当前未使用发送电子邮件后每行中设置的标志-您可能希望在查看该行时进行检查,例如
if (values[i][7] == EMAIL_SENT)
continue;