Validating text item input in google script(for google forms) - google-apps-script

when you are not using scripts, there is an option to put a condition for inputting e-mail adresses then making it a required question. Because I'm randomizing my test and refreshing it every minute I cant do this manually, is there a way to do this by script?

Update: Google team implemented this feature. Check it out: https://developers.google.com/apps-script/reference/forms/data-validation-builder
Here is the ticket to add this feature:
https://code.google.com/p/google-apps-script-issues/issues/detail?id=4216#makechanges
About make the item required, you can do it like this:
if (item.getType() == FormApp.ItemType.TEXT){
var textItem = item.asTextItem();
textItem.setRequired(true);
}
You can do that for any item type that can have answer.
For the complete list of the Items types refer to this link:
https://developers.google.com/apps-script/reference/forms/item-type#properties

Related

Dynamic dependent dropdowns on Google Form

I need to add the following functionality on a google form -
I need to have two dropdowns on the form-
Dropdown1 values are being populated from the google sheet using a script.
Dropdown2 values must also be populated from the google sheet BUT must change based on the value selected in Dropdown1 on the form.
Since Dropdown1 has many values in the list, using the "go to section based on answer" is not an option to show Dropdown2.
How can I implement this cascading/dependent dropdown functionality dynamically on the google form (I guess using the google script and google sheet)?
Thanks in advance!
Answer:
Unfortunately, this is not possible using Google Forms.
Explanation:
In order to change the contents of later dropdowns based on the selected answer in a previous question, direct access to the browser session must be retrieved by Google's server before form submission. This isn't done.
Until a form has been submitted by the user, giving explicit permission to send the answers they have provided, the answers are not communicated to Google's servers and as such later questions can not change based on previous answers. Implementing this would be a privacy issue so I can not forsee Google creating this option in the future either.

Possible to pre-assign user to google form?

I am creating a Google Form dynamically, and emailing it, using Google Apps Script.
I have all the users info, and have figured out how to record the response. However, I would like to add that person's email to the row in the sheet.
It would be ideal if there was a way, since we already collected their email, to pass this email in the "background" to the form we send, and it is attached with there answer in the responses sheet.
Thanks!
I was not able to rest until I got this worked out. This article from almost three years ago really helped.
Not exactly what I was going for, still open if anyone knows how to actually pass info in the background to a sheet.
var items = formName.getItems();
var itemOfInterest;
for(var i in items){
if(items[i].getTitle()=='QUESTION_TITLE'){
itemOfInterest=items[i];
}
}
var preFilledUrl =
schedForm.createResponse().
withItemResponse(itemOfInterest.asTextItem().createResponse(email)).
toPrefilledUrl();
This prefills that question box, so also giving the user the ability to edit before they send. It'll work.

Can I fill in TextItem by Google apps script?

I made a form with Google Form Builder, then I add a script for it.
I can run Session.getEffectiveUser().getEmail() to the respondent's email address. but I cant fill it in the textbox to assist them. Can I do such thing with Google App Script?
I think I found a solution to your problem. But I must admit it was not evident.
In apps scripts documentation you've got everything to create a prefilled url.
BUT for that you need to have a ItemResponse element and I didn't found any explaination to build one. The trick is when you've got an item you can get a ItemResponse from it if you get it as a defined type "asTextItem()".
Best way to understand it, is to watch the code below:
function getPreFilledItem(){
var form = FormApp.openById("YOUR_FORM_ID");
var items = form.getItems();
var itemOfInterest;
for(var i in items){
if(items[i].getTitle()=="YOUR_QUESTION_TITLE"){
itemOfInterest=items[i];
}
}
Logger.log(
form.createResponse().
withItemResponse(itemOfInterest.asTextItem().createResponse("PREFILLED_TEXT")).
toPrefilledUrl()
);
}
Hoping this will help you,
Harold
You can't dynamically modify Google Forms by attaching a script to the Form. See a recent question I asked to get a better understanding.
Basically, you can only use Google Apps Script to create forms in an automated manner. You can't set values for the items as of yet (I certainly hope they add this in the future...).
You can also see the limitations for Forms by looking at the documentation (TextItem, for example).

General assistance in Google Apps scripting

