1

我覺得這應該是如此簡單和明顯,但我不出來...獲取腳本時,只有特定的形式提交給觸發 - 谷歌腳本/表/表格

我一起工作2個表格和2個電子表格。表格1提交給工作表1.在工作表1中,對於每條記錄,都有一個鏈接將用戶帶到表格2,該表格的一半預填充了表格1中的數據。當用戶提交表格2時,表格2 。

我有幾個腳本,應該在提交表單1時觸發。我正在使用「From Spreadsheet」「OnFormSubmit」觸發器。但是,在提交表單2時,腳本也會觸發。

如何才能讓腳本僅在提交表單1時執行?另外,有沒有辦法確保腳本以特定順序觸發?

如果有幫助,腳本如下。除觸發問題外,它們都按原樣正常工作。我知道我可以合併第二和第三個腳本,但我會,但是我想先解決這個觸發問題,因爲每次測試時我都會收到兩倍的電子郵件。

1日腳本:

function onFormSubmit(e) { 

    //Uses time in milleseconds to create unique ID # 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var sheet = ss.getSheetByName("ComplaintLog"); 
    var d = new Date(); 
    var n = d.getTime(); 
    var lastRow = sheet.getLastRow(); 
    var cell = sheet.getRange("BA" + lastRow); 
    cell.setValue(n); 
    var cell = sheet.getRange("BB" + lastRow); 
    cell.setFormula("=right(BA"+lastRow+",6)"); 
    sheet.getRange("BB"+lastRow).copyTo(sheet.getRange("BC"+lastRow), + 
    {contentsOnly:true}); 
} 

第二腳本:

function formSubmitReply(e) { 
//Sends email to certain users when a new complaint has been entered 
    var emailAddresses = '[email protected]'; 

    MailApp.sendEmail(emailAddresses, 
        "[email protected]", 
        "New Guest Complaint", 
        "A new guest complaint has been entered into the database." 
        + "\n\n To vew the database, click here: http://goo.gl/DI33EC"); 
} 

第三腳本:

function createResolutionForm() { 

    var ss = SpreadsheetApp.getActive() 
    var sheet = ss.getSheetByName("ComplaintLog") 
    var lastRow = sheet.getLastRow(); 
    var data = ss.getSheetByName("ComplaintLog") + 
    .getRange("A"+lastRow+":Z"+lastRow).getValues(); 
    var form = FormApp.openById('The form's ID goes here. that part works.'); 
    var items = form.getItems(); 

    for (var i = 0; i < data.length; i++) { 
    var formResponse = form.createResponse(); 

    //ID 
    var formItem = items[1].asTextItem(); 
    var response = formItem.createResponse(data[i][0]); 
    formResponse.withItemResponse(response); 

    //Guest Name 
    var formItem = items[2].asTextItem(); 
    var response = formItem.createResponse(data[i][3]); 
    formResponse.withItemResponse(response); 

    //email 
    var formItem = items[3].asTextItem(); 
    var response = formItem.createResponse(data[i][4]); 
    formResponse.withItemResponse(response); 

    // The pre-populated form is being created here. I didn't include every 
    // form item for brevity's sake. 
    } 

    //Create Link 
    var formUrl = formResponse.toPrefilledUrl(); 

    //Enable Clickable ID 
    var idNum = sheet.getRange("BC"+lastRow).getValues(); 

    sheet.getRange("A"+lastRow).setFormula + 
     ('=HYPERLINK("' + formUrl + '","' + idNum + '")'); 

    var sheetUrl = "The URL to the spreadsheet goes here - that part works."; 


    //Send Email to assigned managers 
    var j,tempname=[],name, subject, managername, message; 

    managername = sheet.getRange("P"+lastRow).getValue(); 
    tempname=managername.split(" "); 

    Logger.log(managername) 

    if (tempname.length==2) { 
     name=tempname[0].slice(0,1) + tempname[1] + '@organization.com'; 
     subject = 'Action Required'; 
     var message = "<html><body>" 
     + "<p> You have been assigned to follow-up on a complaint," 
     + "or your contact information has been given to a customer in" 
     + "regards to a complaint." 
     + "<p><p>The complaint ID number is " + idNum +"." 
     + "<p>To go directly to this complaint," 
     + "<a href="+formUrl+"><b>click here</b></a>." 
     + "<p>To vew the database so that you can take action," 
     +<a href="+sheetUrl+">click here</a>." 
     + "</body></html>"; 


     MailApp.sendEmail(name, subject,"",{htmlBody : message}); 
     } 
    } 

回答

-1

設置從腳本中的觸發器可能是你在找什麼。您可以創建響應,形成在每個表單提交的基礎事件,像這樣的觸發器:

function setup() { 
    ScriptApp.newTrigger('onForm1ResponseHandler').forForm(form1).onFormSubmit().create(); 
    ScriptApp.newTrigger('onForm2ResponseHandler').forForm(form2).onFormSubmit().create(); 
} 

其中Form 1和Form是Form對象和「onForm1ResponseHandler」是處理函數的名稱。

隨着設立這樣的處理函數:

function onForm1ResponseHandler (e) { 
    ... 
} 

e將與屬性的對象記錄here(用於表單提交的事件)。

+0

爲什麼-1?這看起來是一個很好的答案 –