Skip to content

Instantly share code, notes, and snippets.

@owyongsk
Last active June 6, 2018 04:35
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 owyongsk/cf36ae0dc9fdb743ae70cb39fe164f8d to your computer and use it in GitHub Desktop.
Save owyongsk/cf36ae0dc9fdb743ae70cb39fe164f8d to your computer and use it in GitHub Desktop.
A Google Form which sends all the data, including attachment to a URL of your choice via webhooks.
/*
* Inspired by https://gist.github.com/lifehome/db101c66fe547a8ffbffe10d1bc0b426
*
* Please keep the source code updated with this gist at
* https://gist.github.com/owyongsk/cf36ae0dc9fdb743ae70cb39fe164f8d
*
* There's no way to have a gist owned by an organization
*/
var webhook_url = "https://your.own/webhook/path";
var webhook_key = "r@nd0mSt1ng";
function onFormSubmit(e) {
var data = {
"response": {
"id": e.response.getId(),
"timestamp": e.response.getTimestamp(),
"data": e.response.getItemResponses().map(function(item) { // Maps itemResponses into an array of obj with keys: "key and val"
return {
key: item.getItem().getTitle(),
val: item.getItem().getTitle() == "Receipt" ? getFileBlobsWithFileName(item.getResponse()) : item.getResponse()
}
}, this).reduce(function(obj,i){ // Squeezes into one single object
obj[i.key] = i.val;
return obj;
}, {"Email": e.response.getRespondentEmail(), "entropy": Utilities.getUuid()}) // Starting with email and randomly generated entropy as keys
}
};
var string_data = JSON.stringify(data);
var options = {
method: "post",
headers: {"Authorization": computeDigestString(string_data, webhook_key)},
payload: string_data,
contentType: "application/json; charset=utf-8",
};
UrlFetchApp.fetch(webhook_url, options);
};
function getFileBlobsWithFileName(files) {
return files.map(function(f) {
var file = DriveApp.getFileById(f);
return {
file_name: file.getName(),
mime_type: file.getMimeType(),
base64_blob: Utilities.base64Encode(file.getBlob().getBytes())
}
}, this);
}
function computeDigestString(payload, secret) {
var byteSignature = Utilities.computeHmacSha256Signature(payload, secret);
return Utilities.base64Encode(byteSignature);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment