public-google-sheets-parser
Advanced tools
Comparing version 1.0.25 to 1.1.0
{ | ||
"name": "public-google-sheets-parser", | ||
"version": "1.0.25", | ||
"version": "1.1.0", | ||
"description": "Get JSONArray from public google sheets with using only spreadsheetId", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -57,2 +57,8 @@ # Public Google sheets parser | ||
}) | ||
// 4. You can also pass the name of specific sheet if you want to get that instead of the first one. | ||
parser.parse(spreadsheetId, 'Sheet2').then((items) => { | ||
// items should be from the second sheet! | ||
}) | ||
``` | ||
@@ -59,0 +65,0 @@ You can use any of the three methods you want! |
@@ -5,9 +5,8 @@ const isBrowser = typeof require === 'undefined' | ||
class PublicGoogleSheetsParser { | ||
constructor (spreadsheetId) { | ||
constructor (spreadsheetId, sheetName) { | ||
this.id = spreadsheetId | ||
this.sheetName = sheetName | ||
} | ||
getSpreadsheetDataUsingFetch () { | ||
if (!this.id) return null | ||
// Read data from the first sheet of the target document. | ||
@@ -17,3 +16,11 @@ // It cannot be used unless everyone has been given read permission. | ||
// spreadsheet document for example: https://docs.google.com/spreadsheets/d/10WDbAPAY7Xl5DT36VuMheTPTTpqx9x0C5sDCnh4BGps/edit#gid=1719755213 | ||
return fetch(`https://docs.google.com/spreadsheets/d/${this.id}/gviz/tq?`) | ||
// Sheet selection from: https://stackoverflow.com/a/44592363/1649917 | ||
if (!this.id) return null | ||
let url = `https://docs.google.com/spreadsheets/d/${this.id}/gviz/tq?` | ||
if (this.sheetName) { | ||
url = url.concat(`sheet=${this.sheetName}`) | ||
} | ||
return fetch(url) | ||
.then((r) => r.ok ? r.text() : null) | ||
@@ -54,4 +61,5 @@ .catch((_) => null) | ||
async parse (spreadsheetId) { | ||
async parse (spreadsheetId, sheetName) { | ||
if (spreadsheetId) this.id = spreadsheetId | ||
if (sheetName) this.sheetName = sheetName | ||
@@ -58,0 +66,0 @@ if (!this.id) throw new Error('SpreadsheetId is required.') |
@@ -65,1 +65,18 @@ const test = require('tape') | ||
}) | ||
test('should get first sheet by default if none is specified', async (t) => { | ||
const spreadsheetId = '10WDbAPAY7Xl5DT36VuMheTPTTpqx9x0C5sDCnh4BGps' | ||
const result = await parser.parse(spreadsheetId) | ||
const expected = [{ a: 1, b: 2, c: 3 }, { a: 4, b: 5, c: 6 }, { a: 7, b: 8, c: 9 }] | ||
t.deepEqual(result, expected) | ||
t.end() | ||
}) | ||
test('should get 2nd sheet if specified', async (t) => { | ||
const spreadsheetId = '1wdU-Cf7Kn5ZvhcvRePOoL10urdTA6TWGndBSKDAiYbQ' | ||
const sheetName = 'Sheet2' | ||
const result = await parser.parse(spreadsheetId, sheetName) | ||
const expected = [{ a: 10, b: 20, c: 30 }, { a: 40, b: 50, c: 60 }, { a: 70, b: 80, c: 90 }] | ||
t.deepEqual(result, expected) | ||
t.end() | ||
}) |
821142
223
117