0

如何告訴單元格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 
     } 
     } 
} 
+0

一個潛在的更好的方式來做到這一點放在帶有參數的功能,您在傳遞,然後調用它的其他功能,所有的sendmail的信息。現在你要讓計算機完成兩倍的工作量,從而減慢你的腳本運行速度。 – Kevrone 2013-05-14 23:32:01

回答

0

這應該工作或至少給你的想法,因爲我做的事情非常相似。我通常設置觸發這個被onEdit()

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(); 
     //Check to see if column is A or B to trigger email 
     if (cellcol == 1 || cellcol ==2) 
     { 
     //check for row to trigger email 
     if (cellrow ==2) 
     { 
     //Find cell and set date 
     var celldate = sheet.getRange(2, 3); 
     celldate.setValue(new Date()); 
    //end set date 
     var cellvalue = mycell.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); 
    } 
    } 
    } 
+0

這看起來完美的,但是當我把它調整到我想要的列並做從腳本編輯器試運行,我得到一個錯誤的引用錯誤在這條線 如果(cell.indexOf(「d」)!= - 1 ){ 時間戳部分工作正常。 – jredeker 2013-05-14 15:35:45

+0

我沒有看到indexOF作爲範圍類的一部分。你應該使用cellvalue而不是cell是我看到的第一個東西。 cellvalue.indexOf(「G」)!= -1,將檢查字符串中的字母「G」,看它是否存在。要以做INDEXOF澄清你需要有,我們有一個字符串。我們設定cellvalue =早些時候正在編輯的任何單元格(如將確保它是A或B第2行)的F mycell.getValues.toString – Kevrone 2013-05-14 23:11:50

相關問題