Reputation: 28
So I have a spreadsheet script with a form submission trigger set up with:
ScriptApp.newTrigger('onConfirmationFormSubmit').forForm(confirmationForm).onFormSubmit().create();
with the handler function:
function onConfirmationFormSubmit(event)
{
Logger.log('Source object ID:');
Logger.log(event.source.getId());
Logger.log('Spreadsheet ID');
Logger.log(SpreadsheetApp.getActiveSpreadsheet().getId());
}
Now, according to this page, for a Form Submit event, the source object in the event parameter should be the Form which was submitted - but when a form is submitted, the above code gives the following log output:
[14-07-04 17:03:59:417 EST] Source object ID:
[14-07-04 17:03:59:417 EST] 1I7ap9XJuFJtTgcM4xIFpE-IGqXQE8ohDJEpN7oN9IUk
[14-07-04 17:03:59:417 EST] Spreadsheet ID
[14-07-04 17:03:59:418 EST] 1I7ap9XJuFJtTgcM4xIFpE-IGqXQE8ohDJEpN7oN9IUk
The documentation page linked above states that the source property should always be a Form object, but it seems to be a Spreadsheet object - is this intended?
For clarification, you can't set up this type of trigger for a spreadsheet by clicking the 'Current Project's Triggers' button in the script editor, it can only be created with the first code snippet I posted. See:
https://i.sstatic.net/Z5bSE.png
(in this image, the trigger for the onFormResponse
function was created programmatically, while the someOtherFunction
trigger was created with the 'add a new trigger' button).
Upvotes: 0
Views: 1914
Reputation: 1
I fixed the code
function fixEventWithTriggers (event){
ScriptApp.getProjectTriggers().forEach(function (trigger) {
if (trigger.getUniqueId()==event.triggerUid) {
Logger.log("trigger source id: " + trigger.getTriggerSourceId())
event.source = FormApp.openById(trigger.getTriggerSourceId())
Logger.log("e.source: "+event.soure)
return event
}
})
return event
}
Upvotes: 0
Reputation: 109
The failure to provide the .source attribute of the event is documented in Google Code Issue 4810
Luckily, there is at least one workaround, provided in the comments on that issue, which is to use the getEditResponseUrl method of the response to get to the form. Here is my implementation of the fix in the form of a function that fixes up the event object to add the missing source:
function fixBrokenEvent (event) {
if (! event.source ) {
var responseEditUrl = event.response.getEditResponseUrl(); //gets edit response url which includes the form url
var responseUrl = responseEditUrl.toString().replace(/viewform.*/,''); //returns only the form url
event.source = FormApp.openByUrl(responseUrl); //gets the submitted form id
}
return event
}
This workaround does the trick for me. Another solution would be to use the Trigger UID and search through the list of triggers from ScriptApp.getProjectTriggers() for the right trigger UID.
Something like...
function fixEventWithTriggers (event) {
ScriptApp.getProjectTriggers().forEach(function (trigger) {
if (trigger.getUniqueId()==event.triggerUid) {
event.source = FormApp.openFormById(trigger.getSourceId())
return event
}
}
}
This last workaround comes from Comment #5 on Issue 3786
Upvotes: 1
Reputation: 1118
There are two kinds of form submission depending of the type of file that trigger the event, the spreadsheet one (the kind that you're calling) and from de Form file. In the documentation the spreadsheet one doesn't have the source parameter explained, but it looks that it's implemented.
The source indicates the file that triggered the event is your file, and it's no related to the source of the the Form.
If you need to open the Form file in the spreadsheet script you have to code the opening of the file by [FormApp.openById][1]
or [FormApp.openByUrl][2]
passing hardcoded the ID/URL. This will make the script to ask you for Forms authorization.
Upvotes: 0