A Cloud Function to automate Google Spreadsheet CSV import
Update: there is now a step-by-step codelab for this post.
Sure, you can paste or import CSV content to Google Sheets but wouldn’t it be nice to automate the process and thus be able to update a sheet with the content of a file as it’s uploaded to a bucket? This would ensure that you can analyze data (maybe produced by another team) in a spreadsheet as soon as it’s available :
In this post I’ll walk you through how to write a Cloud Function that reacts to a CSV file upload to a Cloud Storage bucket and then updates one of your spreadsheets with that CSV data using the Google Sheets API.
This is what the implementation looks like :
Just a bit of (security) setup
First, let’s create a new Sheets document. Once created, remember its identifier; it will be used as an environment variable for the function we’ll write :
In the “IAM & admin” section, navigate to “Service accounts” and note the Email for the App Engine default service account. It should be of the form
email@example.com. Of course you can also create your own service account dedicated to this action.
Now simply grant this service account edit privileges to your spreadsheet :
At this point a Cloud Function configured to use this service account will be able to request write access to this spreadsheet document.
Create the Cloud Function
We can now create a Cloud Function called
function2sheet that is triggered on file uploads to a specific Cloud Storage bucket. The code will be written in Node.js 8 with async functions.
Change the function body to use the Cloud Storage and Sheets APIs and mark the function as
The use of
async here is required to use
await as we’ll see in a moment.
A couple of important options for this function include :
- the service account used (which should be the same as discussed above)
- an environment variable called
SPREADSHEET_IDthat should match the sheet document that you have created previously :
As a final setup step, here is the
package.json content with the Cloud Storage and Google Sheet APIs as the two dependencies we’ll use :
Once you’ve configured everything as described, go ahead, click “Create Function” !
If you’d rather jump straight to the full code, it’s available here. Otherwise, follow along!
Before we code any further we need to block on the creation of a Google Client API with the proper Storage and Sheet scopes (remember, this is part of an
async function) :
From there we can create a Sheets API client :
Create the empty sheet
With a Sheets API client we can create a simple new sheet in our document but before we go any further, here’s a quick note on vocabulary:
- a spreadsheet is the actual document and is referenced by its identifier (discussed above and visible in the document URL)
- a sheet is one of the tabs in the document and it can be referenced by its name (the tab name) or an identifier generated upon sheet creation
With this in mind, here’s a function using the Sheets API client to create an empty sheet at position 2 (typically after the default “Sheet1”), with 26 columns, 2000 rows, with the first row frozen :
Note how instead of hard-coding the reference to the spreadsheet we rely on the previously-created
SPREADSHEET_ID environment variable.
We need to remember the
sheetId for further requests made to this particular sheet. Also, the sheet name needs to be unique and the creation will fail if there is already a sheet called
batchUpdate function in the Sheets API is a common way to interact with documents and is described here.
Read data from a storage CSV file
Now that we have somewhere to dump our data, let’s go and grab the actual data from the file that was just uploaded and store it in an array. Here we assume that the content is indeed comma-separated which means that undefined things will happen if it’s not :
Populate newly created sheet
It’s now time to populate the sheet we’ve created using the same Sheet client API and the data we’ve just collected :
Note that we define a range to edit that matches the size of the sheet created. This means that content that exceeds 26 columns will fail with this particular code.
If all goes well, at this point you should now have the CSV content in your spreadsheet!
Adding the pretty touches (optional)
If we assume that the first row is made up of headers we can add some formatting to the process, such as changing the font size of that row and making it bold :
We could similarly resize the columns in the same
batchUpdate call, an exercise left to the reader !
Putting it all together and testing the flow
The calls to the functions we’ve just discussed can be made as follows in the
function2sheet function: block on the creation of the new sheet and then on the data retrieval from Cloud Storage. Then populate the sheet with the data and modify the sheet’s style:
Once everything is in place, simply upload a CSV file to the right bucket and watch your spreadsheet be updated with a new sheet with the file’s content.
Try uploading several files to the bucket to see what happens!
The complete function source code is available here.