The `google-spreadsheet` package provides a robust and easy-to-use JavaScript/TypeScript interface for interacting with the Google Sheets API. It simplifies common tasks like reading, writing, and manipulating data within spreadsheets, as well as managing sheets and documents themselves. The current stable version is `5.2.0`, and the project maintains an active development cycle with frequent minor and patch updates for new features, bug fixes, and dependency synchronization. Key differentiators include comprehensive support for multiple authentication methods via `google-auth-library` (service account, OAuth 2.0, API key, ADC), both cell-based and row-based APIs for flexible data interaction, extensive methods for managing worksheets and documents (e.g., adding, removing, resizing, updating properties, setting permissions), and built-in automatic retries with exponential backoff for API requests, enhancing reliability against transient network issues and rate limits. It aims to be the most popular wrapper, abstracting away the complexities of the underlying Google Sheets API.
npm install google-spreadsheetVerified import paths — ran on the pinned version, not inferred.
Demonstrates initializing the GoogleSpreadsheet client with service account credentials, loading document properties, and performing basic operations like reading/updating document/sheet titles and managing sheets.
Ensure your `google-auth-library` peer dependency is `>=8.8.0`. Review your authentication setup, as older patterns might be deprecated or incompatible. Refer to the official authentication guide.
Replace calls to `doc.useServiceAccountAuth()` with creating an authenticated client from `google-auth-library` and passing it to `new GoogleSpreadsheet(id, authClient)`.
If your environment doesn't support top-level `await`, wrap your async code in an immediately invoked async function expression (IIFE): `(async () => { /* your code */ })();`.Upgrade your `google-auth-library` package to version `^8.8.0` or higher to ensure compatibility. For example: `npm install google-auth-library@latest`.
If setting `GOOGLE_PRIVATE_KEY` as an environment variable, ensure newlines are `\\n`. In your JavaScript code, you might need `process.env.GOOGLE_PRIVATE_KEY?.replace(/\\n/g, '\n')` to correctly parse it.
Update your code to anticipate `''` for empty cell values when on or above v5.2.0, or explicitly handle both `undefined` and `''` if supporting a range of versions.
Ensure you are using ESM `import` syntax: `import { GoogleSpreadsheet } from 'google-spreadsheet';`. If using CommonJS, verify your build system or Node.js version supports this package's module format correctly.Ensure the `key` property in your `JWT` constructor correctly represents the private key. If using `process.env`, replace escaped newlines: `key: process.env.GOOGLE_PRIVATE_KEY?.replace(/\\n/g, '\n') ?? ''`. Always double-check the private key format from your service account JSON.
Verify that the service account email has 'Editor' or 'Viewer' access to the target Google Sheet. Also, ensure the Google Sheets API is enabled for your project in the Google Cloud Console.
Wrap your top-level `await` calls within an immediately invoked async function expression (IIFE): `(async function() { /* your code here */ })();` or ensure your Node.js version supports top-level await.