How-to Read & Write Google Sheets with React
CP
Posted on June 16, 2020
I want to read and write Google Sheets with React to update the data for an app I built with Glideapps, a no-code app builder that uses Google Sheets as the data source.
It took me quite a while to figure out how to do that. But turns out the solution is quite straight-forward.
TL;DR:
- Generate a Google service account key file, get
private_key
andclient_email
values.- Get your
spreadsheet_id
andsheet_id
.- Share your spreadsheet with editing permission with the email address in your
client_email
field.- Make sure the 1st row in your spreadsheet contains the header fields. These values will be the "keys" in your data objects.
- Install google-spreadsheet and use it to connect to Google Sheets. There are very good examples in the docs here, and I have included a snippet of how to append to the spreadsheet at the bottom of this post.
Here are the detailed step-by-step instructions with screenshots:
1) Go to Google's developer console:
https://console.developers.google.com/
Click on "Select a project" then click on "New project", give it a name.
Click on "Credentials" then click on "Manage service accounts", then click on "Create service account".
Add a name and description, then click "Create"
Click "Continue" on the "Service account permissions" page, as it is optional.
Click "Done" on "Grant users access to this service account" page, as it is also optional.
Click "Create key" under the Actions column.
Select the default "JSON" and Google will download a key file to your default download folder.
If you open the JSON file, there are two fields you'll need: "private_key" and "client_email"
2) Go to your Google Sheets and find the spreadsheet_id
and sheet_id
3) IMPORTANT Remember to share your spreadsheet with the email address in your key file with the key client_email
You should see the "person added" message right away.
4) Make sure your 1st row contains the column names
5) There are very good examples on Googe-Spreadsheet for reading and writing to the Spreadsheet.
Here is a snippet for appending a new row to the spreadsheet I wrote:
import { GoogleSpreadsheet } from "google-spreadsheet";
// Config variables
const SPREADSHEET_ID = process.env.REACT_APP_SPREADSHEET_ID;
const SHEET_ID = process.env.REACT_APP_SHEET_ID;
const CLIENT_EMAIL = process.env.REACT_APP_GOOGLE_CLIENT_EMAIL;
const PRIVATE_KEY = process.env.REACT_APP_GOOGLE_SERVICE_PRIVATE_KEY;
const doc = new GoogleSpreadsheet(SPREADSHEET_ID);
const appendSpreadsheet = async (row) => {
try {
await doc.useServiceAccountAuth({
client_email: CLIENT_EMAIL,
private_key: PRIVATE_KEY,
});
// loads document properties and worksheets
await doc.loadInfo();
const sheet = doc.sheetsById[SHEET_ID];
const result = await sheet.addRow(row);
} catch (e) {
console.error('Error: ', e);
}
};
const newRow = { Name: "new name", Value: "new value" };
appendSpreadsheet(newRow);
Posted on June 16, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.