如何告訴單元格C2設置單元格A2或B2已更新的日期? 此外,如何在A2或B2更新時觸發發送電子郵件功能?編輯特定範圍時更新日期或發送電子郵件
我的問題是onEdit在任何時候都會觸發文檔被編輯,但我只想要一個動作,如果一個特定的範圍被編輯。
對於郵件的發送,我發現和編輯的腳本幾乎工作,但它發出每當文檔中的任意單元格發生變化,而不是僅僅在G列改變我的電子郵件通知。有什麼建議麼?
function sendNotification() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var cell = ss.getActiveCell().getA1Notation();
var row = sheet.getActiveRange().getRow();
var cellvalue = ss.getActiveCell().getValue().toString();
var recipients = "[email protected]";
var message = 'Cell value has been changed';
if(cell.indexOf('G')!=-1){
message = sheet.getRange('D'+ sheet.getActiveCell().getRowIndex()).getValue()
}
var subject = 'Update to '+sheet.getName();
var body = sheet.getName() + ' has been updated. Visit ' + ss.getUrl() + ' to view the changes on row: «' + row + '». New comment: «' + cellvalue + '». For message: «' + message + '»';
MailApp.sendEmail(recipients, subject, body);
};
這是否與我的onEdit()函數關閉?
對於任何需要最終腳本
我結束了分裂這件事在兩個單獨的函數。這是完成的腳本。
第一個是電子郵件通知
/* This function send an email when a specified range is edited
* The spreadsheets triggers must be set to onEdit for the function
*/
function sendNotification() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
//Get Active cell
var mycell = ss.getActiveSelection();
var cellcol = mycell.getColumn();
var cellrow = mycell.getRow();
//Define Notification Details
var recipients = ENTEREMAILHERE;
var subject = "Update to "+ss.getName();
var body = ss.getName() + "has been updated. Visit " + ss.getUrl() + " to view the changes.";
//Check to see if column is A or B to trigger
if (cellcol == EDITMECOLUMN)
{
//check for row to trigger
if (cellrow == EDITMEROW)
{
//Send the Email
MailApp.sendEmail(recipients, subject, body);
}
//End sendNotification
}
}
這裏是一個時間戳
/* This function saves the date in a cell
* every time a specific row or column is edited
* The spreadsheets triggers must be set to onEdit for the function
*/
function setDate() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
//Get Active cell
var mycell = ss.getActiveSelection();
var cellcol = mycell.getColumn();
var cellrow = mycell.getRow();
//Check to see if column is A or B to trigger
if (cellcol == EDITMECOLUMN)
{
//check for row to trigger
if (cellrow == EDITMEROW)
{
//Find cell and set date in a defined cell
var celldate = sheet.getRange(EDITMEROW, EDITMECOLUMN);
celldate.setValue(new Date());
//end set date
}
}
}
一個潛在的更好的方式來做到這一點放在帶有參數的功能,您在傳遞,然後調用它的其他功能,所有的sendmail的信息。現在你要讓計算機完成兩倍的工作量,從而減慢你的腳本運行速度。 – Kevrone 2013-05-14 23:32:01