DEV Community


How-to Read & Write Google Sheets with React

calvinpak profile image CP ・3 min read

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.


  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:

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 SHEET_ID = process.env.REACT_APP_SHEET_ID;

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" };


Enter fullscreen mode Exit fullscreen mode

Discussion (11)

Editor guide
azzmimy profile image
azzmi-my • Edited

Y'all guys gonna come to an issue,

error - ./node_modules/google-auth-library/build/src/auth/googleauth.js:17:0
Module not found: Can't resolve 'child_process'

You must do this inside your next.config.js to make everything work

module.exports = {
  // other next config
  i18n: {
    locales: ["en", "ms"],
    defaultLocale: "en",
  env: {
     SPREADSHEET_ID : ***,
     SHEET_ID : ***,
     CLIENT_EMAIL : ***,
     PRIVATE_KEY : ***,

  webpack: (config, options) => {
    config.node= {
    // Some libraries import Node modules but don't use them in the browser.
    // Tell Webpack to provide empty mocks for them so importing them works.
    fs: 'empty',
    child_process : 'empty',
    net : 'empty',
    tls: 'empty',

    return config
Enter fullscreen mode Exit fullscreen mode

ignore the other settings and focus on the webpack part.

snikidev profile image
Nikita Kakuev

Magic! Thanks man! :)

sparcbr profile image

Thanks for the screenshots with arrows

jotarenan profile image
João Pedro Renan

Nice post. A question: do I have to Publish the spreadheet on the Web in order for google-spreadsheet to be able to access it?

calvinpak profile image
CP Author

You only need to share the spreadsheet with your google app. ^_^

jotarenan profile image
João Pedro Renan

I'm having the same problem this user describes on StackOverflow. Apparently he was able to fix it, but his solution didn't work for me. Would you have any clue of what may be going on?

Thread Thread
saifrehman99 profile image
Saif ur Rehman

Hi Joao,

Try this,
process.env.PRIVATE_KEY.replace(/\n/g, '\n')

Also enable the google sheets api in google console.
Hope it works for you !

saifrehman99 profile image
Saif ur Rehman • Edited

Hi guys,

If you having some issues with the permission, Just go to google console and Enable the google sheets API.

Also, while using your private key, add this piece of code

process.env.PRIVATE_KEY.replace(/\n/g, '\n')

mchlkucera profile image
gautham495 profile image
Gautham Vijayan

Top notch Stuff!!

harsh2201 profile image
Harsh Jobanputra

This is very helpful. Thank you!