pre-fill google form from google apps script - 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

Related

Why my Google Form FormResponse doesn't submitted?

I try to submit my Google Form response programmaticaly with Google Apps Script to set unique id to answer (I did it when response heading off to Google Spreadsheet, but it is not useful solution for me because I want to send id in Email Notification generated by add-on while form submitted). I follow to Google Apps Script documentation:
Creates a new response to the form. To answer a question item, create an ItemResponse from the item, then attach it to this form response by calling FormResponse.withItemResponse(response). To save the assembled response, call FormResponse.submit(),
and tried to execute this method in onFormSubmit event.
But it failed on FormResponse.submit() with Exception: Failed to send response. Wait a few minutes and try again. at onFormSubmit.
function onFormSubmit(e){
var response = '5';
var form = FormApp.getActiveForm();
var questions = form.getItems();
var textItem=questions[2].asTextItem();
var itemResponse=textItem.createResponse(response);
Logger.log(textItem.getTitle()); //Logs right title 'MyTitle'
Logger.log(itemResponse.getResponse()); //Logs right response '5'
var formResponse=form.createResponse();
formResponse.withItemResponse(itemResponse);
formResponse.submit(); //An exeception occurs here
}
What could be the problem? Thanks.

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

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

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.

Google Apps Script: create a pdf by editing response to google form

I'm hoping that someone can help me, What i want to do is to make a Google form create a new document and send that pdf via email when the form is edited.
I understand that when you edit a form the script inside a spreadsheet does not run, So i have put the script into a form and that is where i am having problems. I can not get the script to read the values that are been resubmitted on the form, it just keeps on coming up as "undefined" on the return
This is the script that i have got so far.
function Test(e) {
var form = FormApp.openById('****Form Key****'); //Enter Form ID here
var docTemplate = "***doc to be used***";
var docName = "***Name of document***";
var formResponse = form.getResponses();
var one = e.response; //I have tried e.value but does not pull through
var two = e.response; // Column 2
var copyId = DocsList.getFileById(docTemplate)
.makeCopy(docName)
.getId();
// Open the temporary document
var copyDoc = DocumentApp.openById(copyId);
// Get the document’s body section
var copyBody = copyDoc.getActiveSection();
copyBody.replaceText('one', one);
copyBody.replaceText('two', two);
// Save and close the temporary document
copyDoc.saveAndClose();
// Convert temporary document to PDF by using the getAs blob conversion
var pdf = DocsList.getFileById(copyId).getAs("application/pdf");
//Sends the email
var email_Address = "***Email Address***"
var subject = "***Subject";
var body = "**Body***";
MailApp.sendEmail(email_Address, subject, body, {attachments: pdf});
}}
Any help will be greatly appreciated as i have been stuck on this for a while. thank you.
In a Form Response Trigger function, the event is an instance of Class FormResponse. Because of that, you don't need to open the form, or get responses... you've just had one handed to you, which you access via e.response. (See Form Response Events in Understanding Events.)
// Handle form response event
// This function must be a Form Response Trigger, attached to
// a Form (not a Spreadsheet).
function rightTest( e ) {
var formResponses = e.response.getItemResponses(); // array of responses
var one = formResponses[0];
var two = formResponses[1];
...
}
As of April 2015, DocsList is not supported, simply replace DocsList. with DriveApp. How to update DocsList to DriveApp in my code