Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposed v2.0.2 rewrite using google-spreadsheet@3.1.15 #31

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
8b5ef85
Update to google-spreadsheet @ 3.1.15
johnbeech Feb 20, 2021
9a5e7ac
Software Removed and rebuilt the package-lock file.
johnbeech Feb 20, 2021
0e6f4d7
Maintenance Created .github/workflows/pr-check.yml from template.
johnbeech Feb 20, 2021
fa21b74
Merge pull request #2 from connected-web/maintenance-jumper-add-pr-check
johnbeech Feb 20, 2021
71163fe
Merge pull request #1 from connected-web/software-jumper-rebuild
johnbeech Feb 20, 2021
1866fae
Update README with new goals
johnbeech Feb 20, 2021
434624d
Remove travis CI file
johnbeech Feb 20, 2021
6b13efa
Add standard js linter and commands (#3)
johnbeech Feb 20, 2021
41a5635
Fix linting and update tests to linter friendly chai (#4)
johnbeech Feb 20, 2021
430faf4
Remove .eslintrc file
johnbeech Feb 20, 2021
a87f3af
Remove .eslintrc file
johnbeech Feb 20, 2021
b6d26ac
Test for reading credentials from local fs or environment variable
johnbeech Feb 20, 2021
a58a20a
Map repository secret to environment for test command
johnbeech Feb 20, 2021
aad31d7
Switch priority of credential test based on input
johnbeech Feb 20, 2021
e61a6b1
Stub test for Film Spreadsheet to JSON
johnbeech Feb 20, 2021
fe165b1
Film spreadsheet integration (#5)
johnbeech Feb 20, 2021
eb3f1f9
Clean up CHANGELOG.md
johnbeech Feb 20, 2021
dfe0af4
Bump version to 2.0.0
johnbeech Feb 20, 2021
9c63646
Fix commander integration after upgrade to 7.1.0
johnbeech Feb 20, 2021
857c9db
Add test around allWorksheets: true option
johnbeech Feb 21, 2021
6d97c56
Prepare v2.0.1 release
johnbeech Feb 21, 2021
a6a49fa
Add guard for parsing worksheets that don't contain cells; i.e. charts
johnbeech Feb 21, 2021
b3ec323
Prepare v2.0.2 for release
johnbeech Feb 21, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 0 additions & 26 deletions .eslintrc

This file was deleted.

28 changes: 28 additions & 0 deletions .github/workflows/pr-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Check PR
on:
pull_request:
branches:
- master
jobs:
test:
name: Linting and Testing
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Use Node JS LTS
uses: actions/setup-node@v1
with:
node-version: 12.x

- name: Install npm dependencies
run: npm install

- name: Run build steps
run: npm build -s

- name: Run default tests
run: npm test -s
env:
GOOGLE_SERVICE_ACCOUNT_SECURE_CREDENTIALS: ${{ secrets.GOOGLE_SERVICE_ACCOUNT_SECURE_CREDENTIALS }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ node_modules

# Users Environment Variables
.lock-wscript

secure-credentials.json
5 changes: 0 additions & 5 deletions .travis.yml

This file was deleted.

21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
# 2.0.2
## Changes
- Add guard for parsing worksheets that don't contain cells; i.e. charts

# 2.0.1
## Changes
- Fix bug with `allWorksheets: true` and added additional test

# 2.0.0
## New
- Tests and CI using Github Actions to repo

## Changes
- Fixed security vulnerabilities reported by `npm audit`
- Updated google-spreadsheet depenedncy
- Removed support for public published sheets due to deprecated authentication options
- Examples now use async await
- Changed internal workings to use 0 based indexes for rows and columns
- Changed internal workings to use cleaner async await code
- Introduced standard js as the enforced linter as part of the CI actions

# 1.0.0
## New
- Service Account auth support
Expand Down
69 changes: 42 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
google-spreadsheet-to-json
==========================

[![NPM version](https://badge.fury.io/js/google-spreadsheet-to-json.png)](http://badge.fury.io/js/google-spreadsheet-to-json) [![Build Status (master branch of bassarisse/google-spreadsheet-to-json)](https://travis-ci.org/bassarisse/google-spreadsheet-to-json.svg?branch=master)](https://travis-ci.org/bassarisse/google-spreadsheet-to-json)
** 2020-02-20 : This fork of `bassarisse/google-spreadsheet-to-json` project adds more tests; implements against the Google v4 API using the latest version of `theoephraim/node-google-spreadsheet` and Service Account credentials. **

A simple tool to export Google Spreadsheets to JSON files. Can be used though Node API or CLI.
Please raise an issue or suggestions on the original repo and tag me @johnbeech you see space for improvements - like missing tests or broken functionality.

## Description

A simple tool to export Google Spreadsheets to JSON files. Can be used though Node API or CLI.

## Installation

Expand All @@ -18,7 +21,6 @@ Node API:
$ npm install --save google-spreadsheet-to-json
```


## Help

```
Expand Down Expand Up @@ -66,34 +68,40 @@ You can also redirect the output if you omit the filename:
$ gsjson abc123456789 >> data.json
```


## Usage (Node API)

With the exception of `beautify` and the file path, the same options from the CLI applies here (options like `include-header` becomes `includeHeader`).

```javascript
var gsjson = require('google-spreadsheet-to-json');

gsjson({
spreadsheetId: 'abc123456789',
// other options...
})
.then(function(result) {
console.log(result.length);
console.log(result);
})
.catch(function(err) {
console.log(err.message);
console.log(err.stack);
});
const gsjson = require('google-spreadsheet-to-json');

const credentials = {
client_email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL,
private_key: process.env.GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY
}

async function example () {
try {
const result = await gsjson({
spreadsheetId: 'abc123456789',
credentials
// other options...
})
console.log(result.length)
console.log(result)
} catch (ex) {
console.log(ex.message)
console.log(ex.stack)
}
}

example()
```


### Notes

- A spreadsheet ID can be extracted from its URL.
- If an array is passed on the `worksheet` option (in CLI, this can be done by repeating the argument) or the `allWorksheets` option is used, the output from each worksheet is returned inside an array (the order is not guaranteed).

- A spreadsheet ID can be extracted from its URL - for example `https://docs.google.com/spreadsheets/d/1G2_YLuQeKXCtpOWshqIBazzUeefuOMDZ5q10F2u9MHw/edit#gid=0` becomes `1G2_YLuQeKXCtpOWshqIBazzUeefuOMDZ5q10F2u9MHw`
- If an array is passed on the `worksheet` option (in CLI, this can be done by repeating the argument) or the `allWorksheets` option is used, the output from each worksheet is returned inside an array; however the order is not guaranteed.

## About authentication

Expand All @@ -108,22 +116,29 @@ For quick tests, there's a method to acquire a temporary token:

For more detailed information regarding auth methods: https://github.com/theoephraim/node-google-spreadsheet


## Known issues

- Public spreadsheets can only be used without authentication if the option "File > Publish to the web" is used in the Google Spreadsheets GUI, even if the spreadsheet is visible to everyone. This problem won't occur when authenticated.
- Public spreadsheets are not currently supported - although there is a way to access raw JSON from a spreadsheet which could be supported in future: `https://spreadsheets.google.com/feeds/cells/1G2_YLuQeKXCtpOWshqIBazzUeefuOMDZ5q10F2u9MHw/1/public/full?alt=json` - this works if the option "File > Publish to the web" is used in the Google Spreadsheets GUI, even if the spreadsheet is visible to everyone. This problem won't occur when authenticated.

## Examples

## Examples & change log
See [EXAMPLES.md](./EXAMPLES.md)

See specific files.
## Change Log

See [CHANGELOG.md](./CHANGELOG.md)

## TO-DO

- Improve options documentation (especially header size)
- Create more test cases


## License
google-spreadsheet-to-json is free and unencumbered public domain software. For more information, see the accompanying UNLICENSE file.


## Contributors

- @bassarisse Bruno Assarisse
- @cigolpl Mateusz
- @johnbeech John Beech (Connected Web)
34 changes: 22 additions & 12 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
var gsjson = require('./index')
const gsjson = require('./')

gsjson({
spreadsheetId: 'spreadsheetId',
token: 'token'
})
.then(function(res) {
console.log(res)
console.log(res.length)
})
.catch(function(err) {
console.log(err.stack)
})
const credentials = {
client_email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL,
private_key: process.env.GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY
}

async function example () {
try {
const result = await gsjson({
spreadsheetId: 'abc123456789',
credentials
// other options...
})
console.log(result.length)
console.log(result)
} catch (ex) {
console.log(ex.message)
console.log(ex.stack)
}
}

example()
94 changes: 46 additions & 48 deletions gsjson.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,64 @@
#!/usr/bin/env node

var fs = require('fs')
var program = require('commander')
var Promise = require('bluebird')
var helper = require('./helper')
var packageData = require('./package.json')
const fs = require('fs')
const program = require('commander')
const Promise = require('bluebird')
const helper = require('./helper')
const packageData = require('./package.json')

program
.version(packageData.version)
.usage('<spreadsheet-id> [file] [options]')
.option('-b, --beautify', 'Beautify final JSON')
.option('-s, --credentials <s>', 'Service Account credentials JSON data or file path')
.option('-t, --token <token>', 'Auth token acquired externally')
.option('-y, --tokentype <tokentype>', 'Type of the informed token (defaults to "Bearer")')
.option('-w, --worksheet <n>', 'Worksheet index (zero-based) or title (defaults to first worksheet, can be repeated)', handlePossibleList)
.option('-a, --all-worksheets', 'Return all worksheets (worksheet option is ignored)')
.option('-c, --hash <column>', 'Column to hash the final object')
.option('-m, --property-mode <mode>', 'How to handle property names: "camel" (default), "pascal", "nospace" or "none"', /^(camel|pascal|nospace|none)$/i, 'camel')
.option('-i, --vertical', 'Use the first column as header')
.option('-l, --list-only', 'Just list the values in arrays')
.option('-0, --include-header', 'Include header when using "list-only" option')
.option('--header-start <n>', 'Header start line (auto-detected by default)')
.option('--header-size <n>', 'Header lines quantity (defaults to 1)')
.option('--ignore-col <n>', 'Column name (Excel-like labels) to be ignored (can be repeated, number are also supported)', handlePossibleList)
.option('--ignore-row <n>', 'Row number to be ignored (can be repeated)', handlePossibleList)
.parse(process.argv)
.version(packageData.version)
.usage('<spreadsheet-id> [file] [options]')
.option('-b, --beautify', 'Beautify final JSON')
.option('-s, --credentials <s>', 'Service Account credentials JSON data or file path')
.option('-t, --token <token>', 'Auth token acquired externally')
.option('-y, --tokentype <tokentype>', 'Type of the informed token (defaults to "Bearer")')
.option('-w, --worksheet <n>', 'Worksheet index (zero-based) or title (defaults to first worksheet, can be repeated)', handlePossibleList)
.option('-a, --all-worksheets', 'Return all worksheets (worksheet option is ignored)')
.option('-c, --hash <column>', 'Column to hash the final object')
.option('-m, --property-mode <mode>', 'How to handle property names: "camel" (default), "pascal", "nospace" or "none"', /^(camel|pascal|nospace|none)$/i, 'camel')
.option('-i, --vertical', 'Use the first column as header')
.option('-l, --list-only', 'Just list the values in arrays')
.option('-0, --include-header', 'Include header when using "list-only" option')
.option('--header-start <n>', 'Header start line (auto-detected by default)')
.option('--header-size <n>', 'Header lines quantity (defaults to 1)')
.option('--ignore-col <n>', 'Column name (Excel-like labels) to be ignored (can be repeated, number are also supported)', handlePossibleList)
.option('--ignore-row <n>', 'Row number to be ignored (can be repeated)', handlePossibleList)
.parse(process.argv)

const options = program.opts()
options.spreadsheetId = program.args[0] || program.spreadsheetId

if (program.args.length < 1) {
program.help()
program.help()
}

program.spreadsheetId = program.args[0] || program.spreadsheetId

var filename = program.args[1]
const filename = program.args[1]

helper.spreadsheetToJson(program)
.then(function(result) {
helper.spreadsheetToJson(options)
.then(function (result) {
return JSON.stringify(result, null, program.beautify ? 4 : null)
})
.then(function(result) {

})
.then(function (result) {
if (filename) {
return Promise.promisify(fs.writeFile)(filename, result, 'utf-8')
return Promise.promisify(fs.writeFile)(filename, result, 'utf-8')
} else {
process.stdout.write(result)
process.stdout.write(result)
}

})
.catch(function(err) {
})
.catch(function (err) {
throw err
})

function handlePossibleList(val, memo) {
})

if (typeof memo !== 'undefined') {
if (Array.isArray(memo)) {
memo.push(val)
return memo
} else {
return [memo, val]
}
function handlePossibleList (val, memo) {
if (typeof memo !== 'undefined') {
if (Array.isArray(memo)) {
memo.push(val)
return memo
} else {
return [memo, val]
}
}

return val
return val
}
Loading