SpreadsheetGrid
Guides

Formatted copy

With copyFormat="rich", copies paste into Excel, Google Sheets and email as a table with borders and colors. copyHeaders adds a row of column names

With copyFormat="rich", copy (Ctrl/Cmd+C) and cut (Ctrl/Cmd+X) write a formatted table (HTML) to the clipboard together with the usual TSV (tab-separated text) (since 1.12.0. The default is 'text' = TSV only, as before). Pasted into Excel, Google Sheets, Word, Gmail or Outlook, it becomes a table with borders, alignment and colors. Pasting into this grid still reads the TSV.

<SpreadsheetGrid rows={rows} columns={columns} copyFormat="rich" copyHeaders />

Demo

Select a range and press Ctrl/Cmd+C, then click the box below and paste with Ctrl/Cmd+V to see roughly what pasting into Excel or an email looks like.

Rows 10 of 10Columns 8 of 8Sort: —
#
1
2
3
4
5
6
7
8
9
10
Rows 10 of 10Columns 8 of 8
Cell —Range —0 cells / 0 rows0 columns
Select a range and press Ctrl/Cmd+C, then click the box below and press Ctrl/Cmd+V (you can also try Excel, a spreadsheet or an email)

What gets written

  • Rows and columns are the same as the text copy (the selected range. Synthetic columns such as the row drag handle column are left out, and rows for which isRowExportable returns false are excluded).
  • Cell text is the text shown on screen (the valueFormatter text if any; a column showing 1280 as 1,280 gives 1,280). The TSV stays the raw values as before (or the text of formatClipboardValue / clipboard: 'label' columns). renderCell columns are text only, and data bars are text only (the bar isn't written). The aggregate cells of tree-data rows with children are the same aggregate text as on screen. Leading spaces and runs of spaces in the text may collapse into one depending on where you paste (an HTML rule; the TSV keeps them).
  • Look: borders on every cell, bold headers with a background color, and alignment from the column's align (headerAlign for headers).
  • Colors are written only for status chips (conditionalFormat.chips; only cells that draw a chip on screen, so not renderCell columns, checkboxes or multi-select cells) and color scales (colorScale), as both background and text color at the time of copying. The table's own background and text colors aren't written (the paste target's defaults stay). Even when the table is dark, the light colors are written (most paste targets have a white background). Colors you changed with a theme (the --ssg-* tokens) are used as long as the table is light.
  • Text columns: the text of columns other than number, date, time, date-time and checkbox, and the header names, are written with the "Text" format so Excel doesn't read "00123" as 123 or "1-2" as a date.
  • If building the table throws (a valueFormatter or chip function throws, for example), only the TSV is written (an error appears in the console in development).
  • Ranges over 20,000 cells write only the TSV (large HTML slows down the clipboard and the paste target).

Row of column names (copyHeaders)

With copyHeaders, a row of column names goes at the start of the copy (it works with copyFormat="text" too). With column groups (columnGroups), the group rows over the copied columns go above it too (the TSV writes the group name in the first column and leaves the rest empty = the same shape as CSV export; the formatted table merges the cells).

  • Not added to cuts (pasting would turn the headers into values).
  • When you paste text with headers copied from this grid straight back into this grid, the header rows are skipped and only the values are pasted (also after the dotted line is gone with Esc or editing, and after sorting). When you paste into another grid, or text copied again in another app, the header rows go in as values.

Browsers

The formatted table is written with navigator.clipboard.write (ClipboardItem). In browsers without ClipboardItem (Firefox up to 126) or when writing is refused, it's written through the copy event; if neither works, only the TSV is written (as before).

/lite

With /lite (createSpreadsheetGrid), copies are formatted only when you add richCopyFeature. Writing copyFormat="rich" without it gives TSV only, with one warning in development. copyHeaders is in the core, so it works without the feature.

import { createSpreadsheetGrid, richCopyFeature, conditionalFormatFeature } from '@ishibashi0112/spreadsheet-grid/lite';

const Grid = createSpreadsheetGrid({ features: { richCopy: richCopyFeature, conditionalFormat: conditionalFormatFeature } });

Chip and color-scale colors are written only when the conditional formatting feature (conditionalFormatFeature) is added too (without it, the screen shows plain cells as well).

On this page