SheetGetter is a tiny nodejs class that helps you fetch data from google sheets. Half of the utility of this repo is probably in the screenshots below that show you how to set up all of the credentialing razzmatazz.
let creds = JSON.parse(fs.readFileSync('service-account-creds-file-aweu54ug4igu.json'));
let sheetId = '1mjz4A4RzXN0hHj3Ww-nUOtk-WSrsi9B5-GvA-ZEuAKA';
// Create a SheetGetter instance associated with a specific google sheet
let mySheetGetter = new SheetGetter(sheetId, creds);
// Fetch a specific tab from the associated sheet
mySheetGetter.getSheet('Animals with fraudulent diplomas')
.then((res) => {
let rows = res.data.values;
// Do something with rows
})
.catch((err) => console.error(err));
See the example server.
Returns an array of all rows of the tab named sheetName
. Uses the spreadsheets.values/get
endpoint of the sheets v4 API. See rate limits.
Returns the date the sheet was last modified. Uses the files endpoint of the drive v3 API. See rate limits.
You need to setup three things:
-
Create a google cloud project, google service account, and download a credentials JSON file
-
Enable the google sheets and google drive APIs for your project
This is the alphanumeric string following the /d/
in a google sheets URL.
For example, if the sheets URL is https://docs.google.com/spreadsheets/d/1mjz4A4RzXN0hHj3Ww-nUOtk-WSrsi9B5-GvA-ZEuAKA, then 1mjz4A4RzXN0hHj3Ww-nUOtk-WSrsi9B5-GvA-ZEuAKA
is the Spreadsheet ID.
More info: https://developers.google.com/sheets/api/guides/concepts#spreadsheet_id.
SheetGetter expects a service account credentials json. Here's how you generate one of those:
-
Create a google account
-
Go to: https://console.cloud.google.com/iam-admin/serviceaccounts
-
Click "Create" to create a project:
- Name the project and click "Create":
- Click "Create Service Account":
- Name your service account, and click "Create":
- Give it the "Owner" role and click "Continue":
- Click "Create Key":
- Click "Create" to finally download the JSON credentials file, and finally click "Done":
- Hooray! You should have successfully downloaded a service account credentials json file, and see a screen like this:
- Go to the APIs library: https://console.cloud.google.com/apis/library
- Type "sheets", and click on the Google Sheets API result:
- Click "enable":
- Repeat for the Google Drive API