Google spreadsheet as api rest using apps script

JRichardsz.java
4 min readNov 24, 2019

--

If your answers are: yes, yes and yes. Follow these steps in order to publish your google sheet data as rest api ready to consume from anywhere!

Steps

  • Create a google sheet with this data:
  • Save the script with some name like: sheet-as-api
  • Scrip needs authorizations. To prompt them, click on “run” option and select testFindAll() function. Accept the requested permissions.
  • You should see the consent popup. Click on review permissions
  • Select your account. In the next script ensure that is your mail and press Go to… (unsafe) and then allow. Basically google is asking you if you trust in the developer. But the developer is you :b
  • The previous steps just was to allow the script. Then press run again. You should see a log like this:
  • This test retrieve all the available data in the sheet as json. If you don’t see this, there is an error. Feel free to ask me ;)
  • Click Deploy >> New Deployment in the upper right corner
  • Select type: web app
  • Enter a description, select your mail and choose anyone. Then press deploy
  • Previous version looked like this
  • After that, you will see the url of your web app. Copy that and save it
  • Previous version looked like this

FindAll Test

Just perform a simple get invocation to the url of your deployed api

Response will be something like:

Simple Query Test

Just perform a simple post to the url of your deployed script

  • http method: get
  • api url : https://script.google.com/macros/s/AK**6j0SpZF4/exec
  • token: your security token
  • operation: simpleQuery
  • field1: some column of your sheet. In the example is name
  • comparator: Any simple math javascript operator. In this example ==
  • field2: Is the expected value related to the entered field1. Don’t forget to quote if it is a string: ‘Richard’

Response will be something like:

That’s all. You have a simple, secure and ready to use rest api to be consumed for backends like android, ios, etc. Don’t forget, just for tests!!

Roadmap

  • test it on javascript client side web apps
  • add crud operations

--

--

JRichardsz.java
JRichardsz.java

Written by JRichardsz.java

Programmer born in vulcan who searches for his internal properties file to perform an overclock in his brain and body. https://stackoverflow.com/users/3957754

No responses yet