SpreadsheetGrid
Guides

Find in cells

Show a find bar on Ctrl+F with the find prop and step through highlighted matches. Replace with Ctrl+H too

With find, Ctrl/Cmd+F while the grid has focus (or handle.openFind()) shows a find bar at the top right of the grid (off by default). Unlike "filtering", it locates things without hiding rows. It also covers virtualized rows that the browser's Ctrl+F can't find.

<SpreadsheetGrid rows={rows} columns={columns} find />
  • It does partial matching on the display text (after valueFormatter = the text visible on screen) and highlights all matches. It's case-insensitive (find={{ caseSensitive: true }} makes it case-sensitive).
  • Enter / ↓ go to the next match and Shift+Enter / ↑ to the previous one (wrapping around). The active cell and scrolling follow. Retyping the query moves to the first match.
  • Esc / × close it, and focus returns to the grid body.
  • Scanning is time-sliced (10ms per chunk), so typing never stalls even with 100,000 rows. Matches stop at 10,000 (the count shows +).
  • When edits, sorting or filtering change the contents, it rescans automatically and keeps the current match if it's still there.

Demo

Rows 2000 of 2000Columns 5 of 5Sort: —
#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Rows 2000 of 2000Columns 5 of 5
Cell —Range —0 cells / 0 rows0 columns

You can also click a grid cell and press Ctrl/Cmd+F (2,000 rows)

Replace (find={{ replace: true }})

With find={{ replace: true }}, the find bar gets a replace row (since 1.12.0; the default is find only). The ▸ at the left edge of the find bar opens and closes it; Ctrl+H (Ctrl+H on Mac too) opens with the replace row open, and Ctrl/Cmd+F with it closed.

<SpreadsheetGrid rows={rows} onRowsChange={setRows} columns={columns} find={{ replace: true }} enableUndoRedo />
Rows 60 of 60Columns 6 of 6Sort: —
#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Rows 60 of 60Columns 6 of 6
Cell —Range —0 cells / 0 rows0 columns

You can also click a cell and press Ctrl+H. ▸ opens and closes the replace row; for "Selection only", select two or more cells first

  • Replace replaces the cell of the current match and moves to the next match (Enter in the replacement field does the same). All matches within one cell are replaced. Replace all replaces all current matches (it checks every row regardless of find's 10,000 limit). Both write with one onRowsChange, and undo (Ctrl/Cmd+Z) is one step. Case follows caseSensitive, and regular expressions aren't used.
  • Cells that are replaced are in text columns (the value is text and the screen text equals the value) and select (select) columns. In select columns the name on screen is replaced, and the value is written only when the result is an option's name (or value) (allowFreeText columns write the replaced text). Written values follow the same rules as paste (parseClipboardValue, validation with validationMode: 'reject').
  • Cells that are skipped: cells that can't be changed (readOnly / editable: false / canEditCell), number / date / time / date-time / checkbox / custom editor / multi-select columns, text columns whose display is changed by valueFormatter (except select columns), computed columns, and cells whose value wouldn't change by replacing (the same option in a select column, for example). Skipped matches keep the same highlight, and the notice row shows the counts, such as "Replaced 4, skipped 1 that cannot be replaced" (matches left after Replace all = skipped cells). When a single replace skips, it shows the reason (a cell that can't be changed, a column that can't be replaced, a name not in the options, the validation message, the value wouldn't change).
  • In selection: select two or more cells in the table first, then check it, and only that range is searched and replaced (counts, highlights and ↑↓ also stay within the range, which is shown in a light color). Even if you select a range, press Ctrl+H, type the search text and then check it (typing moves to the first match and the selection becomes one cell), the range selected before typing is used (the same order as Excel). The range is remembered by its rows when checked, so even if replaced rows leave the filter, rows outside the range don't get in (if all rows of the range leave, the range stays and there are 0 matches). Changing how the table looks, such as sorting, filtering, opening or closing groups, or reordering columns, unchecks it (if it unchecks in the middle of Replace all, it stops there).
  • Rows that are replaced are the rows shown in the table, as with find. Rows hidden by filters and rows inside collapsed groups aren't replaced. Tree data (treeData) also searches rows inside closed parents, so those rows are replaced too.
  • Replace all checks everything first, then writes once. If the find bar is closed partway, "In selection" unchecks, or the table keeps changing (it rechecks every time rows are replaced from outside, and if it doesn't finish in 3 tries it says so in the notice row), it stops without replacing anything (replaceAll resolves with { replaced: 0, skipped: 0 }). For tables updated from outside at short intervals (live updates and the like), pause the updates during Replace all.
  • Read-only tables (readOnly, no onRowsChange) don't show the replace row. It isn't available in serverSide (dataSource) tables (a warning in development, and find only).

Replacing from a button

On devices where Ctrl+H can't be pressed (tablets and so on), put a button that calls openFind({ replace: true }). Fixed replacements can be written with the handle's replaceAll(text) (it returns { replaced, skipped }). On touch, you can't drag across cells to select a range, so to use "In selection", select the range with the handle's selectRange before checking it.

const ref = useRef<SpreadsheetGridHandle<Row>>(null);

<button onClick={() => ref.current?.openFind({ query: 'Osaka', replace: true })}>Replace</button>
<button onClick={async () => {
  ref.current?.openFind({ query: 'Old Name Inc.', replace: true });
  const { replaced, skipped } = (await ref.current?.replaceAll('New Name Inc.')) ?? { replaced: 0, skipped: 0 };
  setMessage(`Replaced ${replaced}, skipped ${skipped}`);
}}>Fix the company name everywhere</button>
<SpreadsheetGrid ref={ref} find={{ replace: true }} rows={rows} onRowsChange={setRows} columns={columns} />

replaceNext(text) replaces the current match and moves to the next. In tables where replace isn't available, openFind opens find only, and replaceNext / replaceAll do nothing (0 matches). With /lite, add findReplaceFeature together with findFeature.

Putting a button that opens find

On phones and tablets where Ctrl/Cmd+F can't be pressed, the find bar can't be opened as is. Put a button that calls handle.openFind() in a toolbar or the like (the example below).

Not taking over the shortcut

Ctrl/Cmd+F is taken over only when the grid body has focus (not inside filter inputs or editors). To give priority to the page's own search, set shortcut: false and open it with handle.openFind() from a toolbar button or the like.

const ref = useRef<SpreadsheetGridHandle<Row>>(null);

<button onClick={() => ref.current?.openFind()}>Find</button>
<SpreadsheetGrid ref={ref} find={{ shortcut: false }} onFindChange={({ matchCount, currentIndex, truncated }) => setLabel(`${(currentIndex ?? 0) + 1} / ${matchCount}${truncated ? '+' : ''}`)} />

onFindChange passes { query, matchCount, currentIndex, open, truncated } (currentIndex is 0-based, null when there is no match). Matches stop at 10,000, and then truncated is true (use it to show a "+" like the built-in bar's "10000+". Since 1.2.0). The imperative API is openFind(query?) / closeFind() / findNext() / findPrev().

Look

Matches are .ssg-find-mark and the current one is .ssg-find-mark--current; change their colors with the tokens --ssg-find-mark-bg / --ssg-find-mark-current-bg. Add classes / styles to the find bar with classNames.findBar.

Limitations

  • With SSRM (dataSource), only loaded ranges are covered (unloaded rows are skipped).
  • renderCell columns are searched by their display text, but the <mark> highlight is for default cells only (moving to the current match works the same).
  • Cells whose text is changed by the label of status chips (conditionalFormat.chips) are searched by the label shown on screen (since 1.0.2).
  • Among columns without renderCell, checkbox editor columns and data bar columns with showValue: false (columns that show no text on screen) aren't covered.
  • Only displayed rows are searched. Rows inside collapsed groups, group source columns hidden by grouping (values shown only in group row labels), and the text of label rows (labelRow) aren't searched. To search by the values of a group source column, narrow with the global filter.

On this page