Last active
December 10, 2022 16:16
-
-
Save TheArmagan/e3beeec765fb7f691588632e338996dd to your computer and use it in GitHub Desktop.
[NEW] Google Forms Send Post Request On Submit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const FETCH_POST_URL = "http://requestbin.net/r/16zlov01"; | |
function onSubmit(e) { | |
let currentForm = FormApp.getActiveForm(); | |
let allFormResponses = currentForm.getResponses(); | |
let lastestResponse = allFormResponses[allFormResponses.length - 1]; | |
let formResponses = lastestResponse.getItemResponses(); | |
let _formId = lastestResponse.getId(); | |
let _formTitle = currentForm.getTitle(); | |
let _formIsQuiz = currentForm.isQuiz(); | |
let _formSubmitDate = lastestResponse.getTimestamp(); | |
let result = { | |
id: _formId, | |
title: _formTitle, | |
quizInfo: { | |
isQuiz: _formIsQuiz, | |
results: { | |
max: 0, | |
current: 0 | |
} | |
}, | |
results: [], | |
submitDate: _formSubmitDate | |
}; | |
for (var i = 0; i < formResponses.length; i++) { | |
let response = formResponses[i]; | |
let responseItem = response.getItem(); | |
let _id = responseItem.getId(); | |
let _title = responseItem.getTitle(); | |
let _result = response.getResponse(); | |
let _helpText = responseItem.getHelpText(); | |
let _type = responseItem.getType(); | |
let _castedItem = _castFormItem(responseItem, _type); | |
let _points = undefined; | |
let _maxPoints = undefined; | |
if (_castedItem && typeof _castedItem.getPoints == "function") { | |
let gradableResponseItem = lastestResponse.getGradableResponseForItem(responseItem); | |
_maxPoints = _castedItem.getPoints(); | |
_points = gradableResponseItem.getScore(); | |
} | |
let r = { | |
id: _id, | |
title: _title, | |
result: _result, | |
score: { | |
max: -1, | |
current: -1 | |
}, | |
type: _type, | |
helpText: _helpText || "No help text found." | |
} | |
if (typeof _maxPoints != "undefined") { | |
result.quizInfo.results.max += _maxPoints; | |
r.score.max = _maxPoints; | |
} | |
if (typeof _points != "undefined") { | |
result.quizInfo.results.current += _maxPoints; | |
r.score.current = _points; | |
} | |
result.results.push(r); | |
}; | |
UrlFetchApp.fetch(FETCH_POST_URL, { | |
method: "POST", | |
contentType: "application/json", | |
payload: JSON.stringify(result) | |
}); | |
Logger.log("PAYLOAD_TEXT:"); | |
Logger.log(JSON.stringify(result, null, 2)); | |
} | |
function _castFormItem(item, itemType) { | |
if (itemType === FormApp.ItemType.CHECKBOX) { | |
return item.asCheckboxItem(); | |
} | |
if (itemType === FormApp.ItemType.DATE) { | |
return item.asDateItem(); | |
} | |
if (itemType === FormApp.ItemType.DATETIME) { | |
return item.asDateTimeItem(); | |
} | |
if (itemType === FormApp.ItemType.DURATION) { | |
return item.asDurationItem(); | |
} | |
if (itemType === FormApp.ItemType.LIST) { | |
return item.asListItem(); | |
} | |
if (itemType === FormApp.ItemType.MULTIPLE_CHOICE) { | |
return item.asMultipleChoiceItem(); | |
} | |
if (itemType === FormApp.ItemType.PARAGRAPH_TEXT) { | |
return item.asParagraphTextItem(); | |
} | |
if (itemType === FormApp.ItemType.SCALE) { | |
return item.asScaleItem(); | |
} | |
if (itemType === FormApp.ItemType.TEXT) { | |
return item.asTextItem(); | |
} | |
if (itemType === FormApp.ItemType.TIME) { | |
return item.asTimeItem(); | |
} | |
if (itemType === FormApp.ItemType.GRID) { | |
return item.asGridItem(); | |
} | |
if (itemType === FormApp.ItemType.CHECKBOX_GRID) { | |
return item.asCheckboxGridItem(); | |
} | |
if (itemType === FormApp.ItemType.PAGE_BREAK) { | |
return item.asPageBreakItem(); | |
} | |
if (itemType === FormApp.ItemType.SECTION_HEADER) { | |
return item.asSectionHeaderItem(); | |
} | |
if (itemType === FormApp.ItemType.VIDEO) { | |
return item.asVideoItem(); | |
} | |
if (itemType === FormApp.ItemType.IMAGE) { | |
return item.asImageItem(); | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment