Code Monkey home page Code Monkey logo

public-google-sheets-parser's Introduction

Public Google Sheets Parser

Author checks npm package codecov license JavaScript Style Guide Hits GitHub stars downloads JSDelivr CDN

Introduction

Introduction

The Public Google Sheets Parser is a zero-dependency library that enables the use of publicly shared Google Sheets as a data source, akin to a database. Ensure your Google Sheet is public and formatted correctly with headers in the first row for seamless integration.

Features:

  • Sheet Name or GID Selection: Fetch data from specific sheets by name or GID (since v1.1.0 and v1.3.0 respectively).
  • Formatted Dates: While you can opt to retrieve dates in their displayed format within the spreadsheet with useFormattedDate (since v1.4.0), it is recommended to use the useFormat option available since v1.5.0 for more precise control and accuracy. The useFormat option ensures that both numeric and date values are returned in their formatted string representations as they appear in your Google Sheets, providing a more accurate and consistent result.
  • Custom Formatting: Leverage useFormat to get numeric and date values as formatted in Google Sheets (since v1.5.0).
  • Browser and Node.js Support: Utilize in various environments though note it requires Fetch API compatibility.
  • API Access: No API key required for the SDK; access data through the provided free API for public sheets.

Installation

yarn add public-google-sheets-parser
# OR
npm i public-google-sheets-parser

Usage

Node.js:

const PublicGoogleSheetsParser = require('public-google-sheets-parser')
const spreadsheetId = 'your_spreadsheet_id_here'
const parser = new PublicGoogleSheetsParser(spreadsheetId)

parser.parse().then(console.log)

Browser:

<script src="https://cdn.jsdelivr.net/npm/public-google-sheets-parser@latest"></script>
<script>
  const parser = new PublicGoogleSheetsParser('your_spreadsheet_id_here')
  parser.parse().then(data => console.log(data))
</script>

Vue v2:

<template>
  <div>
    <ul v-if="items.length">
      <li v-for="(item, index) in items" :key="index">{{ item }}</li>
    </ul>
  </div>
</template>

<script>
import PublicGoogleSheetsParser from 'public-google-sheets-parser'

export default {
  data() {
    return {
      items: [],
    }
  },
  mounted() {
    const parser = new PublicGoogleSheetsParser('your_spreadsheet_id_here')
    parser.parse().then(data => {
      this.items = data
    })
  },
}
</script>

React:

import React, { useState, useEffect } from 'react'
import PublicGoogleSheetsParser from 'public-google-sheets-parser'

const SpreadsheetData = () => {
  const [items, setItems] = useState([])

  useEffect(() => {
    const parser = new PublicGoogleSheetsParser('your_spreadsheet_id_here')
    parser.parse().then(data => {
      setItems(data)
    })
  }, [])

  return (
    <div>
      <ul>
        {items.map((item, index) => (
          <li key={index}>{JSON.stringify(item)}</li>
        ))}
      </ul>
    </div>
  )
}

export default SpreadsheetData

Options and Configurations

  • useFormattedDate: Although you can parse date values according to the spreadsheet's format using useFormattedDate, it is now recommended to use the useFormat option for more comprehensive and precise formatting control. The useFormat option not only affects dates but also applies to numeric values, ensuring consistency and accuracy across your data.

  • useFormat: Get data as formatted in the spreadsheet (applies to numbers and dates).

  • Specify sheet by name or GID to target specific data ranges.

Example with Options:

const options = { sheetName: 'Sheet4', useFormat: true }
const parser = new PublicGoogleSheetsParser('10WDbAPAY7Xl5DT36VuMheTPTTpqx9x0C5sDCnh4BGps', options)
parser.parse().then((data) => {
  // data will be like below:
  // [
  //   {
  //     date: '2024년 1월 1일 월요일 오전 12시 0분 0초',
  //     'with-format': '₩2,000.00',
  //     'without-format': '5678'
  //   },
  //   {
  //     date: '2024년 12월 1일 일요일 오전 12시 0분 0초',
  //     'with-format': '₩2,000.00',
  //     'without-format': '1234'
  //   }
  // ]
})

parser.setOption({ useFormat: false })
parser.parse().then((data2) => {
  // data2 will be like below:
  // [
  //   {
  //     date: 'Date(2024,0,1,0,0,0)',
  //     'with-format': 2000,
  //     'without-format': 5678
  //   },
  //   {
  //     date: 'Date(2024,11,1,0,0,0)',
  //     'with-format': 2000,
  //     'without-format': 1234
  //   }
  // ]
})

License

This project is licensed under the MIT License - see the LICENSE file for details.

public-google-sheets-parser's People

Contributors

ash-datasight avatar cjmling avatar fureweb-com avatar jywarren avatar soulauctioneer avatar spookyuser avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

public-google-sheets-parser's Issues

Typescript support

Is there anyway we could get a @types/public-google-sheets-parser package to be able to use this in TS projects?

Wrong json?

Hello!

