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.
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
isRowExportablereturnsfalseare excluded). - Cell text is the text shown on screen (the
valueFormattertext if any; a column showing1280as1,280gives1,280). The TSV stays the raw values as before (or the text offormatClipboardValue/clipboard: 'label'columns).renderCellcolumns 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(headerAlignfor headers). - Colors are written only for status chips (
conditionalFormat.chips; only cells that draw a chip on screen, so notrenderCellcolumns, 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
123or "1-2" as a date. - If building the table throws (a
valueFormatteror 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
Escor 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).
Selection and clipboard
Cell range selection, row selection (checkboxes), and the rules for copy and paste that round-trip with Excel
Saving and restoring state
Save the column widths, filters, sorting and column order / visibility / pinning users changed, and bring them back next time (getState / applyState / initialState / restoreColumnsFromState)