Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Save respondents answers only
function saveMessage(webhookMessageResult) {
if (webhookMessageResult.type === 'user-answers') {
for (const question of webhookMessageResult.value.data) {
let sheet = SpreadsheetApp.getActiveSheet();
let lastRow = Math.max(sheet.getLastRow(), 1);
sheet.insertRowAfter(lastRow);
let nowDate = new Date();
sheet.getRange(lastRow + 1, 1).setValue(nowDate);
sheet.getRange(lastRow + 1, 2).setValue(webhookMessageResult.value.userName);
sheet.getRange(lastRow + 1, 3).setValue(question.question);
sheet.getRange(lastRow + 1, 4).setValue(question.answer);
SpreadsheetApp.flush();
}
}
}
//this is a function that fires when the webapp receives a GET request
function doGet(e) {
return HtmlService.createHtmlOutput("request received");
}
//this is a function that fires when the webapp receives a POST request
function doPost(data) {
if (
data &&
data.postData &&
data.postData.contents
) {
let webhookMessage = JSON.stringify(data.postData.contents);
while (typeof webhookMessage === 'string') {
webhookMessage = JSON.parse(webhookMessage);
}
if (Array.isArray(webhookMessage.result)) {
for (const result of webhookMessage.result) {
saveMessage(result);
}
} else {
saveMessage(webhookMessage.result);
}
}
return HtmlService.createHtmlOutput("post request received");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment