The Open Standard for Generative UI
---
title: "Handsontable"
---
<Steps>
<Step title="Share the table state" source="https://github.com/thesysdev/openui/blob/main/examples/miscellaneous/handsontable/src/app/api/table/route.ts">
Keep the table in a server-side store that both the grid and the chat route use. Edits made in Handsontable sync back to the server through POST /api/table.
```ts title="src/app/api/table/route.ts"
import { NextResponse } from "next/server";
// In-memory tables keyed by thread id; the chat tools read and write the same store.
import { getTableStore, type TableData } from "../chat/tableStore";
// The grid posts its full contents here after every edit.
export async function POST(req: Request) {
const { threadId, data, colHeaders } = (await req.json()) as {
threadId: string;
data: TableData;
colHeaders?: string[];
};
const store = getTableStore(threadId);
store.data = data;
if (colHeaders) store.colHeaders = colHeaders;
return NextResponse.json({ success: true, message: "Table data synced" });
}
```
</Step>
<Step title="Give the model spreadsheet tools" source="https://github.com/thesysdev/openui/blob/main/examples/miscellaneous/handsontable/src/app/api/chat/tools.ts">
Expose tools such as get_table_data, update_cells, add_rows, and set_formula. After a write, the model responds with a SpreadsheetTable component.
```ts title="src/app/api/chat/tools.ts"
// Server-side table store from step 1.
import { setFormula } from "./tableStore";
// The example keeps one shared table, so every tool uses the "default" thread.
const threadId = "default";
// Passed to client.chat.completions.runTools({ tools, ... }) in the chat route.
export const tools = [
{
type: "function",
function: {
name: "set_formula",
description: "Set an Excel-like formula in a specific cell.",
parameters: {
type: "object",
properties: {
row: { type: "number", description: "Zero-based row index" },
col: { type: "number", description: "Zero-based column index" },
formula: {
type: "string",
description: "Excel-like formula, e.g. =SUM(A1:A5)",
},
},
required: ["row", "col", "formula"],
},
parse: JSON.parse,
function: async (args: { row: number; col: number; formula: string }) =>
JSON.stringify(setFormula(threadId, args.row, args.col, args.formula)),
},
},
// get_table_data, update_cells, add_rows, delete_rows and others follow this shape.
];
```
</Step>
<Step title="Sync results into the grid" source="https://github.com/thesysdev/openui/blob/main/examples/miscellaneous/handsontable/src/lib/spreadsheet-library.tsx">
The Renderer draws SpreadsheetTable from the custom library, and the useSpreadsheetSync hook pushes the new data into the live Handsontable instance.
```tsx title="src/lib/spreadsheet-library.tsx"
import { createLibrary, defineComponent } from "@openuidev/react-lang";
import { openuiLibrary } from "@openuidev/react-ui/genui-lib";
import { z } from "zod";
const SpreadsheetTable = defineComponent({
name: "SpreadsheetTable",
description:
"Syncs data to the live spreadsheet panel. ONLY use this AFTER modification tools " +
"to push updated data to the user's spreadsheet. Always include colHeaders.",
props: z.object({
data: z.array(z.array(z.union([z.string(), z.number(), z.null()]))),
colHeaders: z.array(z.string()).optional(),
}),
component: ({ props }) => {
// Sends rows to TableContext, which reloads the Handsontable grid. Copy
// src/app/useSpreadsheetSync.ts and src/app/TableContext.tsx from the example.
// Required here, as in the example, so importing this file in Node (the
// `openui generate` script does) doesn't load the client-only hook.
const { useSpreadsheetSync } = require("@/app/useSpreadsheetSync");
useSpreadsheetSync(props.data, props.colHeaders);
return null;
},
});
// OpenUI's built-in components plus SpreadsheetTable, passed to <Renderer library>.
export const spreadsheetLibrary = createLibrary({
root: openuiLibrary.root ?? "Stack",
componentGroups: openuiLibrary.componentGroups,
components: [...Object.values(openuiLibrary.components), SpreadsheetTable],
});
```
</Step>
</Steps>