I just parse this google sheet: https://docs.google.com/spreadsheets/d/1zCHUcpJI6KPwHNoZttTopJ7zHLF31bfb7HgsIodsqyg/edit#gid=0

and receive wrong JSON:
[ { '1 урок': '4 урок', '5б': '6в', 'Изменения в расписании уроков 19 сентября, понедельник к 3 уроку': 'Яковлева К.В. (замещение математики)', '': 11 }, { '1 урок': '5 урок', '5б': '7а', 'Изменения в расписании уроков 19 сентября, понедельник к 3 уроку': 'математика, Сергеева Н.И. (замещение)', '': 48 }, { '5б': '9а', 'Изменения в расписании уроков 19 сентября, понедельник к 3 уроку': 'домой' }, { '1 урок': '6 урок', '5б': '6б', 'Изменения в расписании уроков 19 сентября, понедельник к 3 уроку': 'домой' }, { '1 урок': '7 урок', '5б': '6а, 7а ', 'Изменения в расписании уроков 19 сентября, понедельник к 3 уроку': 'домой' }, { '1 урок': 'Отсутствует: Яковлева Т.В.' }, { 'Изменения в расписании уроков 19 сентября, понедельник к 3 уроку': '20 сентября, вторник' }, { '1 урок': '4 урок', '5б': '8б', 'Изменения в расписании уроков 19 сентября, понедельник к 3 уроку': 'физика, Котова Н.И. вместо 7 урока', '': 45 }, { '1 урок': '5 урок', '5б': '8а', 'Изменения в расписании уроков 19 сентября, понедельник к 3 уроку': 'биология, Калинина Л.М. (замещение технологии)', '': 35 }, { '1 урок': '7 урок ', '5б': '8б', 'Изменения в расписании уроков 19 сентября, понедельник к 3 уроку': 'домой' }, { '1 урок': 'Отсутствует: Иванов М.С.' }, { 'Изменения в расписании уроков 19 сентября, понедельник к 3 уроку': '21 сентября, среда' }, { '1 урок': '4 урок', '5б': '9б', 'Изменения в расписании уроков 19 сентября, понедельник к 3 уроку': 'Головко А.Б. вместо 5а класса', '': 9 }, { '1 урок': '6 урок', '5б': '9б', 'Изменения в расписании уроков 19 сентября, понедельник к 3 уроку': 'Скрипка Н.В. вместо 5а класса', '': 32 }, { '1 урок': 'Отсутствует: Рольгейзер К.С.' }, { 'Изменения в расписании уроков 19 сентября, понедельник к 3 уроку': '23 сентября, пятница' }, { '1 урок': '2 урок', '5б': '9б', 'Изменения в расписании уроков 19 сентября, понедельник к 3 уроку': 'математика, Сергеева Н.И. (замещение истории)', '': 48 }, { '1 урок': '4 урок', '5б': '6в', 'Изменения в расписании уроков 19 сентября, понедельник к 3 уроку': 'русский язык, Белова С.Н. (замещение истории)', '': 41 }, { '1 урок': '5 урок', '5б': '6б', 'Изменения в расписании уроков 19 сентября, понедельник к 3 уроку': 'русский язык, Кутушева Н.Д. (замещение истории)', '': 29 }, { '1 урок': '6 урок', '5б': '6а', 'Изменения в расписании уроков 19 сентября, понедельник к 3 уроку': 'домой' }, { '1 урок': '7 урок', '5б': '9а', 'Изменения в расписании уроков 19 сентября, понедельник к 3 уроку': 'домой' }, { '1 урок': 'Отсутствует: Рольгейзер К.С.' } ]

code:`const PublicGoogleSheetsParser = require('public-google-sheets-parser')

const spreadsheetId = '1zCHUcpJI6KPwHNoZttTopJ7zHLF31bfb7HgsIodsqyg'

// 1. You can pass spreadsheetId when parser instantiation
const parser = new PublicGoogleSheetsParser(spreadsheetId)
parser.parse().then((items) => {
console.log(items);
// items should be [{"a":1,"b":2,"c":3},{"a":4,"b":5,"c":6},{"a":7,"b":8,"c":9}]
})`

What I do wrong?

response empty in vue

here is my code

async function readGoogleSheet(){
    const spreadsheetId = '1OUyhgGkaP6J7zkSUe7n7-gyJTeww196-NJIOFfPzQfc'
    // const spreadsheetId='1oCgY0UHHRQ95snw7URFpOOL_DQcVG_wydlOoGiTof5E'
    const sp = new PublicGoogleSheetsParser()
    sp.id = spreadsheetId
    let items = await sp.parse()
    console.log(items)
}

the items is [],
this spreadsheetId works in this demo page https://fureweb-com.github.io/public-google-sheets-parser/

Missing characters on parsed data ");"

My google sheet includes some html tags and some function names or some rgb values on some of them. If google sheets has any of this combination of characters ); (a closing parenthesis followed by a semicolon) it is not included in the parsed results. Just ignores and skips that characters. Probably some parsing error?

