SpreadsheetGrid
Guides

Filter with words (AI)

Place GridAskBar, and AI turns a sentence like "Tokyo items with 10 or fewer in stock, highest price first" into filters, sorting and search

Place GridAskBar from @ishibashi0112/spreadsheet-grid/ai above the grid, and a sentence the user writes in everyday words is sent to AI and applied to the grid's filters, sorting, global search and column visibility (since 0.57.0). Instead of opening the filter screen and entering conditions one by one, just write "Electronics that arrived this month, highest price first".

  • The grid doesn't call AI. It asks through the ask function you pass (your server → any AI), so any kind of AI works, such as Claude, OpenAI, Gemini or Vercel AI Gateway, and the API key stays on your server.
  • Row data isn't sent. Only column names and kinds, options in the column definitions, the current conditions and the user's sentence are sent.
  • Every answer is checked. Missing columns, conditions that column can't use, and values not among the options are dropped, and what was dropped is shown. Applied conditions are shown as chips and can be undone in one step.
  • It's a separate entry from the full build and /lite, so it goes into the bundle only when used (the full build's size doesn't change).

To have a chat AI operate the table many times in a conversation (reading rows and writing cells too), use the tools from the same entry (Let AI agents operate the table).

Three steps to set up

  1. Put filterType on columns. The conditions AI can enter are the same as that column's filter screen. Make numeric and date columns 'number' / 'numberSet' / 'dateSet' (What the AI can do).
  2. Place GridAskBar above the grid. Pass the grid's ref, the same column definitions and ask. ask just sends the request to your server (Client side).
  3. Add one file to your server. It passes the received request to AI and returns the answer. There are examples for Claude, OpenAI, Gemini and the Vercel AI SDK (AI Gateway) (Server side).

To keep the AI API key out of the browser, you need a server such as a Next.js Route Handler, Hono or Express (for pages built with Vite alone, prepare a separate place for the API).

Demo

This site doesn't call AI; pressing an example sentence runs with a sample answer. The sample answers were recorded by actually asking Claude's fast model (Haiku class) the Japanese version of these sentences on this table and keeping the answers as returned (2026-10-28); on the English page, the values in them are replaced with the English words. Checking the answer, applying it to the table, the chips and undo are the real components. Press them from the top to check the following.

  • Reading paraphrases: "Writing supplies" → Category "Stationery", "under 1,000 yen" → Unit price below 1000, "western Japan" → Warehouse "Osaka, Fukuoka", "over the last month" → Arrived "Last 30 days", "price" → Unit price, "Sold-out" → Stock 0
  • Follow-up requests: "Only ¥10,000 and up from those" adds to the current conditions
  • Telling you what it did with things not in the columns: there's no color column, so it searches the whole table for the text "red" and says so

Under "The request sent to the AI and its answer" below, you can see what's passed to ask (no row data) and the answer.

Loading…

Client side

Pass the grid's ref, the same column definitions and ask to GridAskBar. The client code is the same whichever AI you use. In the Solid version, import from @ishibashi0112/spreadsheet-grid-solid/ai (since 1.4.0), and pass grid a function returning the handle (grid={() => grid}) or a signal (Notes for Solid).

'use client';
import { useRef } from 'react';
import { SpreadsheetGrid, type SpreadsheetGridHandle } from '@ishibashi0112/spreadsheet-grid';
import { GridAskBar, type GridAsk } from '@ishibashi0112/spreadsheet-grid/ai';
import '@ishibashi0112/spreadsheet-grid/style.css';

// ask AI through your own server (keep the API key out of the browser)
const ask: GridAsk = async (request, { signal }) => {
  const res = await fetch('/api/grid-ask', {
    method: 'POST',
    headers: { 'content-type': 'application/json' },
    body: JSON.stringify(request),
    signal,
  });
  if (!res.ok) throw new Error(`grid-ask: ${res.status}`);
  return res.json();
};

export function Inventory() {
  const gridRef = useRef<SpreadsheetGridHandle<Item>>(null);
  return (
    <>
      <GridAskBar
        grid={gridRef}
        columns={columns}
        ask={ask}
        examples={['Tokyo items with 10 or fewer in stock', 'Electronics that arrived this month, highest price first']}
        columnHints={{ stock: 'units', price: 'yen, before tax' }}
      />
      <SpreadsheetGrid ref={gridRef} columns={columns} rows={rows} />
    </>
  );
}

Server side

Just pass the received system (fixed instructions), prompt (column descriptions, the current conditions, the user's sentence) and schema (JSON Schema for the answer's shape) to AI, and return the answer. schema can be passed as is to each provider's structured output (the feature that fixes the answer to JSON of that shape). All are examples of a Next.js Route Handler (app/api/grid-ask/route.ts). The Vercel AI SDK example goes through AI Gateway, so changing the model string alone switches to another provider's AI.

import Anthropic from '@anthropic-ai/sdk';

const anthropic = new Anthropic(); // ANTHROPIC_API_KEY is a server environment variable

