Skip to content

Instantly share code, notes, and snippets.

@danteese
Created July 27, 2020 05:24
Show Gist options
  • Save danteese/ee6f51760daf9d7ebc4a04bce358aa4e to your computer and use it in GitHub Desktop.
Save danteese/ee6f51760daf9d7ebc4a04bce358aa4e to your computer and use it in GitHub Desktop.
Introduction to Google Form Mapping
/**
* Map function to get all the results from the database
* this is specially useful if you're trying to access to
* the questions and options as you may want to generate
* a report in your system...(not only the answers)
*
* I mainly did this because of the lack of good examples
* on the documentation, only through other github projects
* and try and failure.
*
* I encourage you to use the official docs, but this may
* helps. https://developers.google.com/apps-script/reference/forms
*/
function mapForm() {
var form = FormApp.getActiveForm(); // You may have an active project where you're testing
var items = form.getItems();
var items_json = [];
var accepted_types = ['TEXT', 'SCALE', 'MULTIPLE_CHOICE', 'CHECKBOX', 'GRID', 'LIST']; // Types of Questions (among many others)
for (let i of items) {
var id = type = title = null;
var payload = {};
let _gType = i.getType();
Logger.log('/' + _gType + '/' + i); // Useful log with the type of the question
if (i.getType() == "TEXT") {
i.asTextItem();
}
if (i.getType() == "SCALE") { // https://developers.google.com/apps-script/reference/forms/scale-item
var item = i.asScaleItem();
var lower = item.getLowerBound();
var upper = item.getUpperBound();
var left_label = item.getLeftLabel();
var right_label = item.getRightLabel();
payload = {
"lower": lower,
"upper": upper,
"left": left_label,
"right": right_label
};
}
if (i.getType() == "MULTIPLE_CHOICE" || i.getType() == "CHECKBOX") { // Those are really similiar https://developers.google.com/apps-script/reference/forms/multiple-choice-item
var item = i.getType() == "MULTIPLE_CHOICE" ? i.asMultipleChoiceItem() : i.asCheckboxItem();
var choices = item.getChoices();
var options = [];
for (var j = 0; j < choices.length; j++) {
options.push({
"id": j,
"value": choices[j].getValue(),
})
}
payload['choices'] = options;
}
if (i.getType() == "GRID") {
var item = i.asGridItem();
var columns = item.getColumns();
var rows = item.getRows();
payload = {
"columns": columns,
"rows": rows
};
}
if (i.getType() == "LIST") {
var item = i.asListItem();
var choices = item.getChoices();
var options = [];
for (var j = 0; j < choices.length; j++) {
options.push({
"id": j,
"value": choices[j].getValue(),
})
}
payload['choices'] = options;
}
if (_gType != 'PAGE_BREAK') { // I didn't want to retrieve PAGE_BREAK
id = i.getId();
type = i.getType();
title = i.getTitle();
items_json.push({
"id": id,
"type": type,
"title": title,
"payload": payload
});
}
}
Logger.log(Utilities.jsonStringify(items_json)); // This maybe not work with big JSON but you can send to your system by REST using https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment