tysoncung/excel-zod(新しいタブで開きます)
主要言語: TypeScript
Type-safe Excel read/write with Zod validation for Node.js
- star 数
- 3
- watcher 数
- 0
- fork 数
- 0
- issue 数
- 0
- 最終更新
- 2026/02/19
主要言語: TypeScript
Type-safe Excel read/write with Zod validation for Node.js
Type-safe Excel reading and writing with Zod schemas.
Define your spreadsheet structure with Zod, then read, write, and validate Excel files with full TypeScript type safety. No more manual column mapping, no more runtime surprises.
npm install excel-zod zod
Zod is a peer dependency. You need to install it separately.
import { z } from 'zod'; import { createExcelSchema } from 'excel-zod'; // 1. Define your schema const UserSchema = createExcelSchema(z.object({ name: z.string(), email: z.string().email(), age: z.number().int().positive(), joinDate: z.date(), active: z.boolean(), })); // 2. Read Excel into typed, validated objects const { data, errors } = await UserSchema.parse('users.xlsx'); // data: { name: string, email: string, age: number, joinDate: Date, active: boolean }[] if (errors.length > 0) { console.error('Validation errors:'); for (const err of errors) { console.error(` Row ${err.row}, Column "${err.column}": ${err.message}`); } } // 3. Write typed data back to Excel await UserSchema.write(data, 'output.xlsx'); // 4. Generate a blank template await UserSchema.generateTemplate('template.xlsx');
Creates an ExcelSchema instance from a Zod object schema.
const schema = createExcelSchema(z.object({ ... }));
Read and validate an Excel file. Returns { data, errors, totalRows }.
Options:
| Option | Type | Default | Description |
|---|---|---|---|
sheet |
string | number |
1 |
Sheet name or 1-based index |
headerRow |
number |
1 |
Row containing headers |
startRow |
number |
headerRow + 1 |
First data row |
columns |
ColumnMapping[] |
auto-detect | Custom column mappings |
stopOnEmpty |
boolean |
true |
Stop on first empty row |
Same as parse, but throws ExcelValidationError if any rows fail validation.
Write typed data to an Excel file with headers, formatting, and proper column widths.
Options:
| Option | Type | Default | Description |
|---|---|---|---|
sheetName |
string |
"Sheet1" |
Worksheet name |
columns |
ColumnMapping[] |
auto | Custom header names |
Generate a blank Excel template with headers, type hints, data validation, and frozen header row.
Options:
| Option | Type | Default | Description |
|---|---|---|---|
sheetName |
string |
"Sheet1" |
Worksheet name |
addValidation |
boolean |
true |
Add Excel data validation |
freezeHeader |
boolean |
true |
Freeze the header row |
By default, excel-zod matches Excel headers to schema keys (case-insensitive). You can customize this:
// Match by custom header names const { data } = await schema.parse('file.xlsx', { columns: [ { key: 'name', header: 'Full Name' }, { key: 'email', header: 'Email Address' }, ], }); // Match by column index (1-based) const { data } = await schema.parse('file.xlsx', { columns: [ { key: 'name', index: 1 }, { key: 'email', index: 3 }, ], });
import { ExcelValidationError } from 'excel-zod'; try { const data = await schema.parseStrict('users.xlsx'); } catch (err) { if (err instanceof ExcelValidationError) { console.error(err.format()); // Row 3, Column "email": Invalid email (got: "not-an-email") // Row 5, Column "age": Number must be greater than 0 (got: -1) } }
| Zod Type | Excel Handling |
|---|---|
z.string() |
Text cells, auto-coerced |
z.number() |
Numeric cells, string-to-number coercion |
z.boolean() |
Yes/No, true/false, 1/0 |
z.date() |
Excel dates, serial numbers, ISO strings |
z.optional() |
Allows empty cells |
z.default() |
Uses default for empty cells |
| Feature | excel-zod | Manual ExcelJS | csv-parse + zod |
|---|---|---|---|
| Type-safe reading | Yes | No | Partial |
| Schema validation | Built-in | Manual | Separate step |
| Excel write | Yes | Manual setup | No (CSV only) |
| Template generation | Yes | Manual | No |
| Error messages | Row + column | Manual | Row only |
| Date handling | Automatic | Manual | Manual |
Looking for advanced features like multi-sheet schemas, streaming for large files, custom formatters, and pivot table generation? Check out excel-zod Pro (coming soon).
MIT