Pre-filling a google form - google-apps-script

I want to pre-fill a Google form such that some of the fields which are pre-filled, are the responses from another form.
What i mean to say is that i send out a few columns of the responses of one of my forms and get the information validated by a third party. Is this kind of pre-filling possible?

function sendForm(e) {
//Code to open second form and get the items of the form
var form = FormApp.openById('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
var items = form.getItems();
var formItem = items[0].asTextItem();
var formResponse = form.createResponse();
var count=0;
var response = formItem.createResponse(e.namedValues["Name"].toString());
formResponse.withItemResponse(response);
formItem = items[1].asTextItem();
response = formItem.createResponse(e.namedValues["Email"].toString());
formResponse.withItemResponse(response);
//Creates URL with pre-filled data
var url = formResponse.toPrefilledUrl();
//Mail the link to the people required.
}
This code goes in the response sheet of the first form and a trigger must be added to call this function every time a form submit action happens.

Related

Validate form with a row value from spreadsheet before submit

I have a Form linked to SpreadSheet and i want to validate the text field "email", and if field exists, go to specific section/page break programatically because Google Forms doesn't give me the option to validate a textfield.
This is a bit of the code i have done until now:
function selectedItems(){
//Select the form and get the items
var form = FormApp.getActiveForm();
var formResponses = form.getResponses();
var formResponse = formResponses[formResponses.length - 1];
var itemResponses = formResponse.getItemResponses();
var name = "";
var email = "";
var gender = "";
//get email field response of the form
for(var i = 0; i < itemResponses.length; i++){
switch(itemResponses[i].getItem().getTitle()){
case "Correo electrónico:": email = itemResponses[i].getResponse();
break;
case "Sexo:": gender = itemResponses[i].getResponse();
break;
case "Nombre completo:": name = itemResponses[i].getResponse();
break;
default:
break;
}
}
//get the spreadsheet where the data is stored
var spreadSheet = SpreadsheetApp.openById(MI FORM ID)
var spreadSheetData = spreadSheet.getDataRange()
var rowValue = spreadSheetData.getValues()
//get the sections title... i don't know what to do here
var sections = form.getItems(FormApp.ItemType.PAGE_BREAK)
var section = sections[0].getTitle()
}
Basically:
I need that if mail exists in spreadsheet, instead of continue to the next section in Google Forms, i need that the user go to last page. I don't know if it's possible with a Form made via GForms. Thanks in advance.
There is no way of achieving this as even though you end up validating a text field such that it contains an email address, you cannot redirect the user to another section as there is no method for it.
From the documentation:
createChoice(value, navigationItem) > Creates a new choice with a page-navigation option that jumps to a given page-break item. This is equivalent to createChoice(value, navigationType) with navigationType set to FormApp.PageNavigationType.GO_TO_PAGE. Choices that use page navigation cannot be combined in the same item with choices that do not use page navigation.
However, the above method is specific to the MultipleChoiceItem Class.
A potential workaround is to use a multiple choice item and redirect the user based on their choice - however, this implies that you will end up relying on the user's statement, without any control from your side.
function selectedItems() {
//other code
let form = FormApp.getActiveForm();
let emailQuestion = form.addMultipleChoiceItem()
.setTitle('Please input your email')
var validEmailSection = form.addPageBreakItem()
.setTitle('Yayy! You have a valid email!')
.setHelpText('Ah bee cee dee');
emailQuestion.setChoices([
emailQuestion.createChoice('Yes, I confirm I have a valid email', validEmailSection),
emailQuestion.createChoice('No, the email is incorrect', FormApp.PageNavigationType.CONTINUE),
]);
}
Another option...
File a feature request on Google's Issue Tracker here!
Reference
TextValidationBuilder Class;
MultipleChoiceItem Class.

Set pre-filled value of Google Form item from google apps script

What I want to achieve?
I want to track the review status of a google document with a google form with dropdown options as "To do, In progress, Done". I have google form items as "URL of document, Status". I have created a google form template that I'll be using to create forms for various users. I want to be able to create a copy of the template form, and set predefined value of "URL" from google apps script, so that the users just have to select the status of the document.
What I tried?
I came across createResponse() method from this answer, but this requires .submit() to be used to save the response and the answer is recorded in sheets. I don't want to submit the form from the script itself. Here is the code:
function form()
{
var form = FormApp.create("Test");
form.addTextItem().setTitle("URL");
form.addTextItem().setTitle("Status");
var items = form.getItems();
var url = items[0].asTextItem();
var fr = url.createResponse('my predefined url');
var FormResponse = form.createResponse();
FormResponse.withItemResponse(fr);
FormResponse.submit();
Logger.log(form.getPublishedUrl());
}
Final query:
How do I get the published URL of the form with the pre-filled answer to the URL item from the apps script? Is it possible?
Turns out there is a method .toPrefilledUrl() which resturns the published url of form with prefilled values. Here is an example:
var form = FormApp.openByUrl("url");
var items = form.getItems();
var url = items[0].asTextItem();
var d= "some.url"
var fr = url.createResponse(d);
var FormResponse = form.createResponse();
var urlPub = FormResponse.withItemResponse(fr).toPrefilledUrl();
Logger.log(urlPub);

pre-fill google form from google apps script

I have a student registration form, there's student id which is a required field. I have a google apps script function which tells if this student is registered for any class or not. Is there a way to auto-fill the field course registered via calling the google apps script function yes or no.
Yes you can create a pre filled response with the forms ID, not that the pre filled fields are showed in the URL
Function formPrefill(formId){
var form = FormApp.openById(formId);
try{
var items = form.getItems();
var formResponse = form.createResponse();
// Prefill SessionId
var formItem = "SOMETHING HERE"
var response = formItem.createResponse(sessionId);
formResponse.withItemResponse(response);
//--------ANOTHER FIELD-------------
formItem = items[4].asMultipleChoiceItem();
response = formItem.createResponse('YOUR FIELD NAME');
formResponse.withItemResponse(response);
}catch(e){catch an error here}
}
check https://developers.google.com/apps-script/reference/forms/form#createresponse

Sort Google Forms responses on Submit

I have three columns in my Sheet where Google Forms store response data: Gender, Name and Email.
If user sends form with Gender-field value equals "Male" I want to store this response to existing sheet named "Males".
Here is my solution(I've already added special trigger for it and here is event hendler implimentation), but it doesn't work:
function onItemAdd(e) {
var formData = e.values;
if (formData[0] == "Male") {
SpreadsheetApp.setActiveSheet(ss.getSheets()[1]);
var form = FormApp.getActiveForm().getResponses();
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Males").getDataRange().getValues()[0];
var formRow = e.range;
var filteredCell = formRow.getCell(1, formRow.getLastColumn()).offset(0, 1).setValue(formData);
}
}
There is no values property for the On Form Submit event object. There are only 3 properties of the On Form Submit event object.
authMode
response
source
source gets the form. response gets the form response.
var thisResponse = e.response.getItemResponses();
var gender = thisResponse[0].getResponse();
Logger.log('gender: ' + gender);

Blank Multiple Choice Item Responses

I am attempting to create a response to a Google Form using the updated API. Below is my code:
function submitResponse() {
var form = FormApp.openByUrl('some url');
var items = form.getItems();
var itemResponse = items[0].asMultipleChoiceItem().createResponse('No');
var formResponse = form.createResponse().withItemResponse(itemResponse);
formResponse.submit();
return;
}
The form contains a Yes/No question. Yes and No are the only options and there is only one question. The script runs but it submits a FormResponse containing a blank response to the item. Any ideas?
Thanks for any and all help!
I just built a quick copy and this worked for me. The only thing I changed was the typo you have in itemResponse.
from:
var formResponse = form.createResponse.withItemResponse(itemREsponse);
to:
var formResponse = form.createResponse.withItemResponse(itemResponse);
Hope this helps.