Skip to content

Instantly share code, notes, and snippets.

@michiomochi
Last active August 29, 2015 14:27
Show Gist options
  • Save michiomochi/5eeb7268d0d0f0b0be83 to your computer and use it in GitHub Desktop.
Save michiomochi/5eeb7268d0d0f0b0be83 to your computer and use it in GitHub Desktop.
google_form_api
function doGet(e) {
var form = FormApp.openById(e.parameter.form_id);
var items = form.getItems();
var result = {form_id: form.getId(), items: []};
for (var i = 0; i < items.length; i++) {
var item = items[i];
var itemId = item.getId();
var itemTitle = item.getTitle();
var itemType = String(item.getType());
switch (itemType) {
case 'TEXT':
var item = item.asTextItem();
break;
case 'MULTIPLE_CHOICE':
var item = item.asMultipleChoiceItem();
break;
case 'LIST':
var item = item.asListItem();
break;
case 'CHECKBOX':
var item = item.asCheckboxItem();
break;
}
var itemChoices = [];
if (itemType !== 'TEXT') {
var choices = item.getChoices();
for (var j = 0; j < choices.length; j++) {
itemChoices[j] = {value: choices[j].getValue()};
}
}
result.items[i] = {id: itemId, title: itemTitle, type: itemType, choices: itemChoices};
}
return ContentService.createTextOutput(JSON.stringify(result));
}
function doPost(e) {
var responseObject = getResponseObject(e.parameters);
var form = FormApp.openById(responseObject.formId);
var itemResponses = [];
for (var i = 0; i < responseObject.responseItems.length; i++) {
var responseItemObject = responseObject.responseItems[i];
var item = form.getItemById(responseItemObject.responseItemId);
var itemType = String(item.getType());
switch (itemType) {
case 'TEXT':
item = item.asTextItem();
break;
case 'MULTIPLE_CHOICE':
item = item.asMultipleChoiceItem();
break;
case 'LIST':
item = item.asListItem();
break;
case 'CHECKBOX':
item = item.asCheckboxItem();
break;
}
itemResponses.push(item.createResponse(responseItemObject.responseItemValues));
}
var formResponse = form.createResponse();
for (var i = 0; i < itemResponses.length; i++) {
formResponse = formResponse.withItemResponse(itemResponses[i]);
}
formResponse.submit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment