Reputation: 17
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
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.
You can test your script as follows.
myFunction()
in your showing script.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.
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
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