Cannot call FormApp.getUi() from bound Google Forms script - google-apps-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.)

Related

Is there a way to cause a UI input box to pop up at specified intervals regardless of the page you're on using Apps Scipt?

I'd like to prompt the user to enter data into a workbook at regular intervals using a UI input box and Google Apps Script. Something like this but with the timer added:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var timer = Browser.inputBox('New Data', 'How many minutes?', Browser.Buttons.OK_CANCEL);
ss.appendRow([timer]);
}
Is there a way to force the spreadsheet window to be the active window when the timer goes off or somehow alert the user to enter data if they've navigated away from the page? TIA!!!
Workaround
From the information you have provided and your requirements (pop up a dialog not only when you are on the Spreadsheet but also on any page of the browser) I think your best option would be to create or use a Chrome extension. If you want to pass the data received in this extension into a specific Spreadsheet to log the user's activity then you want to use Sheets API.
The problems using simply Apps Script for this case scenario are the following:
Time based triggers cannot trigger an UI element such as Browser.inputBox() or getUi
An UI dialog in the Spreadsheet stops the execution of the script until it is dismissed. This will lead that if your user forgets to add the information and close the dialog the script will stop, missing also the next time turns.
The user would need to have the Spreadsheet tab opened as the script only controls what is going on the Spreadsheet and not in the browser. As you want to track the behaviour no matter in which tab the user is in, I believe that it would be best experience for the user to simply have a Chrome extension that does that job.

Creating a custom alert in a form using 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

How to ask for user input in "on edit" installable trigger in Google Spreadsheet?

When a user edits a cell in particular column to a particular value, messages are sent to a Telegram bot, depending on a new value.
This is implemented by an "installable trigger" on edit event. It is installed by the document owner and always runs under that account. There are other users editing the doc and triggering the action. All works fine.
Now for some action we need to request a few words from the user who made the edit.
But since November 2017 (see issue 68846962) "for security reasons" Google restricted their Apps Script and now Ui methods prompt() and showSideBar() are only available to the user who created the trigger. For others it causes "You do not have permission to call showSidebar()" error.
What workaround could there be to request and receive user input on the cell edit event for all users? Considering that further actions require permissions to access external services (call Telegram API).
As a possible workaround, you can create separate sheet that will be your "form" for user input:
"Submit" button should be an image, assign function to it by context menu - "Assign script" (enter your function name to process "form" data):
Then your onEdit trigger should just activate "form" sheet for the current user when edit happened. Sample code:
function onEdit() {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form').activate();
}
After filling the form user clicks on "Submit" image, then you can send data to external service, sample code:
function submit() {
var response = UrlFetchApp.fetch("http://www.google.com/");
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Results').appendRow([response.getResponseCode(), new Date()]);
}

Google App Scripts - Create Google form using CreateForm() function and run MarkForm() function on form submit

Aim
I am currently using Google App Scripts to create a form that one submitted sends an email to the respondent with a detailed summary of their results.
Methods so Far
To achieve this, I have written two functions: one function to create the form and a second function to mark and email the summary of the results to the respondent.
function CreateForm(){
//Working code to create form here
}
function MarkForm(){
//Working code to create and email detailed summary of results
}
In order to make this code work correctly, I have to:
Create the form using the CreateForm() function
Open the new form and paste the MarkForm() function into the script editor.
Set a trigger OnFormSubmit to run the function MarkForm()
Question
Is it possible to do the three steps above using one function?
You can use the "On Form Submit" installable trigger to run a function when a form response comes in. Reading the Google Triggers guide in the Apps Script Documentation will help.
Do the following:
Choose Edit > Current project's triggers. You see a panel with the message No triggers set up. Click here to add one now.
Click the link.
Under Run, select the function you want executed by the trigger. (MarkForm())
Under Events, select From Spreadsheet.
From the next drop-down list, select On form submit.
Click Save.
Now, when a submission comes in, the form response will be marked automatically. This trigger is an installable trigger, which means it needs to be set up through the menu rather than calling a function like onEdit().

running onload function in Google Form app script

I have a Google Form and a script and a script bound to the form.
I'm trying to get a function to run when a user fill in the form.
But the only function I get to run is when a hook it to the OnFormSubmit trigger.
No other function is running, no matter what I try.
Can I run a function i Google App Script on initial load when a user is filling the form? Is this possible?
Thanks
More details (I'll accept some other way to accomplish the task):
the function should capture some url parameters I pass to the form, and send it over to Google Analytics... I believe that part is solved here: Google Forms & The Measurement Protocol
As the form has several pages, the parameters I pass to the first page gets lost when the user goes to next pages, so I cannot capture them when submitting the form - therefore I'm trying to do it whenever the users first edits the form.
I tried to add the params as prefilled fields on a hidden page, but if it is hidden then it too gets lost on submitting...
Since Sandy did not write out an answer, I will.
You cannot execute Apps Script as part of a form when the user visits it. You can't modify a form as the user is filling it out (Dynamic form...etc). If you need this type of behavior, you will need to create your own form as part of a Apps Script Web App.
Web App information: https://developers.google.com/apps-script/guides/web