
Security News
PodRocket Podcast: Inside the Recent npm Supply Chain Attacks
Socket CEO Feross Aboukhadijeh discusses the recent npm supply chain attacks on PodRocket, covering novel attack vectors and how developers can protect themselves.
google-spreadsheet-tables
Advanced tools
A TypeScript library that enables using Google Spreadsheets as a simple database with type safety. It provides a type-safe interface using Zod schemas to define table structures and perform CRUD operations on Google Spreadsheets.
A TypeScript library that enables using Google Spreadsheets as a simple database with type safety. It provides a type-safe interface using Zod schemas to define table structures and perform CRUD operations on Google Spreadsheets.
For small-scale, non-critical applications where performance is not a primary concern, Google Spreadsheets can serve as an excellent database alternative. This library provides a programming interface that makes it easy to view and manipulate data while maintaining type safety through Zod schemas.
./service-account.json
)client_email
field of your JSON key)pnpm add google-spreadsheet-tables
Here's a complete example of how to use the library with a user profile schema:
import { useWorksheetWithServiceAccountFile, useSpreadsheetTable } from 'google-spreadsheet-tables'
import { z } from 'zod'
// Define your schema
const userSchema = z.object({
name: z.string(),
age: z.coerce.number(),
gender: z.enum(['male', 'female', 'other']),
company: z.string().optional(),
})
// Initialize the spreadsheet connection
// Replace 'YOUR_SPREADSHEET_ID' to your file id. e.g. 1vob8zYwa2p9mLDaczN_Egn-01QjC-tC80-Y83yYMCR0
const { doc } = useWorksheetWithServiceAccountFile('YOUR_SPREADSHEET_ID', './service-account.json')
// Create a documents sheet
const { append, get, patch, snapshot, clear } = await useSpreadsheetTable(
doc,
'Users',
userSchema
)
// Add a new user
const newUser = await append({
name: 'John Doe',
age: 30,
gender: 'male',
company: 'Acme Inc.'
})
// Get all users
const { documents: allUsers } = await snapshot()
// Get a specific user
const user = await get(newUser.rowKey)
// Update a user
await patch(newUser.rowKey, {
age: 31,
company: 'New Company'
})
// Clear all users
await clear()
There are two ways to provide service account credentials:
Set the GOOGLE_APPLICATION_CREDENTIALS
environment variable to point to your service account JSON file:
export GOOGLE_APPLICATION_CREDENTIALS="./service-account.json"
Then you can use useWorksheetWithServiceAccountFile
without specifying the file path:
const { doc } = useWorksheetWithServiceAccountFile('YOUR_SPREADSHEET_ID')
Alternatively, you can set the GOOGLE_APPLICATION_CREDENTIALS_JSON
environment variable with the actual JSON content:
export GOOGLE_APPLICATION_CREDENTIALS_JSON='{"client_email":"...","private_key":"..."}'
Then you can use useWorksheetWithServiceAccount
without specifying the service account:
const { doc } = useWorksheetWithServiceAccount('YOUR_SPREADSHEET_ID')
You can also set individual environment variables for the service account credentials:
export GOOGLE_SERVICE_ACCOUNT_EMAIL="your-service-account@project.iam.gserviceaccount.com"
export GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\n..."
Then you can use useWorksheetWithServiceAccount
without specifying the service account:
const { doc } = useWorksheetWithServiceAccount('YOUR_SPREADSHEET_ID')
useWorksheetWithServiceAccount(spreadsheetId?: string, serviceAccount?: ServiceAccount)
Initializes a connection to a Google Spreadsheet using a service account.
spreadsheetId
: The ID of the Google SpreadsheetserviceAccount
: Service account credentials (client_email and private_key)useWorksheetWithServiceAccountFile(spreadsheetId?: string, filePath?: string)
Initializes a connection to a Google Spreadsheet using a service account JSON file.
spreadsheetId
: The ID of the Google SpreadsheetfilePath
: Path to the service account JSON fileuseSpreadsheetTable(doc: GoogleSpreadsheet, worksheetName: string, dataSchema: z.ZodObject<z.ZodRawShape>)
Creates a documents sheet with the specified schema.
Returns an object with the following methods:
append(data: DataType)
: Adds a new documentget(rowKey: number)
: Retrieves a document by row numberpatch(rowKey: number, data: PartialType)
: Updates a documentsnapshot()
: Retrieves all documentsclear()
: Removes all documentsdocumentSchema
: The complete document schemapartialSchema
: The partial document schema for updatessheet
: The underlying Google Spreadsheet worksheetThe library can use the following environment variables:
TABLES_SHEET_ID
: The default spreadsheet IDGOOGLE_APPLICATION_CREDENTIALS_JSON
: Service account credentials as a JSON stringGOOGLE_APPLICATION_CREDENTIALS
: Path to the service account JSON fileGOOGLE_SERVICE_ACCOUNT_EMAIL
: Service account email addressGOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY
: Service account private keyFAQs
A TypeScript library that enables using Google Spreadsheets as a simple database with type safety. It provides a type-safe interface using Zod schemas to define table structures and perform CRUD operations on Google Spreadsheets.
We found that google-spreadsheet-tables demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket CEO Feross Aboukhadijeh discusses the recent npm supply chain attacks on PodRocket, covering novel attack vectors and how developers can protect themselves.
Security News
Maintainers back GitHub’s npm security overhaul but raise concerns about CI/CD workflows, enterprise support, and token management.
Product
Socket Firewall is a free tool that blocks malicious packages at install time, giving developers proactive protection against rising supply chain attacks.