Google Sheet CRUD - A simple library for interacting with Google Sheets using Node.js.
npm install google-sheet-crudGoogleSheetService class provides an abstraction for interacting with Google Sheets. It allows users to perform CRUD operations (Create, Read, Update, Delete) efficiently.
client_email and private_key).
A should be named id.
A1.
sh
npm install google-sheet-crud
`
Class: GoogleSheetService
$3
`ts
constructor(credentials: { client_email: string; private_key: string })
`
- Initializes the GoogleSheetService instance with Google API credentials.
Methods
$3
`ts
async create({ sheetId, range, data }: CreateProps): Promise
`
Parameters:
- sheetId: The ID of the Google Sheet.
- range: The range where data will be added (e.g., Sheet1!A1:D1).
- data: An object containing key-value pairs of data.
Usage Example:
`ts
import { GoogleSheetService } from 'google-sheet-crud';
const googleSheetService = new GoogleSheetService({
client_email: 'your-service-account-email',
private_key: 'your-private-key'
});
await googleSheetService.create({
sheetId: 'your-sheet-id',
range: 'Sheet1!A1:D1',
data: { id: 1, name: 'John Doe', age: 30, active: true }
});
`
$3
`ts
async bulkCreate({ sheetId, range, data }: bulkCreateProps): Promise
`
Parameters:
- sheetId: The ID of the Google Sheet.
- range: The range where data will be added.
- data: An array of objects containing key-value pairs.
Usage Example:
`ts
await googleSheetService.bulkCreate({
sheetId: 'your-sheet-id',
range: 'Sheet1!A1:D1',
data: [
{ id: 1, name: 'John Doe', age: 30, active: true },
{ id: 2, name: 'Jane Doe', age: 25, active: false }
]
});
`
$3
`ts
async update({ sheetId, range, id, data }: UpdateProps): Promise
`
Parameters:
- sheetId: The ID of the Google Sheet.
- range: The range where data exists.
- id: The ID of the row to update.
- data: An object containing the updated key-value pairs.
Usage Example:
`ts
await googleSheetService.update({
sheetId: 'your-sheet-id',
range: 'Sheet1!A1:D1',
id: 1,
data: { name: 'John Updated', age: 31 }
});
`
$3
`ts
async get({ sheetId, range }: GetProps): Promise
`
Parameters:
- sheetId: The ID of the Google Sheet.
- range: The range to retrieve data from.
Usage Example:
`ts
const data = await googleSheetService.get({
sheetId: 'your-sheet-id',
range: 'Sheet1!A1:D10'
});
console.log(data);
`
$3
`ts
async delete({ sheetId, range, id }: RemoveProps): Promise
`
Parameters:
- sheetId: The ID of the Google Sheet.
- range: The range where data exists.
- id: The ID of the row to delete.
Usage Example:
`ts
await googleSheetService.delete({
sheetId: 'your-sheet-id',
range: 'Sheet1!A1:D1',
id: 1
});
``