2016-01-08 181 views
0

我已經編寫了下面的代碼,根據在單元中是否找到某個字符串,向特定個人發送通知電子郵件。我想知道,如果我將腳本設置爲在「表單提交」觸發器上運行(如果它將再次通過完整的電子表格)(或發送重複的電子郵件),或者只是將新行添加到電子表格提交新的Google表單條目。Google Apps腳本觸發表單提交 - 不發送前一行

function notifySponsors() { 
    Logger.clear(); 
    var s = SpreadsheetApp.getActiveSheet(); 
    var data = s.getDataRange().getValues(); 
    var data_len = data.length; 

    //define variables with each organization 
    var mustang = "Camp Mustang Counselor"; 
    var chapel = "Chapel Tech Team"; 
    var dgroup = "D-Group Leader"; 
    var diversity = "Diversity Club"; 
    var journalism = "Journalism"; 
    var nhs = "National Honor Society Officer"; 
    var robotics = "Robotics Club"; 
    var stuco = "Student Council"; 
    var ambassador = "Student Ambassador"; 

    //Send e-mail to specific sponsors to notify that someone has submitted an application 
    for(var i=0; i<data_len; i++) { 
    var datastring = data[i][5].toString(); 

    //E-mail Camp Mustang Sponsors 
if(datastring.match(mustang)== mustang) { 
    var MustangString = data[i][1].toString()+" has submitted a Camp Mustang application"; 
    MailApp.sendEmail("[email protected]", MustangString, MustangString); 
    MailApp.sendEmail("[email protected]", MustangString, MustangString); 
    Logger.log(MustangString); 
    } 

//E-mail Chapel Sponsors 
if(datastring.match(chapel)== chapel) { 
    var ChapelString = data[i][1].toString()+" has submitted a Chapel Team application"; 
    MailApp.sendEmail("[email protected]", ChapelString, ChapelString); 
    Logger.log(ChapelString); 
    } 

//E-mail D-Group Sponsors 
if(datastring.match(dgroup)== dgroup) { 
    var DGroupString = data[i][1].toString()+" has submitted a D-Group Leader application"; 
    MailApp.sendEmail("[email protected]", DGroupString, DGroupString); 
    MailApp.sendEmail("[email protected]", DGroupString, DGroupString); 
    Logger.log(DGroupString); 
    } 

//E-mail Diversity Club Sponsors 
if(datastring.match(diversity)== diversity) { 
    var DiversityString = data[i][1].toString()+" has submitted a Diversity application"; 
    MailApp.sendEmail("[email protected]", "test 2", "New Application for D-Group"); 
    Logger.log(DiversityString); 
    } 
} 

} 
+0

嗯,我已經測試過了,它似乎再次遍歷整個電子表格。我怎樣才能修改這個腳本,使其只能在最近的表單提交上運行? – Tong

回答

0

不要從整張紙走一遍,而您可檢查響應表的最後一行(最新迴應你)

var sheet = SpreadsheetApp.getActiveSheet(); 
var lastrow = sheet.getLastRow(); 
    //imagining number of column in the value range is 10 
var lastRowValues = sheet.getRange(lastrow, 1, 1, 10).getValues(); 

現在刪除for loop。你可以得到最後提交的記錄如下。

var datastring = lastRowValues[0][5].toString(); 

現在您可以通過您的if statements併發送電子郵件。

乾杯!

+0

謝謝你的幫助! – Tong

相關問題