Copy · Paste · Excel
Business users move back and forth between Excel and the grid. The clipboard uses the same tab-separated text as Excel, so data pastes either way as is. .xlsx files are read and written directly, with no external library.
import { createGrid } from 'horee-grid'
const rows = Array.from({ length: 12 }, (_, i) => ({ id: i + 1, month: new Date(2026, i).toLocaleString('en', { month: 'short' }), plan: (i + 1) * 1000, actual: null, memo: '' }))
const grid = createGrid(host, {
locale: 'en',
rowKey: 'id',
columns: [
{ field: 'month', title: 'Month', width: 70, align: 'center' },
{ field: 'plan', title: 'Plan', type: 'number', width: 110 },
{ field: 'actual', title: 'Actual', type: 'number', width: 110, editable: true },
{ field: 'memo', title: 'Memo', flex: 1, editable: true }
],
rows,
excel: { fileName: 'monthly_actuals', sheetName: 'Actuals' },
onPaste: r => {
const bad = r.invalid.map(x => `${x.column}="${x.text}"`).join(', ')
out(`Pasted ${r.changed} cells` + (r.skipped ? ` · skipped ${r.skipped}` : '') + (bad ? ` · invalid ${bad}` : '') + (r.overflow ? ` · ${r.overflow} extra lines` : ''))
}
})
toolbar.innerHTML = `
<button data-a="down">Download Excel</button>
<button data-a="up">Upload Excel</button>
<input type="file" accept=".xlsx" hidden>
<span class="ex-out">Click an Actual cell, then Ctrl+V numbers copied from Excel</span>`
const file = toolbar.querySelector('input')
toolbar.onclick = e => {
if (e.target.dataset.a === 'down') grid.exportExcel()
if (e.target.dataset.a === 'up') file.click()
}
file.onchange = async () => {
const { rows: read, invalid } = await grid.importExcel(file.files[0])
// The grid only reads the file. Your screen decides whether to append the rows
grid.setRows([...grid.getRows(), ...read.map((r, i) => ({ ...r, id: 1000 + i }))])
out(`Read ${read.length} rows · ${invalid.length} invalid values`)
file.value = ''
}
const out = text => (toolbar.querySelector('.ex-out').textContent = text)Range selection
| Action | Result |
|---|---|
| Drag | Select a range |
| Shift + click · Shift + arrow keys | Extend the range from the active cell |
| Ctrl + A | Everything |
| Esc | Clear the range |
Copy · Cut
| Action | Result |
|---|---|
| Ctrl + C | Copies the range as displayed. Thousands separators and date formats go to Excel as they are |
| Ctrl + X | Copies, then clears the editable cells |
| Delete | Clears the editable cells in the range |
Clearing (Ctrl + X · Delete) does not touch list (select) or check (check) cells, so that an empty value cannot become a value missing from the list.
To get the same text in code, use grid.copyText().
Paste
Ctrl + V fills from the top-left of the range.
- If you copied a single cell, the whole selected range is filled with that value.
- Only editable cells change. Read-only cells are skipped.
- Values go through the same validation as editing (including
parse). Thousands separators and many date shapes are accepted. - In
selectcells you can paste either the value (code) or the name. - No rows are added. Extra lines are dropped and reported in the result.
- Changed cells get change marks just like editing, and
onCellChangeis called.
Receive the result with onPaste. Tell users what was left out.
createGrid(host, {
columns, rows,
onPaste: r => {
// r = { changed, skipped, invalid: [{ row, column, text }], overflow }
if (r.invalid.length) toast(`${r.invalid.length} cells were not pasted because the values are invalid`)
if (r.overflow) toast(`${r.overflow} lines were not pasted because there are not enough rows`)
}
})To paste from code, use grid.pasteText(text); it returns the same result.
Excel download
Use Download Excel in the right-click menu, or in code:
await grid.exportExcel('purchases_202611') // without the extensionWhat is included:
- Visible columns, visible rows (after sort and filter) and fixed bottom rows (totals)
- Group headers as merged cells, header rows as frozen panes
- Column widths and borders
numberas number cells (thousands format,decimalsplaces),dateas date cells
createGrid(host, {
columns, rows,
excel: { fileName: () => `purchases_${today()}`, sheetName: 'Purchases' } // defaults for the menu item
})
createGrid(host, { columns, rows, excel: false }) // remove it from the menuTo decide a cell's value yourself, give the column exportValue:
{ field: 'kind', title: 'Type', format: kindName, exportValue: ({ value }) => value } // name on screen, code in ExcelIf you need a Blob instead of a download, use grid.toExcel(). With server data that has not been fully received, all rows are fetched separately with limit: 0 and exported.
Excel upload
const { rows, invalid } = await grid.importExcel(file)- Reads the first sheet. Only columns whose first-row text equals a column header (
title) are read. Column order does not matter. - Values are converted to each column's type. Invalid values are collected in
invalidas{ line, column, text }, wherelineis the Excel row number. - Nothing is put into the grid. Your screen decides whether to append new rows or match existing ones.
const { rows: read, invalid } = await grid.importExcel(file)
if (invalid.length) return showErrors(invalid.map(e => `Row ${e.line} ${e.column}: '${e.text}'`))
grid.setRows([...grid.getRows(), ...read.map(r => ({ ...r, id: tempId(), _new: true }))])For a few dozen rows, pasting is faster. For thousands of rows, or when you exchange a fixed Excel template, upload works better. Pasting never adds rows; upload has no row limit.
Excel without a grid
import { readXlsx, writeXlsx, downloadBlob } from 'horee-grid'
const sheets = await readXlsx(file) // [{ name, rows: value[][] }]
const blob = await writeXlsx({
name: 'Sheet1',
widths: [80, 160], // column widths (px)
header: [['Code', 'Name']], // header rows
rows: [['A1', 'Bolt'], ['A2', 'Nut']]
})
downloadBlob(blob, 'list.xlsx')