Skip to content

Instantly share code, notes, and snippets.

@jptrsn
Last active September 11, 2020 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jptrsn/7cb4159ae63b5936bfbd to your computer and use it in GitHub Desktop.
Save jptrsn/7cb4159ae63b5936bfbd to your computer and use it in GitHub Desktop.
/* script example to retrieve question IDs from form questions, useful for building pre-filled URLs for form entry.
* Copyright 2015, James Petersen
* Published under a Creative Commons Attribution - NonCommercial ShareAlike 4.0 International Licence
*/
function getFormQuestions() {
var form = FormApp.openById(REF_FORM_ID);
var items = form.getItems();
var currentItem;
var validationRules = [];
var questionHeaders = [[],[],[],[]];
for (i in items) {
var type = items[i].getType();
questionHeaders[0].push(items[i].getTitle());
questionHeaders[2].push(type);
switch (type) {
case FormApp.ItemType.CHECKBOX:
currentItem = items[i].asCheckboxItem();
var url = form.createResponse().withItemResponse(currentItem.createResponse([currentItem.getChoices()[0].getValue()])).toPrefilledUrl();
var rule = buildChoiceRule(currentItem.getChoices());
validationRules.push([i,rule]);
break;
case FormApp.ItemType.DATE:
currentItem = items[i].asDateItem();
var url = form.createResponse().withItemResponse(currentItem.createResponse(new Date('2015-01-01'))).toPrefilledUrl();
var rule = SpreadsheetApp.newDataValidation().requireDate().build();
validationRules.push([i,rule]);
break;
case FormApp.ItemType.DATETIME:
currentItem = items[i].asDateTimeItem();
var url = form.createResponse().withItemResponse(currentItem.createResponse(new Date('2015-01-01'))).toPrefilledUrl();
validationRules.push([i,rule]);
break;
case FormApp.ItemType.DURATION:
break;
case FormApp.ItemType.GRID:
break;
case FormApp.ItemType.IMAGE:
break;
case FormApp.ItemType.LIST:
currentItem = items[i].asListItem();
var url = form.createResponse().withItemResponse(currentItem.createResponse(currentItem.getChoices()[0].getValue())).toPrefilledUrl();
var rule = buildChoiceRule(currentItem.getChoices());
validationRules.push([i,rule]);
break;
case FormApp.ItemType.MULTIPLE_CHOICE:
currentItem = items[i].asMultipleChoiceItem();
var url = form.createResponse().withItemResponse(currentItem.createResponse(currentItem.getChoices()[0])).toPrefilledUrl();
var rule = buildChoiceRule(currentItem.getChoices());
validationRules.push([i,rule]);
break;
break;
case FormApp.ItemType.PAGE_BREAK:
break;
case FormApp.ItemType.PARAGRAPH_TEXT:
currentItem = items[i].asParagraphTextItem();
var url = form.createResponse().withItemResponse(currentItem.createResponse('text')).toPrefilledUrl();
break;
case FormApp.ItemType.SCALE:
break;
case FormApp.ItemType.SECTION_HEADER:
break;
case FormApp.ItemType.TEXT:
currentItem = items[i].asTextItem();
var url = form.createResponse().withItemResponse(currentItem.createResponse('text')).toPrefilledUrl();
break;
case FormApp.ItemType.TIME:
break;
}
var startPos = url.indexOf('?')+1; // This will include the entry. in the ID
// var startPos = url.indexOf('entry.')+6; // This will include only the ID number
var endPos = url.indexOf('=') - startPos;
var entry = url.substr(startPos,endPos);
questionHeaders[1].push(entry);
if (currentItem.isRequired()) {
questionHeaders[3].push(true);
} else {
questionHeaders[3].push(false);
}
}
questionHeaders[0].push('Submission Status');
questionHeaders[0].push('URL');
questionHeaders[1].push('Required for script');
questionHeaders[1].push('');
questionHeaders[2].push('Do not delete');
questionHeaders[2].push('');
applyValidation(validationRules);
return questionHeaders;
Logger.log('test');
}
// Create a spreadsheet validation rule based on the choices for a form question response
function buildChoiceRule(choices) {
var choiceArray = [];
for (j in choices) {
choiceArray.push(choices[j].getValue());
}
var rule = SpreadsheetApp.newDataValidation().requireValueInList(choiceArray).build();
return rule;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment