I have a script that runs on Google Forms. It is to look for multiple choice grids and transform them to multiple choice questions if rows or columns are > 3.
The form is set to one answer per row and an answer key is set.
Upon running the script, I get both “An error occurred: gridItem.getValidation is not a function” and “An error occurred: Invalid data updating form.”
I want to script to transform grid items to multiple choice, retrieve correct answers from the answer key, limit choices to 3 (1 of 3 should be correct answer), and limit to 3 choices.
Logs:
Jun 18, 2024, 10:50:15 AM Info Error updating grid item: gridItem.getValidation is not a function
Jun 18, 2024, 10:50:15 AM Info Error: gridItem.getValidation is not a function
// Handle GRID items
try {
var gridItem = item.asGridItem();
// Get rows and columns from the grid item
var rows = gridItem.getRows();
var columns = gridItem.getColumns();
// Check if the grid item has 3 or fewer rows or columns
if (rows.length <= 3 || columns.length <= 3) {
continue; // Skip this grid item if it has 3 or fewer rows or columns
}
// Iterate over each column in the grid
for (var c = 0; c < columns.length; c++) {
// Create a new MultipleChoiceItem for each column
var newMultipleChoiceItem = modifiedForm.addMultipleChoiceItem();
newMultipleChoiceItem.setTitle(columns[c]);
// Prepare choices based on the grid options for the current column
var choices = [];
for (var r = 0; r < rows.length; r++) {
var answerText = gridItem.getValidation().getValidationForCell(r, c).getHelpText(); // Assuming the answer is stored in the help text
var choiceText = rows[r] + ' - ' + answerText;
var isCorrect = answerText === 'correct'; // Adjust as per your validation logic
choices.push(newMultipleChoiceItem.createChoice(choiceText, isCorrect));
}
// Shuffle the choices
choices = shuffleArray(choices);
// Limit to 3 choices
choices = choices.slice(0, 3);
// Log choices before setting
Logger.log('New multiple-choice item choices: ' + JSON.stringify(choices));
// Set choices for the new MultipleChoiceItem
newMultipleChoiceItem.setChoices(choices);
Logger.log('New multiple-choice item created with choices: ' + JSON.stringify(choices));
}
// Remove the original GridItem from the form
modifiedForm.deleteItem(item);
} catch (e) {
Logger.log('Error updating grid item: ' + e.message);
throw e;
}
}
}`
```
I want to script to transform grid items to multiple choice, retrieve correct answers from the answer key, limit choices to 3 (1 of 3 should be correct answer), and limit to 3 choices.
Adequate multiple choice questions are populating with 3 answer choices, but 1 of the 3 choices is usually not the correct answer.
Sam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.