2017-02-20 26 views
2

爲了使我的一些工作自動化,我開始學習谷歌腳本的基礎知識。需要爲多個列和工作表發送通知

我有一個電子表格,我想在數據輸入到一列或另一列時發送電子郵件通知。這個電子表格中還有兩個標籤,我希望發生這種情況。

該腳本的當前結果是第二個「sendnotification」函數的電子郵件。

問題:我如何拿到劇本要考慮兩者的功能?

我知道這個代碼可以通過更好的方式使用IF函數來縮小,但是我很茫然。

對於某些情況:這用於製造操作。生產是由一家非現場公司完成的,當他們將數量輸入到第10欄中的任何一張時,我希望它會向我的團隊發送一封與我合作的電子郵件。同樣,當產品質量測試完成後,我希望能夠輸入到第12欄,並向離線公司發送電子郵件。

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 = "[email protected]"; 
    var subject = "Disc production was entered on the "+ss.getName(); 
    var body = ss.getName() + " has been updated with an amount produced. Visit " + ss.getUrl() + " to view the quantities entered."; 
//Check to see if column is A or B to trigger 
    if (cellcol == 10) 
    { 
//Send the Email 
    MailApp.sendEmail(recipients, subject, body); 
    } 
//End sendNotification 
} 
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 = "[email protected]"; 
    var subject = "A lot of disc has been RELEASED by XYZ Company"; 
    var body = ss.getName() + " has been updated with a lot of disc that were released by XYZ Company. Visit " + ss.getUrl() + " to view this updated information."; 
//Check to see if column is A or B to trigger 
    if (cellcol == 12) 
    { 
//Send the Email 
    MailApp.sendEmail(recipients, subject, body); 
    } 
//End sendNotification 
} 
+1

只是爲了澄清,你要自動發送的電子郵件,當你鍵入到第10列或列12?像'onedit'觸發器一樣? –

+0

正確的,當數據被輸入到其中任何一列的自動電子郵件。 –

回答

0

你需要建立一個onEditinstallable trigger(如果你還沒有的話),並將其分配給下面的功能:

function onEditEmailSender(e) { 
    var sheetName = e.range.getSheet().getName(); 

    if (sheetName === "tab1" || sheetName === "tab2") { 
    var col = e.range.getColumn(); 

    if (col === 10) 
     //send col 10 email 
    else if (col === 12) 
     //send col 12 email 
    } 
} 

onEdit觸發通過與各種參數信息,在你的情況下最有用的是e.range。此Range對象對應於編輯爲觸發onEdit事件的單元格。您可以使用它來獲取工作表的名稱(您稱之爲選項卡)和編輯的列。

使用這些信息,您可以發送相應的電子郵件。祝你好運!