Google Apps Script - Require One Response per Row for Grid Item
I Am Creating Some Student Surveys with Google Forms Using Google Apps Script. the Question Type I'm Using Is a Grid Item. Each Row Is a Question About the...
I am creating some student surveys with Google Forms using Google Apps Script. The question type I'm using is a grid item. Each row is a question about the teacher and each column is a rating (strongly disagree, disagree, etc...).
When creating a form using the normal Forms interface there is an option to require a response for each row within a grid item. I've searched through all the documentation and it appears there is no way to set this using GAS. Am I missing something or is there any workaround?
I can go in and manually set this after all forms have been created but there are a lot of classes and teachers so it will be quite time-consuming.
Here is the code so far:
function createForm() {
var form = FormApp.create('Student Survey');
var questions = getQuestions();
var ratings = getRatings();
var grid = form.addGridItem();
grid.setTitle("Please answer the following questions about your teacher.");
grid.setRows(questions);
grid.setColumns(ratings);
grid.isRequired();
}
Thanks for any help.
1 Answer
It's a little sneaky, but it is there!
For one response per row: Use your GridItem's
.setRequired(true);
For one response per column: Check out the GridItem's setValidation() method. It takes a GridValidation parameter you can build with the GridValidationBuilder. See the second section of code below.
Here is your code with both validation types:
function createForm() {
var form = FormApp.create('Student Survey');
var questions = getQuestions();
var ratings = getRatings();
var grid = form.addGridItem();
grid.setTitle("Please answer the following questions about your teacher.");
grid.setRows(questions);
grid.setColumns(ratings);
grid.setRequired(true); //This part is to require one response per row
//This section requires one response per column.
var gridValidation = FormApp.createGridValidation()
.setHelpText("HELP TEXT")
.requireLimitOneResponsePerColumn()
.build();
grid.setValidation(gridValidation);
}