Jaspers Hub

rym / excel-export

pluginReviewed★ 0↓ 0

Excel Export: Builds .xlsx workbooks from supplied tables or grid panel data and writes/opens them in the plugin's exports folder.

2.1 KB
1import { definePlugin, defineSource } from '@jaspers-ai/sdk'
2import { z } from 'zod'
3import { writeWorkbook } from './save'
4import { readPanelTable } from './panels'
5
6const table = z.object({
7 title: z.string().min(1).describe('The table title; becomes the sheet name (sanitized, max 31 chars).'),
8 columns: z.array(z.string()).min(1).describe('Column headers, in order.'),
9 rows: z
10 .array(z.union([z.array(z.unknown()), z.record(z.string(), z.unknown())]))
11 .describe('Rows: each an array of cell values in column order, or an object keyed by column name. Numbers stay numeric.'),
12})
13
14const filename = z
15 .string()
16 .optional()
17 .describe('File name without folder, like bonds.xlsx. Default: first table title plus a timestamp.')
18
19const export_tables = defineSource({
20 description:
21 "Writes one or more tables to an .xlsx workbook (one sheet per table, bold frozen header, numeric cells, fitted column widths) in the plugin's exports folder and opens it. Answers the path, sheet names, and row counts.",
22 input: z.object({ tables: z.array(table).min(1).max(50).describe('The tables to export.'), filename }),
23 async run(args, ctx) {
24 return writeWorkbook(ctx, args.tables, args.filename)
25 },
26})
27
28const export_panels = defineSource({
29 description:
30 "Writes the tables shown in grid panels (like core/table panel e2) to an .xlsx workbook, one sheet per panel, and opens it. Reads each panel's rows from its published output or text; answers the path, sheet names, and row counts. When a panel's rows cannot be read, run its source and use export-tables instead.",
31 input: z.object({
32 panels: z.array(z.string().min(1)).min(1).max(20).describe('Panel ids on the grid, like e2.'),
33 titles: z.array(z.string()).optional().describe('Sheet titles per panel, in order; default from the panel.'),
34 filename,
35 }),
36 async run(args, ctx) {
37 const tables = []
38 for (const [i, id] of args.panels.entries()) tables.push(await readPanelTable(ctx, id, args.titles?.[i]))
39 return writeWorkbook(ctx, tables, args.filename)
40 },
41})
42
43export default definePlugin({
44 id: 'excel-export',
45 capabilities: ['files', 'state'],
46 sources: { 'export-tables': export_tables, 'export-panels': export_panels },
47})