Here are some examples:
<a style="color: rgb(218, 218, 218); font-weight: bold;" href="/policy/website/terms-of-service" target="_blank">
becomes
<a style="color: rgb(218, 218, 218 font-weight: bold;" href="/policy/website/terms-of-service" target="_blank">
As you can see the characters at the end of the rgb values are gone.

Smaller examples again works:
rgb(218, 218, 218); becomes rgb(218, 218, 218
"CallSomeFunction();" becomes "CallSomeFunction("
CallSomeFunction(); becomes CallSomeFunction(

I am using this on node.js v20.11.0
public-google-sheets-parser v1.5.2
Every other text and characters seems to work fine.

Data is overridden if two columns have the same header name

Problem

Data is overridden if two columns have the same header name. If two columns have the same header name, only the value of the last column with this name is returned. Some sheets use the same column name multiple times for displaying nested information, so it might be useful to find a way to return all columns, even if the name is the same.

Example

This sheet (https://docs.google.com/spreadsheets/d/1ZjNQ-MB660qU8dwxFNVxLxUBfiZr1IW-XoDkngtqZRY/edit?usp=sharing) produces the following result:

uniqueColumn sameColumn sameColumn
Unique 1 Same 1 Same 2
[
  {
    "uniqueColumn": "Unique 1",
    "sameColumn": "Same 2"
  }
]

Possible Fixes

There could be an optional flag in the configuration to also include the column identifier (A, B, ...) in the response, for example A - uniqueColumn, B - sameColumn, C - sameColumn.

No data returned

Spreadsheet id "1RXjhi32S2zIfFivybqL95tKybpN66z-JtzbtuZUdOZM", SheetName = "String"
are shared publicly

new PublicGoogleSheetsParser("1RXjhi32S2zIfFivybqL95tKybpN66z-JtzbtuZUdOZM", { sheetName: "Strings" }).parse().then(r => console.inf(r)) shows []

Interestingly, getSpreadsheetDataUsingFetch() returns:

google.visualization.Query.setResponse({"version":"0.6","reqId":"0","status":"ok","sig":"1458216511","table":{"cols":[{"id":"A","label":"","type":"string"},{"id":"B","label":"","type":"string"}],"rows":[{"c":[{"v":"key"},{"v":"value"}]}],"parsedNumHeaders":0}});

Columns with Falsey values won't get returned in item, but I would like them to

Hi, this is maybe not an issue but a feature. When I have values in my columns like FALSE, 0 they don't get returned, so the items that I get back don't have that Column at all.

Or if some values in the Column are with numbers, and one of them is TRUE/FALSE, the true-false value doesn't get pulled back also

Since I am using conditional logic on my React based on that value I would like to have them, can someone guide me on how to do this?

The simplest way is to treat everything as text, is there an option for this or maybe an option in Google Sheets I can set for this

Use case: Keeping track of items that are in stock.
Works fine if we have items in stock (1,4,55,..) but once we get to 0, the value I get is basically undefined (key doesnt exist at all for that item) and I would like to have 0. And based on it I would like to do my conditionals.

Use case 2: I have a toggle for a button and sometimes it expects true/false based on the item

I can work around my logic for myself, but I am making an app maker (people use my app to make apps) so my selection logic has to be dynamic and they should decide when something is disabled, based on pattern matching for string. So sometimes I need the 0 or the FALSE as a response instead of the whole key being omitted.

Is there a setting for this or should I fallback and try to use the underlying fetch for this myself
https://docs.google.com/spreadsheets/d/{sheetID}/gviz/tq?

outdated SheetInfo types

It seems new property are now allowed for SheetInfo (such as useFormatDate) but index.d.ts isn't yet updated with this option yet.

image

column mismatch when there are empty cells

I love this so far! But I have some data where there are empty cells in some rows, and the parser seems to skip over the nulls when it associates the values with columns for each row. I can't tell by looking if filterUselessRows is removing all nulls in each row, even if there are some data values, or if it's something else.

Let me know if you'd like me to provide more details, but I think it's pretty easy to confirm. (For example, compare Sample data to results when using spreadsheet ID 1m55mK6IVJxs9PEjX8i90VB8uy8eLkmv_F04iR7v6spc in Demo Site. Column headers are wrong for rows 2 and 3. )

Thanks for considering!

"TS2351: This expression is not constructable"

src/scratchpad.ts:4:20 - error TS2351: This expression is not constructable.
  Type 'typeof import("/Users/jeff/Work/@home/fyi-migration/node_modules/public-google-sheets-parser/@types/index")' has no construct signatures.

4 const parser = new PublicGoogleSheetsParser(process.env.GOOGLE_DEFAULT_SHEET, { sheetName: 'talks' });

Still have to figure out where exactly the issue is, but using this library from a TS 5.4 ESM project generates some immediate errors on trying to instantiate the class. Curiously, it does work, but the error suggests something in the type definitions might be a bit off. I'll be investigating further this weekend when I have a chance.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.