Creating a custom alert in a form using Google Apps Script - google-apps-script

I am trying to create a custom alert message in a Google Form, the idea is that once the user submits the form it should show a message displaying a result based on what the user inputs on the form.
I've tried to use Browser.msgBox() and getUi().alert() but they seem to only work in spreadsheets.
Is there any way to achieve this inside a form?

Answer:
This can not be done in Google Forms.
More Information:
According to Google's documentation on Simple and Installable triggers:
The open event for Google Forms does not occur when a user opens a form to respond, but rather when an editor opens the form to modify it.
If attempting to run the FormApp.getUi() method on an installable trigger on Form Submit, the following error appears in the Stackdriver logs under My Executions:
Error: Cannot call FormApp.getUi() from this context. at onSumbit(Code:2)
So unfortunately, this can not be completed.
References:
Google Apps Script - Simple Triggers
Google Apps Script - Class Ui

Related

Why am I missing form submit trigger in Google Scripts UI?

In Google Scripts I'm trying to use a on form submit trigger, but all I see in events are Time-Driven and From Calendar
The project must be bound to the Google form or to the connected spreadsheet for the onFormSubmit trigger to be available in the triggers UI.
Open your Google form
Click Tools > Script editor from within the form

How to trigger google doc script with button or by editting it?

I'm trying to summarize info sent by a form (to make purchase requests) on a google doc with a way to approve the request or not from the google doc and retrieve the information in a Google Apps Script function.
The problem is that on Google Document the trigger onEdit doesn't exist.
Is there a way to create one ?
Or simply add a button to validate the request that would call a function ?
Thanks.
The only way to add a button that calls an Apps Script function on a Google Documents document is by creating a dialog or sidebar. This is done by using the HTML Service. For further details see https://developers.google.com/apps-script/guides/html/

Force auto-submit a Google Form using app-script?

I am using a code which closes the FORM at specified time. So what happened is some students are failing to submit it before the time limit and their responses are not being recorded.
Is there any way to force auto-submit a google form at specified end-time for all the users who are writing the quiz lets say!
Any ideas are welcome.
You can check this documentation about Installable Triggers which let Apps Script run a function automatically when a certain event occurs. It is similar to simple triggers for Google Apps like onOpen(), but they can respond to additional events, and they behave differently.
There are several installable triggers for Google Apps and you can use an installable form-submit trigger that runs when a user responds to a form.
Be noted that there are two versions of the form-submit trigger, one for Google Forms itself and one for Sheets if the form submits to a spreadsheet.

Cannot call FormApp.getUi() from bound Google Forms script

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?
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.)

Google Script Error: You do not have permission to call openById

I am trying to send a mail with attaching a file available in my google drive using following google scripts
var file=DriveApp.getFileById('1qZVK0UZ1jLbDdj10FXZqeAVEodvxEy2Bs');
MailApp.sendEmail('xyz#gmail.com','subject','body',{attachments[file.getAs(MimeType.PDF)]});
when I run above script manually it runs fine, email gets sent. But when I run it using form Submission trigger it gives error "You do not have permission to call openById"
steps that will reproduce the problem?
create a google form.
write above scripts on script editor and add a trigger with submission of the form.
fill and submit the form then you will get above error into mail from 'apps-scripts-notifications#google.com'
Please help.
I ran into this same issue, but felt it was a little silly to have to use the FormResponses Sheet to get the submission event (I mean there's a trigger on the Form Apps Scripts for a reason, right?).
All I had to do was to Remove and Re-add the trigger for From form --> On form submit. When I added it, challenged me for OAuth permissions to view and use my google drive.
Immediately after this, I was able to run on a Forms Apps Script Submission trigger again.
I found this when creating the script on the Form itself.
To solve the problem first have you form responses sent to a spreadsheet.
From you form go to Tools > Script editor... copy you script and delete any triggers you have set up there, you won't be needing this script anymore.
Now click on View responses button in your form to open the spreadsheet.
Go to Tools > Script editor... (for the spreadsheet).
You should start with a blank project.
Delete anything in there and paste in your script.
Save and name you project.
Now set up your triggers as normal. You will find the third drop down list of the new trigger has the option On form submit.
Once your script is authorised any time the form is submitted the event will trigger and will have access to the DriveApp.
Once you've tested and got it working I would go back to the form and delete the script project in there. I do that to keep things tidy and save confusion if I come to alter the script later and have forgotten which one is actually live.
I don't know if there is a limitation with Google Forms that limits their access to Drive or something.