Skip to content

Instantly share code, notes, and snippets.

@standuply
Last active March 9, 2022 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save standuply/3bd6e682296c343d617b245418c6afbd to your computer and use it in GitHub Desktop.
Save standuply/3bd6e682296c343d617b245418c6afbd to your computer and use it in GitHub Desktop.
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