export async function POST(req: Request) {
  // check sign-in and call limits here
  const { system, prompt, schema } = await req.json();
  const message = await anthropic.messages.create({
    model: 'claude-haiku-5-5', // a small, fast model is enough
    max_tokens: 4096, // the limit includes thinking
    system,
    messages: [{ role: 'user', content: prompt }],
    // effort: 'low' reduces thinking for faster answers. format fixes the answer to the schema's shape.
    output_config: { effort: 'low', format: { type: 'json_schema', schema } },
  });
  // return the answer text as is (the bar reads and checks it; a cut-off answer also becomes "couldn't read").
  const text = message.content.find((block) => block.type === 'text');
  return Response.json(text ? text.text : null);
}
  • Return the answer's JSON (an object or a JSON string both work). The bar reads and checks it, so cut-off answers or answers of the wrong shape ({ error } and so on) are reported without changing the table. For server errors, checking res.ok in ask on the client and throwing gives "Couldn't reach the AI. Try again." with "Try again".
  • AI other than the 4 above works the same if you pass schema as JSON Schema. For AI without structured output, you can append "return only JSON matching this JSON Schema" after prompt and return the string you get as is (surrounding text and ```json fences are stripped when read).
  • On public pages, add sign-in checks and call limits on the server side (otherwise anyone can spend your AI costs).

What the AI can do

The conditions AI can enter are the same as those you can enter by hand on that column's filter screen (decided by the column's filterType). It doesn't disagree with the filter screen, and with tables read from a server (SSRM) it doesn't disagree with the server's processing either.

Column filterTypeConditions AI can enter
textContains
textSetContains / equals / starts with / ends with / blank / not blank, and choosing values
number / numberSet= / ≠ / > / ≥ / < / ≤ / between / blank / not blank (numberSet also choosing values)
dateContains (the date as text; tell AI how cells are written with columnHints)
dateSetBetween / on or after / on or before / equals / not equal / today, this month, last 30 days / blank, and choosing values
select / setMatching one value / choosing values (include or exclude)
  • Columns without filterType can't be filtered (only sorted). Put filterType on numeric and date columns ('number' / 'numberSet' / 'dateSet'; 'auto' columns get that kind if the editor is number / date, otherwise only "contains").
  • Options for choosing values are shown to AI from filterOptions (or the select editor's options). In columns without options, AI uses the values it wrote from the sentence as is. Blank cells ("(Blank)" on the filter screen) can be chosen too.
  • Besides that, it changes sorting (including multiple columns) and the global search text. Hiding and showing columns happens only with allowColumnVisibility (the grid needs onColumnsChange).
  • When the grid has enableColumnFilter / enableSorting / enableGlobalFilter set to false, pass allowFilter / allowSort / allowSearch as false to the bar too (so AI doesn't enter conditions that can't be removed from the table).
  • Follow-up requests like "only ¥10,000 and up from those" work (the current conditions are sent to AI every time). Earlier sentences aren't remembered.
  • Answering by looking at row data ("best sellers", "odd values" and so on), grouping rows, and column order and pinning aren't covered.

Column hints and hidden columns

With columnHints, you can tell AI the unit or how values are written per column. Columns set to false aren't shown to AI (and conditions on them aren't changed).

<GridAskBar
  columnHints={{
    price: 'yen, before tax',
    code: 'codes look like A-1001',
    cost: false, // don't show cost to AI
  }}
  {...props}
/>

Input and undo

  • Enter sends. Enter that confirms IME composition doesn't send.
  • While thinking, "Stop" or Esc cancels (answers arriving late are discarded). Esc works when focus is inside the bar or nowhere (Esc inside the table belongs to the table).
  • Applied conditions are shown as chips, and × removes only that condition (select / set values use display names, and numbers use the column's valueFormatter). "Undo" returns to the view before asking. When you change the table by hand after applying, the chips and "Undo" are withdrawn and the bar says so.
  • When the answer can be read, the input is cleared (on failure it stays so you can fix it and send again).

Look and your own UI

The bar's texts are Japanese by default. For English, pass gridAskLocaleTextEn from the same entry as the grid, @ishibashi0112/spreadsheet-grid/locales/en, to localeText (localeTextEn for the grid; English and text). The instructions sent to AI are the same regardless of language (only the names of date period presets follow the screen's language), and users can write in any language.

import { gridAskLocaleTextEn, localeTextEn } from '@ishibashi0112/spreadsheet-grid/locales/en';

<GridAskBar grid={gridRef} columns={columns} ask={ask} localeText={gridAskLocaleTextEn} />
<SpreadsheetGrid ref={gridRef} columns={columns} rows={rows} localeText={localeTextEn} />

Colors are decided by the same tokens as the grid (--ssg-*), and themes/*.css applies to the bar too. The bar is outside the grid, so when you override tokens, list .ssg-ask too, like .ssg-root, .ssg-ask { --ssg-accent: … }. For dark mode, pass the same theme as the grid.

To build it with your own look, useGridAsk (createGridAsk in the Solid version; it returns the same values, and status and so on return the current value when read) returns only the state and behavior (status / session / submit / cancel / undo / remove). submit prevents double sending, but not sending on Enter that confirms IME composition, stopping with Esc while asking, resending the sent sentence on failure, and clearing the input when the answer can be read are things you write yourself if you want them to match the bar (how is in the API reference). Building the request (createGridAskRequest) and checking the answer (readGridAskAnswer) are framework-independent functions, also usable from @ishibashi0112/spreadsheet-grid-core. When applying with them, remove columns before passing to applyState if hidden columns didn't change (isGridAskColumnsChanged; passing it as is may bring back columns hidden by hand while asking). For details, see the API reference.

Choosing an AI model

One request is about a thousand to several thousand input tokens (depending on the number of columns and options), and a few hundred output tokens. It only builds conditions from a sentence, so a small, fast model is enough (Haiku for Claude, GPT-6 Luna for OpenAI, Flash for Gemini). Cheap "decision-only" models that just pick one of several choices don't suit this feature (it needs to freely build numeric and date values and several conditions).

On this page