View as Markdown

Clipboard

Clipboard

Copy cell ranges, selected rows or the focused cell as TSV with or without headers, paste spreadsheet blocks into edit drafts, formatted or raw copy text per column, `onBeforeCopy` and `onBeforePaste`, and the fallbacks when the browser blocks clipboard access.

A grid with editing copies and pastes like a spreadsheet: the editing prop registers the core clipboard module, and the keyboard, toolbar and menu wiring lives in the lazy editing chunk, so a grid without editing loads none of it. A read-only grid that should still copy turns the capabilities off:

<DataGrid data={orders} columns={columns} editing={{ edit: false, add: false, delete: false }} selection={{ mode: 'cell', cellRange: 'box' }} />

Copy

Ctrl+C (⌘C on macOS) copies, Ctrl+Shift+H copies with the header row. api.copy({ includeHeaders }) does the same from code and returns applied, or rejected with nothingToCopy or cancelled. The toolbar item 'copy' and the context menu item 'copy' copy the selection too.

The text is TSV: fields holding a tab, a newline or a quote are quoted with "". A copy takes, in order of priority, the bounding box of the selected cell ranges, else the selected rows, else the focused cell; only data rows (group captions and footers are left out), and a row number column copies empty. Pending drafts are copied as shown. A copy above one million cells is refused.

Formatted or raw text

A copy uses the display text of each cell by default: formats, lookup display values and valueFormatter output. column.clipboardFormatter(value, ctx) replaces that text for one column, for example a raw number for a currency column. api.copy({ copyFormatted: false }) copies raw values instead (numbers unformatted, dates as ISO strings) for every column except those with a clipboardFormatter, which always wins; lookup columns still copy their display text. Keyboard and menu copies use the display text.

Paste

Ctrl+V (⌘V) pastes TSV (quoted fields, CRLF, CR or LF) from the top-left cell of the selected range, or from the focused cell. api.paste(text) pastes text from code; api.paste() without text reads the system clipboard.

  • one value fills the whole selected range; a larger block is clipped to the range or the grid edge;
  • read-only cells (editable: false, isCellEditable, primary keys on existing rows) and utility columns are skipped;
  • values are parsed by the column type and validated like typed input (accepting a copied $18.50 or 25% back);
  • in cell mode the values are written at once; in the other modes they become drafts (in batch mode marked until Save).

Events

<DataGrid
  editing={{ mode: 'batch' }}
  selection={{ mode: 'cell', multiple: true, cellRange: 'box' }}
  onBeforeCopy={(e) => {
    // e.data: the TSV text; e.copied: 'range' | 'rows' | 'cell'; e.cells, e.rowIds, e.fields, e.includeHeaders
    e.data = e.data.replaceAll('\t', ';');
  }}
  onBeforePaste={(e) => {
    // e.data: the clipboard text; e.target: the top-left target cell
    if (e.data.length > 100_000) e.preventDefault();
  }}
/>

onBeforeCopy and onBeforePaste are cancellable with preventDefault(), and assigning event.data replaces the text that is written or pasted; data is the one writable field of these otherwise frozen event objects. e.cells is the number of copied data cells. Paste raises one event per paste with the whole text, not one per cell: per-cell conversions belong in the column parser.

Without clipboard access

The async Clipboard API exists only in secure contexts (HTTPS, localhost) and can be blocked by permissions. The grid falls back instead of failing:

  • Copy writes with navigator.clipboard.writeText when it is available, else selects the text in a hidden textarea and runs the browser's copy command. When both are refused, a small dialog shows the text selected: press Ctrl/⌘+C, and the dialog closes.
  • Ctrl/⌘+V never needs the Clipboard API: the keystroke moves focus to a hidden textarea for an instant, the browser pastes into it, and the grid reads the paste event. There is no permission prompt.
  • api.paste() without text reads navigator.clipboard.readText(), or opens a paste dialog: press Ctrl/⌘+V in it.

The dialogs are native <dialog> elements with the grid's localized texts (clipboardCopyHint, clipboardPasteHint, clipboardText, copy, paste); focus returns to the grid when they close.

Copy and paste

Open demo

Copy a cell range, the selected rows or the focused cell as TSV, with or without headers; paste TSV into batch drafts, skipping read-only cells; raw and display-text copies; onBeforeCopy / onBeforePaste changing or cancelling the text.

Order
Customer
Category
Country (read-only)
Qty
Price
Amount (raw copy)
ORD-10000
Dewi Lestari
Coffee beans
Japan
11
$14.00
$154.00
ORD-10001
Farah Aziz
Loose tea
France
2
$12.75
$25.50
ORD-10002
Farah Aziz
Loose tea
France
12
$12.75
$145.35
ORD-10003
Dewi Lestari
Coffee beans
Germany
10
$14.00
$133.00
ORD-10004
Dewi Lestari
Coffee beans
Japan
1
$14.00
$11.90
ORD-10005
Chen Wei
Brewing equipment
Malaysia
4
$42.00
$159.60
ORD-10006
Jun Park
Loose tea
Malaysia
3
$12.75
$32.51
ORD-10007
Elif Kaya
Brewing equipment
Germany
9
$129.00
$1,102.95
ORD-10008
Jun Park
Coffee beans
Japan
11
$14.00
$146.30
ORD-10009
Goran Petrov
Brewing equipment
Japan
11
$129.00
$1,419.00
ORD-10010
Bao Nguyen
Coffee beans
Japan
6
$18.50
$99.90
ORD-10011
Jun Park
Loose tea
France
12
$9.25
$105.45
ORD-10012
Jun Park
Coffee beans
Japan
6
$14.00
$84.00
ORD-10013
Jun Park
Brewing equipment
Japan
9
$64.90
$584.10
ORD-10014
Jun Park
Brewing equipment
Portugal
4
$64.90
$246.62
ORD-10015
Farah Aziz
Brewing equipment
Malaysia
10
$129.00
$1,161.00
ORD-10016
Bao Nguyen
Coffee beans
Japan
9
$18.50
$141.53
ORD-10017
Chen Wei
Brewing equipment
Portugal
5
$42.00
$189.00
ORD-10018
Dewi Lestari
Loose tea
Japan
3
$12.75
$36.34
ORD-10019
Ines Duarte
Loose tea
Portugal
7
$9.25
$64.75
No events yet.

API reference

See DataGridClipboardApi, DataGridClipboardProps and ColumnDef (clipboardFormatter) in the API reference.