Skip to content

Instantly share code, notes, and snippets.

@Yizack
Created March 25, 2022 11:31
Show Gist options
  • Save Yizack/6ef8abf59b69ff880ceda433c87cb1ba to your computer and use it in GitHub Desktop.
Save Yizack/6ef8abf59b69ff880ceda433c87cb1ba to your computer and use it in GitHub Desktop.
Script to deploy a JSON service on Google Apps Script from all the responses of a Google Forms / Script para implementar un servicio JSON en Google Apps Script a partir de todas las respuestas de un formulario de Google Forms
function getSubmissions() {
var form = FormApp.openById('YOUR-FORM-ID'); // Paste your form ID here
var submissions = [];
var formResponses = form.getResponses();
// Loop through all responses
for (var i = 0; i < formResponses.length; i++) {
var formResponse = formResponses[i];
var itemResponses = formResponse.getItemResponses();
// Get timestamp of response
var timestamp = formResponse.getTimestamp();
var time_options = {year: 'numeric', month: 'long', day: 'numeric', hourCycle: 'h23', hour:'2-digit', minute:'2-digit', second:'2-digit', timeZone: 'America/Panama',timeZoneName: 'short' };
timestamp = timestamp.toLocaleDateString('en-US', time_options);
var item = [];
var obj = {};
// Loop through all questions
for (var j = 0; j < itemResponses.length; j++) {
var id = "question_" + (j + 1);
obj[id] = itemResponses[j].getResponse();
if (j == itemResponses.length-1){
item.push(obj);
}
}
obj['timestamp'] = timestamp; // Add timestamp
submissions.push(item[0]); // Push all question responses
}
return submissions;
}
function doGet() {
var content = getSubmissions();
return ContentService.createTextOutput(JSON.stringify(content, null, 2)).setMimeType(ContentService.MimeType.JSON);
// Deploy as JSON
}
/* Output example
[
{
"question_1": "RESPONSE 1",
"question_2": "RESPONSE 2",
"question_3": "RESPONSE 3",
"timestamp": "March 25, 2022, 07:52:35 EST"
},
{
"question_1": "RESPONSE 1",
"question_2": "RESPONSE 2",
"question_3": "RESPONSE 3",
"timestamp": "March 25, 2022, 17:41:04 EST"
},
...
]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment