Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export JavaScript object to Google Sheet using NodeJs

I have a JavaScript object like this

[{ a: "123", b: "456" }, { a: "321", b: "654" }]

Is there any way to export it to Google Sheet like this using NodeJs

enter image description here

like image 240
BlackLotus Avatar asked Feb 12 '26 02:02

BlackLotus


1 Answers

I believe your goal is as follows.

  • You want to put the values of [{ a: "123", b: "456" }, { a: "321", b: "654" }] to the Spreadsheet using Node.js.

In this case, how about using google-spreadsheet?

Usage:

1. Install google-spreadsheet.

You can see it at here.

2. Authorization

I thought that in this case, the service account might be useful.

You can see the method for authorization at here.

3. Sample script:

const { GoogleSpreadsheet } = require("google-spreadsheet");

const creds = require("credential.json"); // Please set your credential file.

const doc = new GoogleSpreadsheet("##"); // Please set your Spreadsheet ID.

const sample = async () => {
  await doc.useServiceAccountAuth(creds);
  await doc.loadInfo();
  const worksheet = doc.sheetsByIndex[0]; // Here, 1st tab on Google Spreadsheet is used.

  // This is from your sample value.
  const values = [
    { a: "123", b: "456" },
    { a: "321", b: "654" },
  ];
  await worksheet.setHeaderRow(["a", "b"]); // This is the header row.
  await worksheet.addRows(values); // Your value is put to the sheet.
};

sample();
  • When this script is run, the result in your sample image is obtained.

References:

  • google-spreadsheet
  • document of google-spreadsheet
like image 144
Tanaike Avatar answered Feb 13 '26 17:02

Tanaike



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!