【发布时间】:2019-10-19 05:44:09
【问题描述】:
我目前正在编写一个脚本,以便在根据单元格的特定值更新单元格时自动发送电子邮件。现在,我想获取通过电子邮件发送的相同值并将它们填充到新的电子表格中,并发布该行更新时间的时间戳。在新的 SS 中,它将是 A(时间戳)、B、C、D、E(B-E 将是自动通过电子邮件发送的数据)。
在另一个用户的帮助下,我能够让自动电子邮件系统正常工作。我发现几个帖子询问了类似的问题,我试图拼凑起来看看我是否可以让这个工作,但我没有成功。我一直在代码中遇到块,因为我不知道 google-apps-scripts 的所有正确语法。我尝试了 copyTo()、duplicateSheet()。但问题是我只关心一次更新一行,因为特定单元格在行上得到更新。我发现的只是有关将整张数据复制到新电子表格中的帖子。我以为我必须在上面定义它,然后在 if 语句中添加 copyTo() 但每次我尝试它时,if 语句都会中断并且会在代码中收到错误消息。
(我在项目触发器中也有一个onEdit触发器)
function sendNotification(e){
var ss = e.source.getSheetByName('Datasheet'); //defines the source of
where to look
var cell = e.range.getA1Notation();
var row = e.range.getRow(); //from the range gets the row(is important
for calling the control owner and control ID on line 15 and 16)
var col = e.range.getColumn(); //from the range gets the
column(important when pulling specific columns of information)
var cellvalue = e.range.getValue(); //this pulls whatever is inside of
the cell. (so 'NAME' in column i)
var subject = 'SUBJECT: '+ ss.getSheetName(); //tells the program what
to put in the subject line of the email (ss.getSheetName() gets the name
of the tab of the data)
var name = ss.getRange(row, 9).getValue(); //get column 9 for current
row (column 9 is column i which is the control certifier)
if (name === 'NAME' && (col === 23 || col === 24 || col === 31 || col
=== 32) === true) { //states: if the cell in column i = TRUE, AND column
(w)23, (x)24, (ae)31 OR (af)32 = changed/updated THEN execute command
below
var control = ss.getRange(row, 2).getValue(); //get value for column B
in updated cell row
var owner = ss.getRange(row, 8).getValue(); //get value for column H
in updated cell row
//line 21-35 is the email formatting
//MailApp.sendEmail() sends the email if line 14 = TRUE
// to: who the email is sent too
//Subject = subject defined above
//htmlBody = what is in the body of the paragraph sent
MailApp.sendEmail({
to: "EMAIL",
subject: subject,
htmlBody: "The following cell has been updated: <br><br>"+
"<br><br>" + "The control is: " + control +
", <br><br>The owner is: " + owner +
"<br><br>The change was: " + cellvalue + "<br>" +
"<br><br>Thank you. <br><br><br><br>" +
})
}
}
基本上,如果 W、X、AE 或 AF 列得到更新,我会喜欢它。除了目前所做的事情之外,还向我发送了一封包含信息的电子邮件。还要将所有这些信息更新到一个完全不同的电子表格的新行中,并带有编辑时间的时间戳,以便我可以记录它。
【问题讨论】:
标签: javascript google-apps-script google-sheets