[英]Using onEdit trigger on data that is uploaded from another spreadsheet
I have one spreadsheet that takes data from an onEdit trigger, and inserts it into a new spreadsheet. 我有一个电子表格从onEdit触发器获取数据,并将其插入到新的电子表格中。 Now, I am trying to get this new spreadsheet to send an email when that data is inserted from the other spreadsheet based on certain values.
现在,我正在尝试使用此新电子表格在基于特定值从其他电子表格插入数据时发送电子邮件。
I have tried a ton of different combinations hoping to get something to work, but to no luck. 我尝试了很多不同的组合,希望得到一些工作,但没有运气。 My project trigger error rate is going down, so the function is working.
我的项目触发错误率正在下降,因此该功能正在运行。 It just isn't populating an email and sending the data.
它只是没有填充电子邮件和发送数据。 I am not sure if my If statement is wrong or what is happening.
我不确定我的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
});
}
}
Here is the solution I found. 这是我找到的解决方案。 I learned about loops and then instead of using the onEdit to send an email I just made a daily sender(it could be as frequent as you want) and set it based on the time stamp that it's logged at.
我学习了循环,然后使用onEdit发送电子邮件而不是发送电子邮件我只是做了一个每日发送者(它可能是你想要的频繁)并根据它记录的时间戳设置它。 So if the row was inserted within the last 214 hours then log it and send the email.
因此,如果在最近的214小时内插入了行,则记录并发送电子邮件。 This compiles it all instead of sending it one at a time.
这会编译所有内容,而不是一次发送一个。 I felt like this was a better solution than the question I originally posted.
我觉得这比我最初发布的问题更好。
Excuse my poor set up I am still learning! 请原谅我的糟糕设置,我还在学习! Thanks for everyone's help.
谢谢大家的帮助。
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
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.