Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Last active October 13, 2020 05:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanaikech/49e1e6515225d810e849b3487142a90d to your computer and use it in GitHub Desktop.
Save tanaikech/49e1e6515225d810e849b3487142a90d to your computer and use it in GitHub Desktop.
Retrieving All Values from All Sheets from URL of 2PACX- of Web Published Google Spreadsheet using Node.js

Retrieving All Values from All Sheets from URL of 2PACX- of Web Published Google Spreadsheet using Node.js

This is a sample script for retrieving all values from all sheets from URL of 2PACX- of Web Published Google Spreadsheet using Node.js.

In this post, it supposes that the Google Spreadsheet has already been published for Web. Ref

Before you use this sample script, please install SheetJS js-xlsx.

Flow

The flow of this method is as follows.

  1. Retrieve XLSX data from the URL of web published Google Spreadsheet as the buffer data.
    • The URL is like https://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml.
  2. XLSX data is parsed with SheetJS js-xlsx.
  3. Retrieve all values from all sheets.

Sample script

Please set spreadsheetUrl.

const request = require("request");
const xlsx = require("xlsx");

const spreadsheetUrl =
  "https://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml";

if (
  /https:\/\/docs\.google\.com\/spreadsheets\/d\/e\/2PACX-.+\/pub(html)?/.test(
    spreadsheetUrl
  )
) {
  const temp = spreadsheetUrl.split("/");
  temp.pop();
  const url = temp.join("/") + "/pub?output=xlsx";
  request({ url: url, encoding: null }, (err, res, buf) => {
    if (err) {
      console.error(err);
      return;
    }
    const workbook = xlsx.read(buf, { type: "buffer" });
    const r = Object.entries(workbook.Sheets).map(([k, v]) => ({
      sheetName: k,
      values: xlsx.utils.sheet_to_json(v, { header: 1 }),
    }));
    console.log(r);
  });
}

Result

When above script is run, the following sample value is returned.

[
  {
    "sheetName": "Sheet1",
    "values": [["a1", "b1", "c1"], ["a2", "b2", "c2"], , ,]
  },
  {
    "sheetName": "Sheet2",
    "values": [["a1", "b1", "c1"], ["a2", "b2", "c2"], , ,]
  },
  ,
  ,
]

Reference

SheetJS js-xlsx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment