Possible to pre-assign user to google form? - google-apps-script

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.

Related

Go to next page depending on multiple answers – Google Form

I've tried to create a Google Form with Google Apps Script without success.
My goal is to redirect to a new page depending on the results of multiple answers, not just one. I've got three questions and if some of them has a specific answer I've would like to go to another page.
It seems like its possible just for one question like this:
var choiceOne = item.createChoice('Vanilla', FormApp.PageNavigationType.CONTINUE);
var choiceTwo = item.createChoice('Chocolate', FormApp.PageNavigationType.SUBMIT);
Is it a way to solve the problem?

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

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

Script to update Google Form from Spreadsheet

Here's my problem:
I can't seem to piece this info together nor find it anywhere even though it seems simple enough. When I find help, they just tell me to enter it manually (but my users can't be trusted) or make a new form (not an option).
What I need is to be able to keep:
the same google form
it's ID and link to the same spreadsheet
the same 20 "paragraph text" questions (titles remain the same).
There can't be any new questions or anything that would change the name or layout of the response destination page in the spreadsheet.
However, what I want is:
to update the help text below these questions from twenty cells in the spreadsheet. If my cells are from a range called questions!b2:b19 in the same spreadsheet, how do I use a script to take their contents and write over and update the help text in these paragraph questions on the google form?
Any help would be greatly appreciated.
It might be easiest to create your form in AppScript. With your form created, you can then use AppScript to access your spreadsheet. Read the information from the sheet and use it to create the help text on your questions with setHelpText.
Here's a basic sample.
var form = FormApp.create('New Form');
var item = form.addCheckboxItem();
var ss = SpreadsheetApp.openById("<ID>");
var val = ss.getRange(<RANGE>).getValue();
item.setTitle('Question');
item.setHelpText(val);
item.setChoices([
item.createChoice('ONE'),
item.createChoice('TWO'),
item.createChoice('THREE')
]);

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?