Skip to content

Commit

Permalink
Merge pull request #9 from urbancups/updates
Browse files Browse the repository at this point in the history
Code style (prettier), dependencies updates, and release of 1.0.0
  • Loading branch information
cguedes authored Jan 23, 2018
2 parents 3892d33 + 0e2a838 commit 2807843
Show file tree
Hide file tree
Showing 11 changed files with 3,589 additions and 295 deletions.
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package.json
.prettierrc.js
cred/node-sheets-test.json
lib
3 changes: 3 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
singleQuote: true
};
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.formatOnSave": true
}
75 changes: 35 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
> Read rows from google spreadsheet with google's sheets api.
[![Build Status](https://travis-ci.org/urbancups/node-sheets.svg?branch=master)](https://travis-ci.org/urbancups/node-sheets)
[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)

** This library is beta so you should use it with care. We will make an effort to support the library, but we reserve the right to make incompatible changes when necessary.

## Instalation

This library is distributed on `npm`. In order to add it as a dependency, run the following command:

## Installation

```bash
$ npm install node-sheets --save
Expand All @@ -20,42 +16,43 @@ $ npm install node-sheets --save
Example to retrieve data from [this google spreadsheet](https://docs.google.com/spreadsheets/d/1amfst1WVcQDntGe6walYt-4O5SCrHBD5WntbjhvfIm4) using ES7 async/await.

```javascript
import Sheets from 'node-sheets'
import Sheets from 'node-sheets';
try {
const gs = new Sheets('1amfst1WVcQDntGe6walYt-4O5SCrHBD5WntbjhvfIm4')
const authData = require('someGoogleCredentials.json') // authData = { client_email, private_key }
await gs.authorizeJWT(authData)
const table = await gs.tables('Formats!A1:E3')
console.log(table.headers)
console.log(table.formats)
console.log(table.rows)
const gs = new Sheets('1amfst1WVcQDntGe6walYt-4O5SCrHBD5WntbjhvfIm4');
const authData = require('someGoogleCredentials.json'); // authData = { client_email, private_key }
await gs.authorizeJWT(authData);
const table = await gs.tables('Formats!A1:E3');
console.log(table.headers);
console.log(table.formats);
console.log(table.rows);
} catch (err) {
console.error(err)
console.error(err);
}
```

You can also use the lib with Promises.

```javascript
import Sheets from 'node-sheets'
const gs = new Sheets('1amfst1WVcQDntGe6walYt-4O5SCrHBD5WntbjhvfIm4')
const authData = require('someGoogleCredentials.json') // authData = { client_email, private_key }
gs.authorizeJWT(authData)
import Sheets from 'node-sheets';
const gs = new Sheets('1amfst1WVcQDntGe6walYt-4O5SCrHBD5WntbjhvfIm4');
const authData = require('someGoogleCredentials.json'); // authData = { client_email, private_key }
gs
.authorizeJWT(authData)
.then(() => gs.tables('Formats!A1:E3'))
.then(table => {
console.log(table.headers)
console.log(table.formats)
console.log(table.rows)
console.log(table.headers);
console.log(table.formats);
console.log(table.rows);
})
.catch(err => {
console.error(err)
})
console.error(err);
});
```

If you want to use this with `require` you need to import the `default`:

```javascript
const Sheets = require('node-sheets').default
const Sheets = require('node-sheets').default;
```

## API
Expand All @@ -66,7 +63,7 @@ Returns tabular sheet data for the specified ranges. This method accepts three d

### String

If a **string** argument is specified, it defines the name of the range (A1 format) to be retrieved from the spreadsheet.
If a **string** argument is specified, it defines the name of the range ([A1 notation](https://developers.google.com/sheets/api/guides/concepts#a1_notation)) to be retrieved from the spreadsheet.
The return model is a `SheetTable` object.

### Object
Expand All @@ -89,7 +86,7 @@ The `.tables()` method returns SheetTable objects that contains tabular data for
| ... | ... | ... |

```javascript
const table = await gs.tables('Formats')
const table = await gs.tables('Formats');

{
title: 'Formats', // name of the sheet/table
Expand All @@ -113,19 +110,19 @@ const table = await gs.tables('Formats')
Sample access to the value of col 'Header 2' of first row:

```javascript
const currencyValue = table.rows[0]['Header 2'].value // 0.41
const currencyValue = table.rows[0]['Header 2'].value; // 0.41
```

**Note:** Formats are retrieved from first data row.

### Sample usage

```js
const sheet = await gs.tables('main') // ranges = ['main']
const sheet = await gs.tables('A100') // ranges = ['A100'] - that is the cell A100 and not the sheet A100
const sheet = await gs.tables({name: 'main'}) // ranges = ['main!A:ZZZ']
const sheet = await gs.tables({name: 'main', range: 'A1:B4'}) // ranges = ['main!A1:B4']
const sheets = await gs.tables([{name: 'main'}, {name: 'D001', range: 'A1:D3'}, {name: 'D002'}]) // ranges = ['main!A:ZZZ', 'D001!A1:D3', 'D002!A:ZZZ']
const sheet = await gs.tables('main'); // ranges = ['main']
const sheet = await gs.tables('A100'); // ranges = ['A100'] - that is the cell A100 and not the sheet A100
const sheet = await gs.tables({sheet: 'main'}); // ranges = ['main!A:ZZZ']
const sheet = await gs.tables({sheet: 'main', range: 'A1:B4'}); // ranges = ['main!A1:B4']
const sheets = await gs.tables([{sheet: 'main'}, {sheet: 'D001', range: 'A1:D3'}, {sheet: 'D002'}]); // ranges = ['main!A:ZZZ', 'D001!A1:D3', 'D002!A:ZZZ']
```

#### Caveat
Expand All @@ -135,27 +132,25 @@ More info [here](http://stackoverflow.com/a/39641586).

## Authentication

For now, node-sheets offers two authentication methods.
node-sheets offers two authentication methods.

1. With JWT token (`.authorizeJWT(auth [, scopes])`) using `private_key` and `client_email`, and also allowing to set auth scopes. The default auth scope is https://www.googleapis.com/auth/spreadsheets.readonly.

1. With APIKEY (`.authorizeApiKey(apikey)`) using an API Key you have created in the [google developers console](https://console.developers.google.com).
1. With JWT token (`.authorizeJWT(auth [, scopes])`) using `private_key` and `client_email`, and also allowing to set auth scopes. The default auth scope is https://www.googleapis.com/auth/spreadsheets.readonly.

1. With api key (`.authorizeApiKey(apikey)`) using an api key you have created in the [google developers console](https://console.developers.google.com).

## `Sheets.getLastUpdateDate()`

Returns a ISO_8601 compatible string with the last update date of the spreadsheet.
Returns a [ISO_8601](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString) compatible string with the last update date of the spreadsheet.
This can be used to check if a re-fetch is needed.

## `Sheets.getSheetsNames()`

Returns a list with all the names of the sheets in the spreadsheet.


## Examples

You can check the `/test/index.js` file for examples.


## License

This library is licensed under MIT. Full license text is available in [LICENSE](LICENSE).
Expand Down
12 changes: 0 additions & 12 deletions cred/node-sheets-test-e9b53a714340.json

This file was deleted.

Loading

0 comments on commit 2807843

Please sign in to comment.