Skip to content

Instantly share code, notes, and snippets.

@qoobaa
Last active June 15, 2022 15:37
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 qoobaa/5938bafd207e29fde46b80dc2398b9dc to your computer and use it in GitHub Desktop.
Save qoobaa/5938bafd207e29fde46b80dc2398b9dc to your computer and use it in GitHub Desktop.
var SHEET_ID = "17lRx0wgx9f2JutGKKFfkHmbrkqgsy7nf6IDJL07w-Vs";
var SHEET_NAME = "downloads";
function doGet(event) {
return handleResponse(event);
}
function doPost(event) {
return handleResponse(event);
}
function handleResponse(event) {
var lock = LockService.getPublicLock();
lock.waitLock(30000);
try {
var result = {status: "ok"};
var sheet = SpreadsheetApp
.openById(SHEET_ID)
.getSheetByName(SHEET_NAME);
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
var currentRowNumber = sheet.getLastRow() + 1;
var currentRow = [];
for (var i in headers) {
var header = headers[i];
if (header === "timestamp") {
// automatically insert a new date into the first column
currentRow.push(new Date());
} else {
currentRow.push(event.parameter[header] || "");
}
}
sheet.getRange(currentRowNumber, 1, 1, currentRow.length).setValues([currentRow]);
} catch (error) {
result = {status: "error", error: error};
} finally {
lock.releaseLock();
}
return ContentService
.createTextOutput(JSON.stringify(result))
.setMimeType(ContentService.MimeType.JSON);
}
@qoobaa
Copy link
Author

qoobaa commented Jun 14, 2018

  1. Create a new Google Spreadsheet. It may be required to use a normal (non-company) account - I wasn't able to create a publicly available endpoint when the spreadsheet was on …@jah.pl, it worked flawlessly on …@gmail.com.
  2. Adjust the spreadsheet ID in line #1 (when you open the spreadsheet, copy the "1bK3jI9TL5T8Tf0CmlVHpIlhg6Mu2-qJXDCmmip7rzGA" part from the URL - https://docs.google.com/spreadsheets/d/1bK3jI9TL5T8Tf0CmlVHpIlhg6Mu2-qJXDCmmip7rzGA/edit).
  3. Adjust the sheet name to e.g. "downloads" (line #2).
  4. Add following colums (the first row):
    timestamp | firstname | lastname | email | subscribe | country | organization | sector | explanation
  5. Go to Tools -> Script Editor.
  6. Paste the above script there.
  7. Go to File -> Manage versions, save a new version.
  8. Go to Publish -> Deploy as a web application, ensure to change permissions to anonymous access.
  9. Adjust the endpoint URL in the code with the generated address.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment