Note: The "Push to GSheet" service used in this article is no longer active, but you can use Pizzly to host your own version.
I'm a huge fan of Google APIs. I use a lot of them on a daily basis. Whether in my code or through the infinite number of services that rely on it.
Recently, I was running a scraping script and quickly found myself doing some horrible copy/pasting into a Google Sheet. I thought that there should be a better way to dynamically push data into a spreadsheet:
Having a quick look at the Google Sheets API reference, I found an /append
endpoint that would do the work nicely. 30 minutes later, I was sipping lemonade while my script was running in the background π
If I wasn't in love with Google APIs, I'd certainly have moved on to something else. But wait, 20 minutes
to send data to a spreadsheet? Come on! Let's try to make it as simple as searching on Google.
What do we need?
First, understanding the API. As said just before, Google Sheets provides a handy endpoint /append
to push any type of data. All that it asks for, are values formatted as a two-dimensional array
:
const values = [
// First row
["A1","A2","A3"],
// Second row
["B1","B2","B3"]
// Third row...
]
Data will always be appended to the sheet. So if the sheet has some values on row #1, new values will be added on row #2 (and so-on).
Next, handling the authentication. Like most APIs that give access to users' data, the Google Sheets API uses OAuth2. It's an authentication schema that is both very powerful and... complicated to setup.
What's interesting for us, is that each request to the Google Sheets API requires an access_token
- a long string, unguessable, that certifies the request is made on behalf of a user.
Google provides a playground to retrieve one easily. But I'm going to use a service called Pizzly that handles the OAuth-dance securely for me and will handle refresh token, something that the playground doesn't (disclaimer: I helped built Pizzly).
Anything else? We are just missing on which spreadsheet the data shall be appended. That's the spreadsheetID
as per the documentation. Each spreadsheet has a unique ID that we can find by looking into the URL
:
Show me the code!
Wrapping it up, it took 10 lines of code to actually send data to any spreadsheet.
// 1. Run npm install node-fetch
// 2. Import an HTTP client
const fetch = require("node-fetch");
// 3. Declare spreadsheet and values to append
const spreadsheetId = 'SPREADSHEET_ID'
const data = [["firstname", "lastname"], ["John", "Doe"]]
// 4. Send data with a POST request
const baseUrl = "https://pushtogsheet.herokuapp.com";
const query = valueInputOption=RAW&pizzly_pkey=pope8Qy8qfYyppnHRMgLMpQ8MuEUKDGeyhfGCj
;
const url = new URL(/proxy/google-sheets/spreadsheets/</span><span class="p">${</span><span class="nx">spreadsheetId</span><span class="p">}</span><span class="s2">/values/A1:append?</span><span class="p">${</span><span class="nx">query</span><span class="p">}</span><span class="s2">
, baseUrl);
fetch(url.href, {
method: "POST",
body: JSON.stringify({ values: data }),
headers: { 'Pizzly-Auth-Id': 'CONNECT_FIRST' }
})
.then((res) => res.text())
.then(console.log)
.catch(console.error);
How can I use it?
I did my best to make pushing data to gsheet as easy as googling, and crafted a dedicated website for that:
The "Push to GSheet" service used in this article is no longer active, but you can use Pizzly to host your own version.
PS: it's 100% free and open source βοΈ
Top comments (23)
It's even eaiser with Google Forms, if you want the general public to be able to add to the sheet. Just send a request mimicing a form response.
Yes, and I actually did it in my app recently, it's convenient with the form field editor.
But you have to extract the form meta to be able to make a request, and it can only send a request per line.
Also I made a tool to make this process easier too.
link: restful-google-form.vercel.app
Great idea! Works like a charm as well π (edited)
Why wouln't you be able to use it from a server? it works OK for me.
Oh, neat! I have always thought google sheets could be used as a kind of database, but thought it would be a lot harder to integrate with... thanks! I'll definitely try this out.
oh - is getting data out of the spreadsheet just as easy? (I guess once you have the token, then yes?)
Hi Chris! Thanks a lot. Really appreciate ;)
Getting values is just as easy. Replace step 4 with the following and you will be ready to go:
Hello Corentin. I've been looking for something like this for a long time. For fetching the data, your example uses 'gsheet'. Is this another library? I can't quite figure out what to do with it.
I also tried modifying the fetch example using get, but got a CORS error.
Awesome - going to play with that this weekend :)
Really cool work, thanks for sharing! I was dabbling with Google Sheet endpoints as well yesterday and managed to send POST straight to them without any middleman using apps script.
While it modifies the spreadsheet as intented it always returns an error, making it near unusable as you never know when it worked or hit a limit. Seems to be down to some redirect with POST requests, was wondering whether you had any insight?
Hi, this is great. I've got one question however, would you be able to update a specific row, based on a value?
Hi Robert! As far as I know, the Google Sheets API doesn't provide an endpoint to do that easily.
So you have to perform two requests (fetching data, then saving it) and some data manipulation in between.
1. Fetching all the sheet values
2. Updating a cell based on its position in the sheet (A1 notation)
I'll let do the glue between this two functions, depending on your need. Feel free to share your snippet at the end. It might help other readers π
Hey thank you Corentin, that was actually pretty helpful. Here's a quick implementation of mine:
Awesome Robert! Thanks a lot for sharing
Hi Corentin, sorry if this is any bother but after working with this for a little bit more, I found out that this only works with spreadsheets created specifically from your app, if I create one manually myself it returns a 404. Tested with multiple spreadsheet IDs that I created.
Hi Robert! That's 100% true. This is due to the OAuth scope used by this project.
I'm using googleapis.com/auth/drive.file which grants "per-file access to files created or opened by the app". I had to do so, because Google has recently limited developers access to Google customers data. To avoid an important audit process (that would have costed me thousands of dollars), I went with the minimal scope required.
Have a look more deeply into Google Sheets scopes here: developers.google.com/sheets/api/g... - and if you want to go over that restriction, feel free to create your own integration with Google Sheets by creating an account on Bearer.sh
Bearer has a free plan with up to 100K API calls / month.
I was looking for this. π€©π₯π₯π₯
This is pretty cool, Corentin!
I've had ideas for a few tools that would make working life easier for me and my colleagues, and this solves one of many little problems!
Thanks a lot! I hope you gonna enjoy using it βοΈ
This is so cool corentin.
In theory, could it mean that you can populate linkedin jobs as they are posted, to the g ss?
Or is it limited to just names?
Hi Meena! You can pass whatever data you want. It just has to be formatted as a two-dimensional
array
:Happy coding π©βπ»
Hello!
I was looking for a post like this!
how do you get 'A_BEARER_AUTH_ID'?
Nvm i found it :) thanks a lot
I want to send data to Google Sheets using Vue (well, Quasar) and I'm having a heck of a time finding the right syntax. Have you done this with Vue?