Skip to content

Instantly share code, notes, and snippets.

@MichalKalita
Last active September 22, 2020 14:44
Show Gist options
  • Save MichalKalita/f232b473280d692fa56d8ba3717323f9 to your computer and use it in GitHub Desktop.
Save MichalKalita/f232b473280d692fa56d8ba3717323f9 to your computer and use it in GitHub Desktop.
Write to Google Sheet with REST API

Write data to Google Sheet with REST API

Setup

  1. Open your Google Sheet document
  2. Tools -> Script editor
  3. Copy the code, edit token value
  4. Save
  5. Publish -> as web application -> project version new, execute app as me, anyone should have access
  6. Confirm
  7. Copy URL

How it works

Call POST request to url?token=yourtoken Request body must contains data in object {key1: "a", key2: "b"} It will create new row and fill the data from object, the names of keys are ignored, only values are copied

Enjoy ;)

function doPost(e) {
if(e.parameter.token !== "--- use some random text here ---") {
return ContentService.createTextOutput(JSON.stringify({error: "bad token"})).setMimeType(ContentService.MimeType.JSON);
}
var params = JSON.parse(e.postData.contents);
SpreadsheetApp.getActive().getSheets()[0].appendRow(Object.values(params));
return ContentService.createTextOutput(JSON.stringify(e)).setMimeType(ContentService.MimeType.JSON);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment