S. Costa
S. Costa

Reputation: 17

Create a Google Form and add an installable trigger to it from Sheets Apps Script

I have a spreadsheet with data from which a form will be created when the user selects a custom menu to do so. After this newly created form is submitted, I'd like to run another function through a onFormSubmit trigger (to automatically create a document with the form's response based on a Google Docs template file).

What I have tried so far is:

function myFunction() {
  const spreadsheet = SpreadsheetApp.getActive();
  const sheet = spreadsheet.getActiveSheet();
  const range = sheet.getRange(2,1,sheet.getLastRow(),sheet.getLastColumn());

  const form = FormApp.create(spreadsheet.getName() + ' ' + sheet.getName())
    .setDestination(FormApp.DestinationType.SPREADSHEET, spreadsheet.getId());
  ScriptApp.newTrigger('newDocFromTemplate').forForm(form).onFormSubmit().create();

  // form fields creation
}

function newDocFromTemplate(e) {
  // code goes here
}

But the created form has no triggers and newDocFromTemplate() doesn't appear to be called anywhere. I also tried changing the trigger to onOpen but it also doesn't seem to create the trigger.

Upvotes: 1

Views: 60

Answers (2)

Tanaike
Tanaike

Reputation: 201553

In the case of the installable trigger, it can be used from the outside of the Google Docs files (Document, Spreadsheet, Form, and so on). This answer might be useful for understanding this. Ref

So, I think that your script works. But, from But the created form has no triggers and newDocFromTemplate() doesn't appear to be called anywhere., I'm worried that you might misunderstand this script. The flow of your script is as follows.

  1. Create a new Google Form.
    • In this case, a new Google Form is created in the root folder.
  2. Install the OnSubmit trigger in the created Google Form.
    • In this case, the OnSubmit trigger is installed on the current Google Apps Script project. This is not installed in the container-bound script of the created Google Form. Please be careful about this. But. this installed OnSubmit trigger is fired when the created Google Form is submitted.

Testing:

You can test your script as follows.

  1. Run myFunction() in your showing script.
  2. Submit the created new Google Form.

By this flow, newDocFromTemplate of your showing script is automatically run by the OnSubmit trigger. In this case, the Google Apps Script project including your showing script is run. So, for example, when you check the triggers on the Google Apps Script project including your showing script, you can confirm the installed triggers. Also, you can see the log when the Google Form is submitted.

Important:

As an important point, in the current stage, the maximum number of installable triggers for a Google Apps Script project is 20. Please be careful about this. So, when you use your showing script, 20 Google Forms can be created by installing the OnSubmit trigger.

Upvotes: 0

Lidia
Lidia

Reputation: 61

I don't think you can create a trigger in that way, as the function won't be in the script of the newly created Forms.

You can use the OnSubmit trigger event directly from your sheets, as you are linking the new Forms to the current spreadsheet, and do the process you'd like directly from Sheets!

Upvotes: 0

Related Questions