FreeGridv1.15.1한국어

Cell ranges and range copy

Excel-style cell ranges by drag, Shift and Ctrl, copied to and pasted from Excel as blocks, cut, cleared and totalled in the status bar.

Live demoCell ranges & range copy

Excel-style cell ranges, several at once, copied to and pasted from Excel, with totals of the selection.

Code for this demo
import { createGrid, type GridApi } from '@freegridjs/core';
import { demoColumns, makeRows } from '../data';
import { button, separator, text, toggle, tr, type DemoContext } from '../ui';

export default function mount(ctx: DemoContext) {
  const { lang } = ctx;
  // Paste only writes to columns marked editable.
  const editable = new Set(['name', 'q1', 'q2', 'q3', 'q4', 'joined', 'active', 'status']);
  const columns = demoColumns(lang).map(function mark(c): typeof c {
    if (c.children) return { ...c, children: c.children.map(mark) };
    // email cannot be copied: it pastes as an empty cell
    if (c.field === 'email') return { ...c, copyable: false, cellClass: 'demo-secret' };
    return editable.has(c.field ?? '') ? { ...c, editable: true } : c;
  });
  const data = makeRows(500);
  let grid: GridApi;

  const create = (rangeSelection: boolean) => {
    grid?.destroy();
    grid = createGrid(ctx.host, {
      columns,
      data,
      rowSelection: 'multiple',
      locale: lang,
      rangeSelection, // drag, Shift+click, Shift+arrows and Ctrl+A select cell ranges (Excel-style)
      statusBar: true, // count, sum and average of the selected cells
    });
    grid.on('rangeChange', e => e.range && ctx.log(`rangeChange → ${e.range.start.colId} ${e.range.start.rowIndex + 1} : ${e.range.end.colId} ${e.range.end.rowIndex + 1}`));
    grid.on('cellValueChanged', e => ctx.log(`cellValueChanged → row ${e.rowIndex + 1} ${e.colId}: ${String(e.oldValue)} → ${String(e.newValue)}`));
    grid.on('paste', e => ctx.log(`paste → changed ${e.changed}, skipped ${e.skipped}`));
  };
  create(true);

  text(ctx, tr(lang, {
    en: 'Drag across cells, press Ctrl+C and paste into Excel. Copy cells in Excel, click a Name or Q1–Q4 cell and press Ctrl+V. Hold Ctrl to add more ranges; the bar below totals them. The Email column cannot be copied.',
    ko: '셀을 드래그하고 Ctrl+C를 누른 뒤 엑셀에 붙여넣어 보세요. 엑셀에서 복사한 셀은 이름이나 1~4분기 셀을 클릭하고 Ctrl+V로 붙여넣습니다. Ctrl을 누른 채 드래그하면 범위를 더 추가하고, 아래 막대에 합계가 나옵니다. 이메일 컬럼은 복사되지 않습니다.',
  }));
  separator(ctx);
  toggle(ctx, tr(lang, { en: 'Drag selects cell ranges', ko: '드래그로 셀 범위 선택' }), true, on => {
    create(on);
    ctx.log(`rangeSelection: ${on}`);
  });
  button(ctx, tr(lang, { en: 'Copy with headers', ko: '헤더 포함 복사' }), () => {
    const tsv = grid.copySelection({ headers: true });
    ctx.log(tr(lang, { en: `copied ${tsv.split('\r\n').length} lines`, ko: `${tsv.split('\r\n').length}줄 복사됨` }));
  });
  button(ctx, tr(lang, { en: 'Paste sample into Name, Q1', ko: '이름·1분기에 예시 붙여넣기' }), () => {
    grid.setRange({ start: { rowIndex: 0, colId: 'name' }, end: { rowIndex: 0, colId: 'name' } });
    grid.pasteText('Kim Minsu\t1,234,000\r\nLee Jiwoo\t(50)');
  });
}

Select cells like in Excel: drag across them, Shift-click, use Shift with the arrow keys, or press Ctrl+A for everything. Hold Ctrl (⌘) while clicking or dragging to add more ranges. Clicking a row number selects the whole row.

Ctrl+C copies the ranges in a format Excel and Google Sheets understand: numbers stay numbers, dates become YYYY-MM-DD. Ranges in the same columns or rows copy together, as in Excel. Ctrl+V pastes a block copied from Excel starting at the selected cell, and a single copied value fills the whole selected range. Ctrl+X copies and then clears the editable cells, and Delete clears every range; each is one undo step.

Pasting only changes editable columns and converts each value to the column type, the same way as pasting a single value; values that cannot be converted are skipped and counted in the paste event. Columns with copyable: false copy as empty text. With statusBar: true, the bar under the rows shows the count, sum, average, minimum and maximum of the selected cells.

const grid = createGrid(el, {
  columns: [
    { field: 'id', type: 'number' },                        // read-only
    { field: 'name', editable: true },                      // paste allowed
    { field: 'qty', type: 'number', editable: true },       // "1,234", "(5)", "12%" are parsed
    { field: 'due', type: 'date', editable: true },         // "2026-03-05", "2026. 3. 5." …
    { field: 'rrn', copyable: false },                      // copies as empty text
  ],
  data,
  rangeSelection: true, // default: drag / Shift+click / Shift+arrows / Ctrl+A
});

grid.on('rangeChange', e => console.log(e.range));
grid.on('paste', e => console.log(e.changed, 'cells changed'));

grid.setRange({ start: { rowIndex: 0, colId: 'name' }, end: { rowIndex: 9, colId: 'qty' } });
grid.getRanges();                      // every range, the active one last
grid.copySelection({ headers: true }); // TSV, also written to the clipboard
grid.cutSelection();                   // copy, then clear the editable cells (Ctrl+X)
grid.pasteText('Kim	1,234
Lee	56');

Turn ranges off with rangeSelection: false: a click then focuses a single cell. Drag the corner of a range to fill cells with the fill handle.

Keys

KeyAction
Shift + ← ↑ → ↓ / Home / End / PageUp / PageDownExtend the range.
Ctrl+ASelect every cell.
Ctrl+C / Ctrl+X / Ctrl+VCopy, cut and paste the ranges.
DeleteClear the editable cells of every range.

Options, methods and events

NameTypeDescription
rangeSelectionbooleanCell ranges by drag, Shift+click, Shift+arrow keys and Ctrl+A (default true).
getRange() / setRange(range | null)GridRangeThe active range: { start: { rowIndex, colId }, end: { rowIndex, colId } }.
getRanges() / setRanges(ranges)GridRange[]Every range, including those added with Ctrl+click or Ctrl+drag; the last one is active.
copySelection({ headers? }) / cutSelection()stringCopy the ranges as Excel TSV (and to the clipboard); cut also clears their editable cells in one undo step.
pasteAddsRowsboolean | ({ index }) => RowOption: lines pasted past the last row become new rows (one undo step); a function makes each row.
pasteText(text){ changed, skipped, added? }Paste a block of TSV / Excel text at the range or the focused cell.
rangeChange{ range } (event)The range changed.