What is @googleapis/sheets?
@googleapis/sheets is an npm package that provides a client library for accessing the Google Sheets API. It allows developers to interact with Google Sheets programmatically, enabling them to read, write, and manipulate spreadsheet data.
What are @googleapis/sheets's main functionalities?
Read Data from a Spreadsheet
This feature allows you to read data from a specified range in a Google Sheets spreadsheet. The code sample demonstrates how to use the Google Sheets API to fetch data from a specific range in a sheet.
const { google } = require('@googleapis/sheets');
const sheets = google.sheets('v4');
async function readData(auth) {
const request = {
spreadsheetId: 'your-spreadsheet-id',
range: 'Sheet1!A1:D5',
auth: auth,
};
try {
const response = await sheets.spreadsheets.values.get(request);
console.log(response.data.values);
} catch (err) {
console.error(err);
}
}
Write Data to a Spreadsheet
This feature allows you to write data to a specified range in a Google Sheets spreadsheet. The code sample demonstrates how to use the Google Sheets API to update a range of cells with new data.
const { google } = require('@googleapis/sheets');
const sheets = google.sheets('v4');
async function writeData(auth) {
const request = {
spreadsheetId: 'your-spreadsheet-id',
range: 'Sheet1!A1',
valueInputOption: 'RAW',
resource: {
values: [
['Item', 'Cost'],
['Apple', '$1'],
['Banana', '$2'],
],
},
auth: auth,
};
try {
const response = await sheets.spreadsheets.values.update(request);
console.log(response.data);
} catch (err) {
console.error(err);
}
}
Append Data to a Spreadsheet
This feature allows you to append data to a specified range in a Google Sheets spreadsheet. The code sample demonstrates how to use the Google Sheets API to add new rows of data to a sheet.
const { google } = require('@googleapis/sheets');
const sheets = google.sheets('v4');
async function appendData(auth) {
const request = {
spreadsheetId: 'your-spreadsheet-id',
range: 'Sheet1!A1',
valueInputOption: 'RAW',
insertDataOption: 'INSERT_ROWS',
resource: {
values: [
['Orange', '$3'],
['Grapes', '$4'],
],
},
auth: auth,
};
try {
const response = await sheets.spreadsheets.values.append(request);
console.log(response.data);
} catch (err) {
console.error(err);
}
}
Other packages similar to @googleapis/sheets
google-spreadsheet
The google-spreadsheet package is another popular library for interacting with Google Sheets. It provides a simpler interface for reading and writing data to Google Sheets. Compared to @googleapis/sheets, it abstracts away some of the complexities of the Google Sheets API, making it easier to use for basic operations.
gsheets
The gsheets package is a lightweight library for accessing Google Sheets. It offers basic functionalities for reading and writing data. While it is not as feature-rich as @googleapis/sheets, it is suitable for simple use cases and quick integrations.
sheets
Reads and writes Google Sheets.
Installation
$ npm install @googleapis/sheets
Usage
All documentation and usage information can be found on GitHub.
Information on classes can be found in Googleapis Documentation.
License
This library is licensed under Apache 2.0. Full license text is available in LICENSE.
Contributing
We love contributions! Before submitting a Pull Request, it's always good to start with a new issue first. To learn more, see CONTRIBUTING.
Questions/problems?
Crafted with ❤️ by the Google Node.js team