OK, I'm tired of searching for specific questions to help with a project, finding answers, changing my implementation which just adds more questions, realizing there's a better way to do things, etc. So allow me to ask for general assistance, I will then do my best to research how to do it and ask further questions if needed.
I'm writing a script to be used as a Gadget in a Google Site page
(I'm more than willing to share this if anyone wants to take a look
at it); right now I'm doing this just for me, but I want to write
this to be easily used by others.
This will list all user's Google Docs in a specified folder; when
selecting the document from the list, the contents will be displayed
for editing in another field.
The user will be able to define certain lines, starting wit a period,
to "mark" as chords that can be automatically transposed with the
push of a button; that is to say, the user clicks a button and all
A's go to A#, B to C, C to C# and so on, but only on the specified
"Chord" lines.
The user can then save this document back to the Google Docs for
printing if needed.
I've got the layout mostly. Some problems I'm coming across:
Doing a .find apparently finds all documents that have the given string in the name and
the contents. The fix would be to put the document IDs in a Hidden, but it doesn't seem
that a List returns the numbered item you clicked on, so how can I also get the ID
that's stored somewhere else?
I'd like the TextArea to be rich text for bolding and what-not; does
Google Apps have a text editor (it'd be awesome if I could just put
the Google Docs editor in a panel)? RichTextArea has been
deprecated, is there a replacement?
To do the transposing, I was planning on just putting every character
of the text area into an array, stepping through the array, when it
sees a "\n" followed by a "." to flip a var "on", then changing any
following characters, then if it sees another "\n" to turn the var
"off"; is there a better way to do this?
Or, is there way to add a script to a Google Document that would do
the transposing (I know you can do macros for spreadsheets, but there
doesn't really seem to be an equivalent for documents)? That way I can
just give out this macro and tell people to use on their existing document.
Since you asked, yes, separate questions would be appropriate, because the combination of questions is very specialized, while the individual problems might be more general, and of use to more people. But let me take a stab at it anyway...
[With the result of find()]... how can I also get the ID that's stored somewhere else?
DocsList.find() returns a list of File objects. Class File has a getId() method that returns the document ID you are used to seeing in Google Drive. To get the IDs of all your files:
var files = DocsList.getAllFiles();
for (var i in files) {
Logger.log(files[i].getId());
}
You should also look at DocsListDialog for creating a file picker that works on Google Drive.
RichTextArea has been deprecated, is there a replacement?
No, not in apps-script. You've just got TextArea. However, you may be able to embed a third-party rich text editor in your UI.
To do the transposing, ... is there a better way to do this?
Change the TextArea.value into an array of lines, then manipulate those, without needing to manage an on/off state. See How do I get information out of TextArea in Google App Script on the button click? and Javascript: Convert textarea into an array.
// aTextArea contains user's input. Probably a Johnny Cash song.
var inputText = e.parameter.aTextArea;
var inputLines = inputText.split('\n');
for (var i in inputLines) {
if (inputLines[i].charAt(0) == '.') {
// Transpose
}
}
// Put lines back together, if you wish
var outputText = inputLines.join('\n');
..is there way to add a script to a Google Document that would do the transposing...
Yes (capability extended to Docs and Forms since question was originally asked). No, Spreadsheets are the only document type that can be a container for scripts at this time.
Alternatively, you could employ a stand-alone script to operate directly on Docs! Perhaps with a script deployed as a Web App that lets users pick the target music to transpose from documents on their Google Drive, and that then writes a new copy of the document, transposed?

Get TextBox Value from Google Apps Script

I am new to the world of Google's Apps Script, and I am trying to create a basic UI for my end user to query data (stored in google spreadsheets) and display the data in a formatted / user friendly way.
Since I want to start off simple and add in components as I learn Apps Script I decided to create a basic form that will allow me to enter text in a text box, then assign that value to a label (what I thought would be a simple exercise in creating basic form components, and retrieving / assigning values).
Unfortunately the stumbling block I ran into is that there is no getText() or TextBox.getValue() function. I tried searching through online forums / google etc to find out the way around this, but nothing I try seems to work (previous searched led me totry and work this out in an event handler).
My question is this. If I have a textBox ('textBox1') and a button ('button1') and a label ('label1'). How to I get my web app to assign the value I enter in textBox1 to label1 when I click button1?
I realize this is probably the most basic of questions, and I know it has been asked before....but no matter how hard I dig through these old posts I can't seem to figure what should be an easy bit of code out.
Thanks in advance.
Suppose you have code that looks like this:
var textbox = app.createTextBox().setName("textboxName");
var buttonHandler = app.createServerHandler("updateLabelText").addCallbackElement(textbox);
var button = app.createButton("Click me").addClickHandler(buttonHandler);
var label = app.createLabel().setId("label");
Then in your function:
function updateLabelText(e) {
var app = UiApp.getActiveApplication();
app.getElementById("label").setText(e.parameter.textboxName);
return app;
}
So the things to note:
The button handler is given the name of a function that you define somewhere else in your code.
The button handler also must be given a "callback element". This is required if you want to read the value of that element in your function. You can add a panel as a callback element and anything that's on that panel will be added to the callback.
Values of callback elements are passed back through e.parameter. The property name is the name of the element. Example: e.parameter.textboxName.
The label needs an ID so that you can reference it from your other function.