Blank Multiple Choice Item Responses - google-apps-script

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.

Related

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);

Fill Google form using Google Apps Script

I just tried my coding below...but the problem is, my google apps script just submit the form with blank value for both "short answer" field. Can someone show me somelight how i can solve my problem. TQ.
function myFunction() {
var dapatkanForm = FormApp.openByUrl('https://docs.google.com/forms/d/1S1F3cKMwkXm3gVt00RegQ-GCzRfELOq74IH9WEk8SlU/edit?usp=sharing');
var nama = dapatkanForm.getTitle();
var listIsiText = dapatkanForm.getItems(FormApp.ItemType.TEXT);
var panjangArray = listIsiText.length;
for(var i=0; i<panjangArray; i++)
{
var textTemp = listIsiText[i];
var nakSet = textTemp.asTextItem();
var response = nakSet.createResponse('MOHD ANIS BIN AZINAN');
}
var formResponse = dapatkanForm.createResponse();
formResponse.submit();
Logger.log(nama);
}
Your code is very close to the solution. You just need one additional method. The following line…:
var formResponse = dapatkanForm.createResponse();
…should end up looking like:
var formResponse = dapatkanForm.createResponse().withItemResponse(response);
By using .withItemResponse() the form submit will upload your responses instead of being void. Please, ask me for more clarifications if you still need help.

ScriptApp.newTrigger().forForm Issue

I am attempting to create a Google Form that when fed a different Google Form URL, it would create an onFormSubmit() trigger for the given form url.
function createFormSubmitTrigger(e) {
var formResponse = e.response;
var itemResponses = formResponse.getItemResponses();
var url = itemResponses[0].getResponse();
var formCopy = FormApp.openByUrl(url);
ScriptApp.newTrigger('onFormSubmit').forForm(formCopy).onFormSubmit().create();
}
When I log the details of formCopy, it is definitely retrieving the details of the correct given form. And yet, the only triggers getting created are for the Google Form that is accepting the URLs
Triggers Description
Am I understanding TriggerBuilder incorrectly? Is this some sort of bug?

get the form attached to a spreasheet

I'm working with the "new" version of google SS.
I'd like to get the form attached to the spreadsheet I'm in, like this:
function findFormURL() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
return ss.getFormUrl();
}
However, this function does not work yet in the new version.
Anyway,it gives the URL of the form, which is interesting, but I'd like to have the form ID or object so I can then work with it, change some stuff etc. Is that possible ?
This is indeed annoying but there is a possible way to get around this missing feature using the drive search capabilities... I tested it with the code below and it worked.
I agree that this is far from ideal and requires to have a form that has the same unique name as your spreadsheet but it's better than nothing.
function getFormTest() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var nameToSearch = ss.getName();
Logger.log('title contains "'+nameToSearch+'"');
var forms = DriveApp.searchFiles('title contains "'+nameToSearch+'"');
while (forms.hasNext()){
var formDoc = forms.next();
Logger.log(formDoc.getMimeType());
if(formDoc.getMimeType()=='application/vnd.google-apps.form'){
break;
}
}
Logger.log('formDoc = '+formDoc);
var form = FormApp.openById(formDoc.getId());
var items = form.getItems();
for(var i in items){
Logger.log(items[i].getTitle()+' '+items[i].getType());
}
}

Pre-filling a google form

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.