我有一个电子表格,它从onEdit触发器中获取数据,并将其插入到新的电子表格中。现在,我试图让这个新的电子表格在基于某些值从另一个电子表格插入数据时发送电子邮件。
我尝试了很多不同的组合,希望能有所作为,但是没有运气。我的项目触发错误率下降了,因此该功能正常运行。它只是没有填充电子邮件并发送数据。我不确定If语句是否错误或发生了什么。
function sendEmail(){
var recipientList = "EMAIL";
var emailSubject = "Great Test Email";
var senderName = "EmailSender";
var s = SpreadsheetApp.getActiveSheet();
var emailTable = "<table> <tr><th>|| Control ID ||</th> <th>Significance ||</th> <th>Control Owner ||</th> <th> Control Certifier ||</th> <th>Next IA Testing Date ||</th> <th>Most Recent IA Test Results ||</th> <th> Test Date ||</th> <th>Test Results ||</th> <th>IA Control Conclusion ||</th> </tr>";
var subject = 'UPDATE on: '+ s.getSheetName();
var i = 2;
var lastRow = s.getLastRow();
var controlID = s.getRange(i, 2).getValue();
var significance = s.getRange(i, 3).getValue();
var controlOwner = s.getRange(i, 4).getValue();
var controlCertifier = s.getRange(i, 5).getValue();
var nextIATestingStartDate = Utilities.formatDate(s.getRange(i, 6).getValue(), "America/Los_Angeles", "MMM-dd-yyyy"); // Format that date/timestamp
var mostRecentIATestResultsConclusion = s.getRange(i, 7).getValue();
var TestDate = s.getRange(i, 8).getValue();
var TestResults = s.getRange(i, 9).getValue();
var IAControlConclusion = s.getRange(i, 10).getValue();
emailTable += "<tr><td>" + controlID + "</td> <td>" + significance + "</td> <td>" + controlOwner + "</td> <td>" + controlCertifier + "</td> <td>" + nextIATestingStartDate + "</td> <td>" + mostRecentIATestResultsConclusion + "</td> <td>" + TestDate + "</td> <td>" + TestResults + "</td> <td>" + IAControlConclusion + "</td> </tr>";
emailTable += "</table>";
if (controlCertifier === 'FILTER') {
MailApp.sendEmail({
to: recipientList,
subject: subject,
htmlBody: emailTable
});
}
}
答案 0 :(得分:0)
这是我找到的解决方案。我了解了循环,然后不再使用onEdit发送电子邮件,而是让它成为每天的发件人(可以根据需要设置其发送频率),并根据记录的时间戳进行设置。因此,如果该行是在最近214小时内插入的,则对其进行记录并发送电子邮件。这将全部编译,而不是一次发送一次。我觉得这比我最初发布的问题更好。
不好意思,我还在学习!感谢大家的帮助。
function dailyEmail() {
var s = SpreadsheetApp.getActiveSheet();
var recipientList = "EMAIL";
var digestFrequency = 1.0;
var emailSubject = 'Changes were made to: '+ s.getSheetName();
var senderName = "Email Sender";
var lastRow = s.getLastRow();
var i = 2;
var date = new Date();
var entriesCounter = 1;
var emailTable = '<table> <tr><th style = "width: 200px" bgcolor = "#aed1e8"> Control ID </th> <th style = "width: 200px" bgcolor = "#aed1e8">Significance </th> <th style = "width: 500px" bgcolor = "#aed1e8">Control Description </th> <th style = "width: 200px" bgcolor = "#aed1e8">Control Owner </th><th style = "width: 200px" bgcolor = "#aed1e8"> Control Certifier </th> <th style = "width: 200px" bgcolor = "#aed1e8">Next IA Testing Date </th> <th style = "width: 200px" bgcolor = "#aed1e8">Most Recent IA Test Results Conclusion</th> <th style = "width: 200px" bgcolor = "#aed1e8"> Test Date </th> <th style = "width: 200px" bgcolor = "#aed1e8">Test Results </th> <th style = "width: 200px" bgcolor = "#aed1e8">IA Control Conclusion </th><th style = "width: 200px" bgcolor = "#aed1e8">Column Updated </th> </tr>';
for (var i; i <= lastRow; i++) {
var iaTestDate = new Date( s.getRange(i, 7).getValue());
var rowDate = new Date(s.getRange(i, 1).getValue()); // your date from API
//Time is standard set to milliseconds
var t1 = date.getTime(),
t2 = rowDate.getTime();
//Math to find the difference in days from the current date to the date that each row was edited or changed
var diffInDays = Math.floor((t1-t2)/(24*3600*1000));// 24*3600*1000 is milliseconds in a day
if (diffInDays <= digestFrequency) {
entriesCounter++;
var controlID = s.getRange(i, 2).getValue();
var significance = s.getRange(i, 3).getValue();
var controlName = s.getRange(i, 4).getValue();
var controlOwner = s.getRange(i, 5).getValue();
var controlCertifier = s.getRange(i, 6).getValue();
var nextIATestingStartDate = Utilities.formatDate(iaTestDate, "America/Los_Angeles", "MMM-dd-yyyy"); // Format that date/timestamp
var mostRecentIATestResultsConclusion = s.getRange(i, 8).getValue();
var TestDate = s.getRange(i, 9).getValue();
var TestResults = s.getRange(i, 10).getValue();
var IAControlConclusion = s.getRange(i, 11).getValue();
var headerColumn = s.getRange(i, 12).getValue();
//builds the table data for the email
emailTable += "<tr><td style = 'width: 200px' bgcolor = '#F8F8F8'>" + controlID + "</td> <td style = 'width: 200px' bgcolor = '#F8F8F8'>" + significance + "</td> <td style = 'width: 500px' bgcolor = '#F8F8F8'>" + controlName + "</td> <td style = 'width: 200px' bgcolor = '#F8F8F8'>" + controlOwner + "</td> <td style = 'width: 200px' bgcolor = '#F8F8F8'>" + controlCertifier + "</td> <td style = 'width: 200px' bgcolor = '#F8F8F8'>" + nextIATestingStartDate + "</td> <td style = 'width: 200px' bgcolor = '#F8F8F8'>" + mostRecentIATestResultsConclusion + "</td> <td style = 'width: 200px' bgcolor = '#F8F8F8'>" + TestDate + "</td> <td style = 'width: 200px' bgcolor = '#F8F8F8'>" + TestResults + "</td> <td style = 'width: 200px' bgcolor = '#F8F8F8'>" + IAControlConclusion + "</td><td style = 'width: 200px' bgcolor = '#F8F8F8'>" + headerColumn + "</td> </tr>";
}
}
emailTable += "</table>";
if (entriesCounter == 1) {
emailTable = "<b>No changes were made since the previous report.</b>";
}
var htmlBody = "Hello, <br><br> This is an automated email sent between 6-7am. <br> <br> The Following changes have been made within the past 24 hours.<br><br>" + emailTable + '<br><br> <a href = "URL"><b>Visit the Google Sheet for this e-mail here</b></a>';
MailApp.sendEmail(recipientList, emailSubject, '', {
to: recipientList,
name: senderName,
htmlBody: htmlBody,
cc: recipientList
});
}