Specifying form for 'on form submit' trigger

后端 未结 3 1999
面向向阳花
面向向阳花 2020-12-04 03:08

I have two google forms, which send data to two tabs in a single spreadsheet.

I have set up a script with two functions. I would like the first function to run on s

3条回答
  •  眼角桃花
    2020-12-04 03:23

    Another way this can be done is by looking at the sheet name that comes in the response's event object via the range property. Here's an example of how I determined the form that triggered the submission:

    // set the sheet name that stores the form responses you care about here:
    function getSourceSheetName() { return 'Form Responses 1'; }
    
    function submit(eventObj) {
        var range = eventObj.range;
    
        var eventSourceSheetName = range.getSheet().getName();
        var givenSourceSheetName = getSourceSheetName();
    
        // if the source sheet (from form response sheet) is not the same as the specified form response sheet, quit (return) so extra forms don't trigger the code
        if (eventSourceSheetName.toUpperCase() !== givenSourceSheetName.toUpperCase() ) {
            Logger.log('A different form just submitted data - quit code');
            return;
        }
    
        // you now know which form submitted the response, so do whatever you want!
        // ... your code goes here
    }
    

    Basically, this code grabs the name of the sheet that the Form response comes from via range.getSheet().getName(). Next, it checks if that name is a specified name we're looking for (specified in the function getSourceSheetName().

    I think it's also possible to get the active sheet name via:

    eventObj.source.getActiveSheet().getName();
    

    In case you don't want to use "Range".

    I hope this helps!

提交回复
热议问题