Created
March 8, 2019 20:56
-
-
Save statico/764d8eb7cef3676da1021e25d7f916ea to your computer and use it in GitHub Desktop.
Get a Google Sheet using NodeJS async and TypeScript and Service Account credentials
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as fs from 'fs-extra' | |
import { google } from 'googleapis' | |
import * as pathlib from 'path' | |
const getSheet = async ( | |
keyFile: string, | |
spreadsheetId: string, | |
range: string | |
): Promise<string[][]> => { | |
const auth = await google.auth.getClient({ | |
keyFile, | |
scopes: ['https://www.googleapis.com/auth/spreadsheets.readonly'] | |
}) | |
const sheets = google.sheets('v4') | |
const values: any = await new Promise((resolve, reject) => { | |
sheets.spreadsheets.values.get( | |
{ | |
auth, | |
spreadsheetId, | |
range | |
}, | |
(err, data) => { | |
if (err) { | |
reject(err) | |
} else { | |
resolve(data) | |
} | |
} | |
) | |
}) | |
return values.data.values | |
} | |
export const syncData = async (credentialsPath: string): Promise<void> => { | |
const dataDir = pathlib.resolve(__dirname, '../data') | |
await fs.mkdirp(dataDir) | |
const rows = await getSheet( | |
credentialsPath, | |
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', | |
'Sheet 1!1:9999' | |
) | |
console.log('XXX', rows) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment