This project demonstrates how to integrate the Google Sheets API with a Node.js application. It allows users to read from and write data to a Google Sheet.
- Write Data: Add new rows to a Google Sheet.
- Read Data: Retrieve data from a specified range in the Sheet.
- Node.js
- A Google Cloud Platform account with the Sheets API enabled
- A service account key file (JSON) from Google Cloud Console
-
Clone the Repository
git clone https://github.com/adanzweig/nodejs-google-sheets.git cd your-repository -
Install Dependencies
npm install
-
Set Up Your Credentials
Place your Google service account key file (e.g.,
google.json) in the root directory.
-
Writing Data to Google Sheets
Call the
writeToSheetfunction with an array of data. Each sub-array represents a row.const dataToWrite = [['Name', 'Age', 'Location'], ['Alice', 30, 'New York']]; writeToSheet(dataToWrite);
-
Reading Data from Google Sheets
Simply call the
readSheetfunction. It retrieves data from the specified range in the sheet.readSheet().then(data => console.log(data));
- Update the
spreadsheetIdinindex.jsto point to your target Google Sheet. - Adjust the range in the
writeToSheetandreadSheetfunctions as needed.
Contributions, issues, and feature requests are welcome!
This project is licensed under the MIT License.