How-to Read & Write Google Sheets with React

calvinpak

CP

Posted on June 16, 2020

How-to Read & Write Google Sheets with React

Alt Text

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:

  1. Generate a Google service account key file, get private_key and client_email values.
  2. Get your spreadsheet_id and sheet_id.
  3. Share your spreadsheet with editing permission with the email address in your client_email field.
  4. Make sure the 1st row in your spreadsheet contains the header fields. These values will be the "keys" in your data objects.
  5. 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.
Alt Text
Alt Text

Click on "Credentials" then click on "Manage service accounts", then click on "Create service account".
Alt Text
Alt Text

Add a name and description, then click "Create"
Alt Text

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.
Alt Text

Select the default "JSON" and Google will download a key file to your default download folder.
Alt Text

If you open the JSON file, there are two fields you'll need: "private_key" and "client_email"
Alt Text

2) Go to your Google Sheets and find the spreadsheet_id and sheet_id
Alt Text

3) IMPORTANT Remember to share your spreadsheet with the email address in your key file with the key client_email
Alt Text

You should see the "person added" message right away.
Alt Text

4) Make sure your 1st row contains the column names
Alt Text

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);



Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
calvinpak
CP

Posted on June 16, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related