Last active
March 9, 2022 13:07
-
-
Save standuply/3bd6e682296c343d617b245418c6afbd to your computer and use it in GitHub Desktop.
Save respondents answers only
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
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