问题
As the title says, I'm trying to get the UI of a Google Form from a script, so that I can show an alert box to the user with some information.
This is the relevant part of the function:
function Foobar(){
...
var ui = FormApp.getUi();
ui.alert("Foo", "bar", ui.ButtonSet.OK);
}
This code executes on the "On form submit" event trigger. Invariably, a few seconds later I get an email containing this error:
Cannot call FormApp.getUi() from this context.
Now, from my understanding, bound scripts (like this one, it was created from the form via Tools -> Script Editor) should have access to the ui of the form.
I already tried calling the same function from SpreadsheetApp, in case the form response automatically switched to the spreadsheet, but no dice. I also tried saving a (global) reference to the UI from a function called by the "On open" trigger, but that too failed.
What am I missing?
回答1:
This isn't possible in response to a form submission, you can't actually interact with the UI of a user-facing Google Form in "real time" at all.
At the documentation you linked, it states this limitation:
(Note that in Google Forms, user-interface elements are visible only to an editor who opens the form to modify it, not to a user who opens the form to respond.)
来源:https://stackoverflow.com/questions/33353333/cannot-call-formapp-getui-from-bound-google-forms-script