엑셀에서 복사한 칸 붙여넣기Paste cells copied from a spreadsheet
엑셀이나 시트에서 복사한 칸을 Ctrl+V로 붙여 넣으면 고른 칸부터 채워져요. 칸 편집과 같은 검증을 거치고, 안 맞는 칸은 건너뛰며 몇 칸이 들어갔는지 알려 줘요. 코드로 붙여 넣고 복사할 수도 있어요.Paste cells copied from Excel or a sheet with Ctrl+V and they fill from the selected cell. They go through the same validation as edits, bad cells are skipped and the count is reported. You can call pasteCells and copyCells from code too.
코드Code
탭을 골라 코드를 복사하세요. 위 화면은 이 코드를 그대로 실행한 결과예요.Pick a tab and copy the code. The preview above runs exactly this code.
이 화면은 새 모양 옵션을 켠 모양이에요: density: 'spacious', iconStyle: 'soft', motion: true (코드에 없으면 기본 모양으로 보여요)This preview has the new-look options on: density: 'spacious', iconStyle: 'soft', motion: true (without them in your code you get the default look)
버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<div id="app">
<div class="toolbar">
<button type="button" id="paste">붙여넣기</button>
<button type="button" id="pasteBad">잘못된 값 붙여넣기</button>
<button type="button" id="copy">복사 글자 보기</button>
<button type="button" id="undo">되돌리기</button>
<button type="button" id="redo">다시 실행</button>
<button type="button" id="discard">변경 버리기</button>
</div>
<div id="grid" style="height: 290px"></div>
</div>
<script type="module" src="./main.js"></script>import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('paste').addEventListener('click', () => actions.paste(api));
document.getElementById('pasteBad').addEventListener('click', () => actions.pasteBad(api));
document.getElementById('copy').addEventListener('click', () => actions.copy(api));
document.getElementById('undo').addEventListener('click', () => actions.undo(api));
document.getElementById('redo').addEventListener('click', () => actions.redo(api));
document.getElementById('discard').addEventListener('click', () => actions.discard(api));const rows = [
{ id: 1, item: 'A4 복사지', qty: 10, price: 4500 },
{ id: 2, item: '토너', qty: 2, price: 79000 },
{ id: 3, item: '서류 파일', qty: 30, price: 1200 },
{ id: 4, item: '보드 마카', qty: 12, price: 2500 },
{ id: 5, item: '포스트잇', qty: 20, price: 1800 },
];
// 엑셀·구글 시트에서 복사한 칸은 "탭으로 칸을, 줄바꿈으로 줄을" 나눈 글자로 와요.
// pasteCells(글자)는 고른 칸(없으면 포커스 칸)부터 그 글자를 채워 넣어요. 칸 편집과 똑같은 검증을 거치고,
// 숫자 칸에 글자가 오는 것처럼 안 맞는 칸은 건너뛰어서 몇 칸이 들어가고 몇 칸이 빠졌는지 알려 줘요.
export const options = {
columns: [
{ id: 'item', title: '품목', width: 150, editable: true },
{
id: 'qty',
title: '수량',
kind: 'number',
width: 90,
align: 'end',
editable: true,
validation: { min: 0 },
},
{
id: 'price',
title: '단가',
kind: 'number',
width: 110,
align: 'end',
editable: true,
formatValue: (v) => `${Number(v).toLocaleString('ko-KR')}원`,
},
],
rows,
rowKey: 'id',
cellSelection: 'multiple',
saveMode: 'batch',
};
let say = () => { };
export function setup(api, log) {
say = log;
log({
ko: '칸을 하나 눌러 둔 뒤 버튼을 누르거나, 엑셀에서 복사한 칸을 Ctrl+V로 붙여 보세요.',
en: 'Select one cell, then press a button, or copy cells from Excel and paste them with Ctrl+V.',
});
return api.on('cellsPasted', ({ pasted, skipped }) => log({
ko: `붙여넣기: ${pasted}칸 바꿨고 ${skipped}칸은 건너뛰었어요.`,
en: `Paste: ${pasted} cells changed, ${skipped} skipped.`,
}));
}
export const actions = {
// 1번 줄 "품목"부터 3줄×3칸을 채워요.
paste: (api) => {
if (!api.getSelectedCells().length)
return say({
ko: '먼저 붙여 넣기 시작할 칸을 하나 눌러 주세요.',
en: 'Select the cell to start pasting from first.',
});
api.pasteCells('A4 복사지\t25\t5000\n토너\t3\t75000\n서류 파일\t40\t1000');
},
// 두 번째 줄의 수량에 숫자가 아닌 글자가 있어요. 그 칸만 건너뛰어요.
pasteBad: (api) => {
if (!api.getSelectedCells().length)
return say({
ko: '먼저 붙여 넣기 시작할 칸을 하나 눌러 주세요.',
en: 'Select the cell to start pasting from first.',
});
api.pasteCells('A4 복사지\t25\t5000\n토너\t많이\t75000\n서류 파일\t-5\t1000');
},
// 고른 칸을 엑셀에 붙는 글자로 꺼내 봐요. 맨 위에 컬럼 제목을 붙일 수도 있어요.
copy: (api) => {
if (!api.getSelectedCells().length)
return say({ ko: '먼저 복사할 칸을 눌러 주세요.', en: 'Select the cells to copy first.' });
const text = api.copyCells({ header: 'columns' });
say({
ko: `복사된 글자: ${text.replaceAll('\t', ' | ').replaceAll('\n', ' / ').replaceAll(String.fromCharCode(13), '')}`,
en: `Copied text: ${text.replaceAll('\t', ' | ').replaceAll('\n', ' / ').replaceAll(String.fromCharCode(13), '')}`,
});
},
undo: (api) => say({
ko: api.undo() ? '되돌렸어요.' : '되돌릴 것이 없어요.',
en: api.undo() ? 'Undone.' : 'Nothing to undo.',
}),
redo: (api) => say({
ko: api.redo() ? '다시 넣었어요.' : '다시 실행할 것이 없어요.',
en: api.redo() ? 'Redone.' : 'Nothing to redo.',
}),
discard: (api) => api.rollbackChanges(),
};<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>엑셀에서 복사한 칸 붙여넣기</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="paste">붙여넣기</button>
<button type="button" id="pasteBad">잘못된 값 붙여넣기</button>
<button type="button" id="copy">복사 글자 보기</button>
<button type="button" id="undo">되돌리기</button>
<button type="button" id="redo">다시 실행</button>
<button type="button" id="discard">변경 버리기</button>
</div>
<div id="grid" style="height: 290px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const rows = [
{ id: 1, item: 'A4 복사지', qty: 10, price: 4500 },
{ id: 2, item: '토너', qty: 2, price: 79000 },
{ id: 3, item: '서류 파일', qty: 30, price: 1200 },
{ id: 4, item: '보드 마카', qty: 12, price: 2500 },
{ id: 5, item: '포스트잇', qty: 20, price: 1800 },
];
// 엑셀·구글 시트에서 복사한 칸은 "탭으로 칸을, 줄바꿈으로 줄을" 나눈 글자로 와요.
// pasteCells(글자)는 고른 칸(없으면 포커스 칸)부터 그 글자를 채워 넣어요. 칸 편집과 똑같은 검증을 거치고,
// 숫자 칸에 글자가 오는 것처럼 안 맞는 칸은 건너뛰어서 몇 칸이 들어가고 몇 칸이 빠졌는지 알려 줘요.
const options = {
columns: [
{ id: 'item', title: '품목', width: 150, editable: true },
{
id: 'qty',
title: '수량',
kind: 'number',
width: 90,
align: 'end',
editable: true,
validation: { min: 0 },
},
{
id: 'price',
title: '단가',
kind: 'number',
width: 110,
align: 'end',
editable: true,
formatValue: (v) => `${Number(v).toLocaleString('ko-KR')}원`,
},
],
rows,
rowKey: 'id',
cellSelection: 'multiple',
saveMode: 'batch',
};
let say = () => { };
function setup(api, log) {
say = log;
log({
ko: '칸을 하나 눌러 둔 뒤 버튼을 누르거나, 엑셀에서 복사한 칸을 Ctrl+V로 붙여 보세요.',
en: 'Select one cell, then press a button, or copy cells from Excel and paste them with Ctrl+V.',
});
return api.on('cellsPasted', ({ pasted, skipped }) => log({
ko: `붙여넣기: ${pasted}칸 바꿨고 ${skipped}칸은 건너뛰었어요.`,
en: `Paste: ${pasted} cells changed, ${skipped} skipped.`,
}));
}
const actions = {
// 1번 줄 "품목"부터 3줄×3칸을 채워요.
paste: (api) => {
if (!api.getSelectedCells().length)
return say({
ko: '먼저 붙여 넣기 시작할 칸을 하나 눌러 주세요.',
en: 'Select the cell to start pasting from first.',
});
api.pasteCells('A4 복사지\t25\t5000\n토너\t3\t75000\n서류 파일\t40\t1000');
},
// 두 번째 줄의 수량에 숫자가 아닌 글자가 있어요. 그 칸만 건너뛰어요.
pasteBad: (api) => {
if (!api.getSelectedCells().length)
return say({
ko: '먼저 붙여 넣기 시작할 칸을 하나 눌러 주세요.',
en: 'Select the cell to start pasting from first.',
});
api.pasteCells('A4 복사지\t25\t5000\n토너\t많이\t75000\n서류 파일\t-5\t1000');
},
// 고른 칸을 엑셀에 붙는 글자로 꺼내 봐요. 맨 위에 컬럼 제목을 붙일 수도 있어요.
copy: (api) => {
if (!api.getSelectedCells().length)
return say({ ko: '먼저 복사할 칸을 눌러 주세요.', en: 'Select the cells to copy first.' });
const text = api.copyCells({ header: 'columns' });
say({
ko: `복사된 글자: ${text.replaceAll('\t', ' | ').replaceAll('\n', ' / ').replaceAll(String.fromCharCode(13), '')}`,
en: `Copied text: ${text.replaceAll('\t', ' | ').replaceAll('\n', ' / ').replaceAll(String.fromCharCode(13), '')}`,
});
},
undo: (api) => say({
ko: api.undo() ? '되돌렸어요.' : '되돌릴 것이 없어요.',
en: api.undo() ? 'Undone.' : 'Nothing to undo.',
}),
redo: (api) => say({
ko: api.redo() ? '다시 넣었어요.' : '다시 실행할 것이 없어요.',
en: api.redo() ? 'Redone.' : 'Nothing to redo.',
}),
discard: (api) => api.rollbackChanges(),
};
options.ariaLabel = "엑셀에서 복사한 칸 붙여넣기";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
document.getElementById('paste').addEventListener('click', () => actions.paste(api));
document.getElementById('pasteBad').addEventListener('click', () => actions.pasteBad(api));
document.getElementById('copy').addEventListener('click', () => actions.copy(api));
document.getElementById('undo').addEventListener('click', () => actions.undo(api));
document.getElementById('redo').addEventListener('click', () => actions.redo(api));
document.getElementById('discard').addEventListener('click', () => actions.discard(api));
</script>
</body>
</html>버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<button
type="button"
disabled={!api}
onClick={() => api && actions.paste(api)}
>붙여넣기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.pasteBad(api)}
>잘못된 값 붙여넣기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.copy(api)}
>복사 글자 보기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.undo(api)}
>되돌리기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.redo(api)}
>다시 실행</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.discard(api)}
>변경 버리기</button>
</div>
<Grid style={{ height: 290 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
item: string;
qty: number;
price: number;
}
const rows: Row[] = [
{ id: 1, item: 'A4 복사지', qty: 10, price: 4500 },
{ id: 2, item: '토너', qty: 2, price: 79000 },
{ id: 3, item: '서류 파일', qty: 30, price: 1200 },
{ id: 4, item: '보드 마카', qty: 12, price: 2500 },
{ id: 5, item: '포스트잇', qty: 20, price: 1800 },
];
// 엑셀·구글 시트에서 복사한 칸은 "탭으로 칸을, 줄바꿈으로 줄을" 나눈 글자로 와요.
// pasteCells(글자)는 고른 칸(없으면 포커스 칸)부터 그 글자를 채워 넣어요. 칸 편집과 똑같은 검증을 거치고,
// 숫자 칸에 글자가 오는 것처럼 안 맞는 칸은 건너뛰어서 몇 칸이 들어가고 몇 칸이 빠졌는지 알려 줘요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'item', title: '품목', width: 150, editable: true },
{
id: 'qty',
title: '수량',
kind: 'number',
width: 90,
align: 'end',
editable: true,
validation: { min: 0 },
},
{
id: 'price',
title: '단가',
kind: 'number',
width: 110,
align: 'end',
editable: true,
formatValue: (v) => `${Number(v).toLocaleString('ko-KR')}원`,
},
],
rows,
rowKey: 'id',
cellSelection: 'multiple',
saveMode: 'batch',
};
let say: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
log({
ko: '칸을 하나 눌러 둔 뒤 버튼을 누르거나, 엑셀에서 복사한 칸을 Ctrl+V로 붙여 보세요.',
en: 'Select one cell, then press a button, or copy cells from Excel and paste them with Ctrl+V.',
});
return api.on('cellsPasted', ({ pasted, skipped }) =>
log({
ko: `붙여넣기: ${pasted}칸 바꿨고 ${skipped}칸은 건너뛰었어요.`,
en: `Paste: ${pasted} cells changed, ${skipped} skipped.`,
}),
);
}
export const actions = {
// 1번 줄 "품목"부터 3줄×3칸을 채워요.
paste: (api: GridApi<Row>) => {
if (!api.getSelectedCells().length)
return say({
ko: '먼저 붙여 넣기 시작할 칸을 하나 눌러 주세요.',
en: 'Select the cell to start pasting from first.',
});
api.pasteCells('A4 복사지\t25\t5000\n토너\t3\t75000\n서류 파일\t40\t1000');
},
// 두 번째 줄의 수량에 숫자가 아닌 글자가 있어요. 그 칸만 건너뛰어요.
pasteBad: (api: GridApi<Row>) => {
if (!api.getSelectedCells().length)
return say({
ko: '먼저 붙여 넣기 시작할 칸을 하나 눌러 주세요.',
en: 'Select the cell to start pasting from first.',
});
api.pasteCells('A4 복사지\t25\t5000\n토너\t많이\t75000\n서류 파일\t-5\t1000');
},
// 고른 칸을 엑셀에 붙는 글자로 꺼내 봐요. 맨 위에 컬럼 제목을 붙일 수도 있어요.
copy: (api: GridApi<Row>) => {
if (!api.getSelectedCells().length)
return say({ ko: '먼저 복사할 칸을 눌러 주세요.', en: 'Select the cells to copy first.' });
const text = api.copyCells({ header: 'columns' });
say({
ko: `복사된 글자: ${text.replaceAll('\t', ' | ').replaceAll('\n', ' / ').replaceAll(String.fromCharCode(13), '')}`,
en: `Copied text: ${text.replaceAll('\t', ' | ').replaceAll('\n', ' / ').replaceAll(String.fromCharCode(13), '')}`,
});
},
undo: (api: GridApi<Row>) =>
say({
ko: api.undo() ? '되돌렸어요.' : '되돌릴 것이 없어요.',
en: api.undo() ? 'Undone.' : 'Nothing to undo.',
}),
redo: (api: GridApi<Row>) =>
say({
ko: api.redo() ? '다시 넣었어요.' : '다시 실행할 것이 없어요.',
en: api.redo() ? 'Redone.' : 'Nothing to redo.',
}),
discard: (api: GridApi<Row>) => api.rollbackChanges(),
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<button type="button" :disabled="!api" @click="api && actions.paste(api)">붙여넣기</button>
<button
type="button"
:disabled="!api"
@click="api && actions.pasteBad(api)"
>잘못된 값 붙여넣기</button>
<button type="button" :disabled="!api" @click="api && actions.copy(api)">복사 글자 보기</button>
<button type="button" :disabled="!api" @click="api && actions.undo(api)">되돌리기</button>
<button type="button" :disabled="!api" @click="api && actions.redo(api)">다시 실행</button>
<button type="button" :disabled="!api" @click="api && actions.discard(api)">변경 버리기</button>
</div>
<Grid style="height: 290px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
item: string;
qty: number;
price: number;
}
const rows: Row[] = [
{ id: 1, item: 'A4 복사지', qty: 10, price: 4500 },
{ id: 2, item: '토너', qty: 2, price: 79000 },
{ id: 3, item: '서류 파일', qty: 30, price: 1200 },
{ id: 4, item: '보드 마카', qty: 12, price: 2500 },
{ id: 5, item: '포스트잇', qty: 20, price: 1800 },
];
// 엑셀·구글 시트에서 복사한 칸은 "탭으로 칸을, 줄바꿈으로 줄을" 나눈 글자로 와요.
// pasteCells(글자)는 고른 칸(없으면 포커스 칸)부터 그 글자를 채워 넣어요. 칸 편집과 똑같은 검증을 거치고,
// 숫자 칸에 글자가 오는 것처럼 안 맞는 칸은 건너뛰어서 몇 칸이 들어가고 몇 칸이 빠졌는지 알려 줘요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'item', title: '품목', width: 150, editable: true },
{
id: 'qty',
title: '수량',
kind: 'number',
width: 90,
align: 'end',
editable: true,
validation: { min: 0 },
},
{
id: 'price',
title: '단가',
kind: 'number',
width: 110,
align: 'end',
editable: true,
formatValue: (v) => `${Number(v).toLocaleString('ko-KR')}원`,
},
],
rows,
rowKey: 'id',
cellSelection: 'multiple',
saveMode: 'batch',
};
let say: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
log({
ko: '칸을 하나 눌러 둔 뒤 버튼을 누르거나, 엑셀에서 복사한 칸을 Ctrl+V로 붙여 보세요.',
en: 'Select one cell, then press a button, or copy cells from Excel and paste them with Ctrl+V.',
});
return api.on('cellsPasted', ({ pasted, skipped }) =>
log({
ko: `붙여넣기: ${pasted}칸 바꿨고 ${skipped}칸은 건너뛰었어요.`,
en: `Paste: ${pasted} cells changed, ${skipped} skipped.`,
}),
);
}
export const actions = {
// 1번 줄 "품목"부터 3줄×3칸을 채워요.
paste: (api: GridApi<Row>) => {
if (!api.getSelectedCells().length)
return say({
ko: '먼저 붙여 넣기 시작할 칸을 하나 눌러 주세요.',
en: 'Select the cell to start pasting from first.',
});
api.pasteCells('A4 복사지\t25\t5000\n토너\t3\t75000\n서류 파일\t40\t1000');
},
// 두 번째 줄의 수량에 숫자가 아닌 글자가 있어요. 그 칸만 건너뛰어요.
pasteBad: (api: GridApi<Row>) => {
if (!api.getSelectedCells().length)
return say({
ko: '먼저 붙여 넣기 시작할 칸을 하나 눌러 주세요.',
en: 'Select the cell to start pasting from first.',
});
api.pasteCells('A4 복사지\t25\t5000\n토너\t많이\t75000\n서류 파일\t-5\t1000');
},
// 고른 칸을 엑셀에 붙는 글자로 꺼내 봐요. 맨 위에 컬럼 제목을 붙일 수도 있어요.
copy: (api: GridApi<Row>) => {
if (!api.getSelectedCells().length)
return say({ ko: '먼저 복사할 칸을 눌러 주세요.', en: 'Select the cells to copy first.' });
const text = api.copyCells({ header: 'columns' });
say({
ko: `복사된 글자: ${text.replaceAll('\t', ' | ').replaceAll('\n', ' / ').replaceAll(String.fromCharCode(13), '')}`,
en: `Copied text: ${text.replaceAll('\t', ' | ').replaceAll('\n', ' / ').replaceAll(String.fromCharCode(13), '')}`,
});
},
undo: (api: GridApi<Row>) =>
say({
ko: api.undo() ? '되돌렸어요.' : '되돌릴 것이 없어요.',
en: api.undo() ? 'Undone.' : 'Nothing to undo.',
}),
redo: (api: GridApi<Row>) =>
say({
ko: api.redo() ? '다시 넣었어요.' : '다시 실행할 것이 없어요.',
en: api.redo() ? 'Redone.' : 'Nothing to redo.',
}),
discard: (api: GridApi<Row>) => api.rollbackChanges(),
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<button
type="button"
[disabled]="!api"
(click)="api && actions.paste(api)"
>붙여넣기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.pasteBad(api)"
>잘못된 값 붙여넣기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.copy(api)"
>복사 글자 보기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.undo(api)"
>되돌리기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.redo(api)"
>다시 실행</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.discard(api)"
>변경 버리기</button>
</div>
<mx-grid style="height: 290px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
item: string;
qty: number;
price: number;
}
const rows: Row[] = [
{ id: 1, item: 'A4 복사지', qty: 10, price: 4500 },
{ id: 2, item: '토너', qty: 2, price: 79000 },
{ id: 3, item: '서류 파일', qty: 30, price: 1200 },
{ id: 4, item: '보드 마카', qty: 12, price: 2500 },
{ id: 5, item: '포스트잇', qty: 20, price: 1800 },
];
// 엑셀·구글 시트에서 복사한 칸은 "탭으로 칸을, 줄바꿈으로 줄을" 나눈 글자로 와요.
// pasteCells(글자)는 고른 칸(없으면 포커스 칸)부터 그 글자를 채워 넣어요. 칸 편집과 똑같은 검증을 거치고,
// 숫자 칸에 글자가 오는 것처럼 안 맞는 칸은 건너뛰어서 몇 칸이 들어가고 몇 칸이 빠졌는지 알려 줘요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'item', title: '품목', width: 150, editable: true },
{
id: 'qty',
title: '수량',
kind: 'number',
width: 90,
align: 'end',
editable: true,
validation: { min: 0 },
},
{
id: 'price',
title: '단가',
kind: 'number',
width: 110,
align: 'end',
editable: true,
formatValue: (v) => `${Number(v).toLocaleString('ko-KR')}원`,
},
],
rows,
rowKey: 'id',
cellSelection: 'multiple',
saveMode: 'batch',
};
let say: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
log({
ko: '칸을 하나 눌러 둔 뒤 버튼을 누르거나, 엑셀에서 복사한 칸을 Ctrl+V로 붙여 보세요.',
en: 'Select one cell, then press a button, or copy cells from Excel and paste them with Ctrl+V.',
});
return api.on('cellsPasted', ({ pasted, skipped }) =>
log({
ko: `붙여넣기: ${pasted}칸 바꿨고 ${skipped}칸은 건너뛰었어요.`,
en: `Paste: ${pasted} cells changed, ${skipped} skipped.`,
}),
);
}
export const actions = {
// 1번 줄 "품목"부터 3줄×3칸을 채워요.
paste: (api: GridApi<Row>) => {
if (!api.getSelectedCells().length)
return say({
ko: '먼저 붙여 넣기 시작할 칸을 하나 눌러 주세요.',
en: 'Select the cell to start pasting from first.',
});
api.pasteCells('A4 복사지\t25\t5000\n토너\t3\t75000\n서류 파일\t40\t1000');
},
// 두 번째 줄의 수량에 숫자가 아닌 글자가 있어요. 그 칸만 건너뛰어요.
pasteBad: (api: GridApi<Row>) => {
if (!api.getSelectedCells().length)
return say({
ko: '먼저 붙여 넣기 시작할 칸을 하나 눌러 주세요.',
en: 'Select the cell to start pasting from first.',
});
api.pasteCells('A4 복사지\t25\t5000\n토너\t많이\t75000\n서류 파일\t-5\t1000');
},
// 고른 칸을 엑셀에 붙는 글자로 꺼내 봐요. 맨 위에 컬럼 제목을 붙일 수도 있어요.
copy: (api: GridApi<Row>) => {
if (!api.getSelectedCells().length)
return say({ ko: '먼저 복사할 칸을 눌러 주세요.', en: 'Select the cells to copy first.' });
const text = api.copyCells({ header: 'columns' });
say({
ko: `복사된 글자: ${text.replaceAll('\t', ' | ').replaceAll('\n', ' / ').replaceAll(String.fromCharCode(13), '')}`,
en: `Copied text: ${text.replaceAll('\t', ' | ').replaceAll('\n', ' / ').replaceAll(String.fromCharCode(13), '')}`,
});
},
undo: (api: GridApi<Row>) =>
say({
ko: api.undo() ? '되돌렸어요.' : '되돌릴 것이 없어요.',
en: api.undo() ? 'Undone.' : 'Nothing to undo.',
}),
redo: (api: GridApi<Row>) =>
say({
ko: api.redo() ? '다시 넣었어요.' : '다시 실행할 것이 없어요.',
en: api.redo() ? 'Redone.' : 'Nothing to redo.',
}),
discard: (api: GridApi<Row>) => api.rollbackChanges(),
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<div id="app">
<div class="toolbar">
<button type="button" id="paste">Paste</button>
<button type="button" id="pasteBad">Paste with bad values</button>
<button type="button" id="copy">Show copied text</button>
<button type="button" id="undo">Undo</button>
<button type="button" id="redo">Redo</button>
<button type="button" id="discard">Discard changes</button>
</div>
<div id="grid" style="height: 290px"></div>
</div>
<script type="module" src="./main.js"></script>import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('paste').addEventListener('click', () => actions.paste(api));
document.getElementById('pasteBad').addEventListener('click', () => actions.pasteBad(api));
document.getElementById('copy').addEventListener('click', () => actions.copy(api));
document.getElementById('undo').addEventListener('click', () => actions.undo(api));
document.getElementById('redo').addEventListener('click', () => actions.redo(api));
document.getElementById('discard').addEventListener('click', () => actions.discard(api));const rows = [
{ id: 1, item: 'A4 paper', qty: 10, price: 4.5 },
{ id: 2, item: 'Toner', qty: 2, price: 79 },
{ id: 3, item: 'Folders', qty: 30, price: 1.2 },
{ id: 4, item: 'Markers', qty: 12, price: 2.5 },
{ id: 5, item: 'Sticky notes', qty: 20, price: 1.8 },
];
// Cells copied from Excel or Google Sheets arrive as text split by tabs into cells and by line breaks into rows.
// pasteCells(text) fills that text starting at the selected cell (or the focused cell if none). It runs the same validation as cell editing,
// and skips cells that do not fit, such as text coming into a number cell, then reports how many cells went in and how many were left out.
export const options = {
columns: [
{ id: 'item', title: 'Item', width: 150, editable: true },
{
id: 'qty',
title: 'Qty',
kind: 'number',
width: 90,
align: 'end',
editable: true,
validation: { min: 0 },
},
{
id: 'price',
title: 'Unit price',
kind: 'number',
width: 110,
align: 'end',
editable: true,
formatValue: (v) => `$${Number(v).toLocaleString('en-US', { minimumFractionDigits: 2 })}`,
},
],
rows,
rowKey: 'id',
cellSelection: 'multiple',
saveMode: 'batch',
};
let say = () => { };
export function setup(api, log) {
say = log;
log('Select one cell, then press a button, or copy cells from Excel and paste them with Ctrl+V.');
return api.on('cellsPasted', ({ pasted, skipped }) => log(`Paste: ${pasted} cells changed, ${skipped} skipped.`));
}
export const actions = {
// Fills 3 rows x 3 cells starting from the "Item" of row 1.
paste: (api) => {
if (!api.getSelectedCells().length)
return say('Select the cell to start pasting from first.');
api.pasteCells('A4 paper\t25\t50\nToner\t3\t750\nFolders\t40\t10');
},
// The quantity on the second line is text, not a number. Only that cell is skipped.
pasteBad: (api) => {
if (!api.getSelectedCells().length)
return say('Select the cell to start pasting from first.');
api.pasteCells('A4 paper\t25\t50\nToner\tmany\t750\nFolders\t-5\t10');
},
// Take the selected cells out as text that pastes into Excel. Column titles can be added at the top.
copy: (api) => {
if (!api.getSelectedCells().length)
return say('Select the cells to copy first.');
const text = api.copyCells({ header: 'columns' });
say(`Copied text: ${text.replaceAll('\t', ' | ').replaceAll('\n', ' / ').replaceAll(String.fromCharCode(13), '')}`);
},
undo: (api) => say(api.undo() ? 'Undone.' : 'Nothing to undo.'),
redo: (api) => say(api.redo() ? 'Redone.' : 'Nothing to redo.'),
discard: (api) => api.rollbackChanges(),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Paste cells copied from a spreadsheet</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="paste">Paste</button>
<button type="button" id="pasteBad">Paste with bad values</button>
<button type="button" id="copy">Show copied text</button>
<button type="button" id="undo">Undo</button>
<button type="button" id="redo">Redo</button>
<button type="button" id="discard">Discard changes</button>
</div>
<div id="grid" style="height: 290px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const rows = [
{ id: 1, item: 'A4 paper', qty: 10, price: 4.5 },
{ id: 2, item: 'Toner', qty: 2, price: 79 },
{ id: 3, item: 'Folders', qty: 30, price: 1.2 },
{ id: 4, item: 'Markers', qty: 12, price: 2.5 },
{ id: 5, item: 'Sticky notes', qty: 20, price: 1.8 },
];
// Cells copied from Excel or Google Sheets arrive as text split by tabs into cells and by line breaks into rows.
// pasteCells(text) fills that text starting at the selected cell (or the focused cell if none). It runs the same validation as cell editing,
// and skips cells that do not fit, such as text coming into a number cell, then reports how many cells went in and how many were left out.
const options = {
columns: [
{ id: 'item', title: 'Item', width: 150, editable: true },
{
id: 'qty',
title: 'Qty',
kind: 'number',
width: 90,
align: 'end',
editable: true,
validation: { min: 0 },
},
{
id: 'price',
title: 'Unit price',
kind: 'number',
width: 110,
align: 'end',
editable: true,
formatValue: (v) => `$${Number(v).toLocaleString('en-US', { minimumFractionDigits: 2 })}`,
},
],
rows,
rowKey: 'id',
cellSelection: 'multiple',
saveMode: 'batch',
};
let say = () => { };
function setup(api, log) {
say = log;
log('Select one cell, then press a button, or copy cells from Excel and paste them with Ctrl+V.');
return api.on('cellsPasted', ({ pasted, skipped }) => log(`Paste: ${pasted} cells changed, ${skipped} skipped.`));
}
const actions = {
// Fills 3 rows x 3 cells starting from the "Item" of row 1.
paste: (api) => {
if (!api.getSelectedCells().length)
return say('Select the cell to start pasting from first.');
api.pasteCells('A4 paper\t25\t50\nToner\t3\t750\nFolders\t40\t10');
},
// The quantity on the second line is text, not a number. Only that cell is skipped.
pasteBad: (api) => {
if (!api.getSelectedCells().length)
return say('Select the cell to start pasting from first.');
api.pasteCells('A4 paper\t25\t50\nToner\tmany\t750\nFolders\t-5\t10');
},
// Take the selected cells out as text that pastes into Excel. Column titles can be added at the top.
copy: (api) => {
if (!api.getSelectedCells().length)
return say('Select the cells to copy first.');
const text = api.copyCells({ header: 'columns' });
say(`Copied text: ${text.replaceAll('\t', ' | ').replaceAll('\n', ' / ').replaceAll(String.fromCharCode(13), '')}`);
},
undo: (api) => say(api.undo() ? 'Undone.' : 'Nothing to undo.'),
redo: (api) => say(api.redo() ? 'Redone.' : 'Nothing to redo.'),
discard: (api) => api.rollbackChanges(),
};
options.ariaLabel = "Paste cells copied from a spreadsheet";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
document.getElementById('paste').addEventListener('click', () => actions.paste(api));
document.getElementById('pasteBad').addEventListener('click', () => actions.pasteBad(api));
document.getElementById('copy').addEventListener('click', () => actions.copy(api));
document.getElementById('undo').addEventListener('click', () => actions.undo(api));
document.getElementById('redo').addEventListener('click', () => actions.redo(api));
document.getElementById('discard').addEventListener('click', () => actions.discard(api));
</script>
</body>
</html>When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<button
type="button"
disabled={!api}
onClick={() => api && actions.paste(api)}
>Paste</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.pasteBad(api)}
>Paste with bad values</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.copy(api)}
>Show copied text</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.undo(api)}
>Undo</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.redo(api)}
>Redo</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.discard(api)}
>Discard changes</button>
</div>
<Grid style={{ height: 290 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
item: string;
qty: number;
price: number;
}
const rows: Row[] = [
{ id: 1, item: 'A4 paper', qty: 10, price: 4.5 },
{ id: 2, item: 'Toner', qty: 2, price: 79 },
{ id: 3, item: 'Folders', qty: 30, price: 1.2 },
{ id: 4, item: 'Markers', qty: 12, price: 2.5 },
{ id: 5, item: 'Sticky notes', qty: 20, price: 1.8 },
];
// Cells copied from Excel or Google Sheets arrive as text split by tabs into cells and by line breaks into rows.
// pasteCells(text) fills that text starting at the selected cell (or the focused cell if none). It runs the same validation as cell editing,
// and skips cells that do not fit, such as text coming into a number cell, then reports how many cells went in and how many were left out.
export const options: GridOptions<Row> = {
columns: [
{ id: 'item', title: 'Item', width: 150, editable: true },
{
id: 'qty',
title: 'Qty',
kind: 'number',
width: 90,
align: 'end',
editable: true,
validation: { min: 0 },
},
{
id: 'price',
title: 'Unit price',
kind: 'number',
width: 110,
align: 'end',
editable: true,
formatValue: (v) => `$${Number(v).toLocaleString('en-US', { minimumFractionDigits: 2 })}`,
},
],
rows,
rowKey: 'id',
cellSelection: 'multiple',
saveMode: 'batch',
};
let say: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
log('Select one cell, then press a button, or copy cells from Excel and paste them with Ctrl+V.');
return api.on('cellsPasted', ({ pasted, skipped }) =>
log(`Paste: ${pasted} cells changed, ${skipped} skipped.`),
);
}
export const actions = {
// Fills 3 rows x 3 cells starting from the "Item" of row 1.
paste: (api: GridApi<Row>) => {
if (!api.getSelectedCells().length) return say('Select the cell to start pasting from first.');
api.pasteCells('A4 paper\t25\t50\nToner\t3\t750\nFolders\t40\t10');
},
// The quantity on the second line is text, not a number. Only that cell is skipped.
pasteBad: (api: GridApi<Row>) => {
if (!api.getSelectedCells().length) return say('Select the cell to start pasting from first.');
api.pasteCells('A4 paper\t25\t50\nToner\tmany\t750\nFolders\t-5\t10');
},
// Take the selected cells out as text that pastes into Excel. Column titles can be added at the top.
copy: (api: GridApi<Row>) => {
if (!api.getSelectedCells().length) return say('Select the cells to copy first.');
const text = api.copyCells({ header: 'columns' });
say(
`Copied text: ${text.replaceAll('\t', ' | ').replaceAll('\n', ' / ').replaceAll(String.fromCharCode(13), '')}`,
);
},
undo: (api: GridApi<Row>) => say(api.undo() ? 'Undone.' : 'Nothing to undo.'),
redo: (api: GridApi<Row>) => say(api.redo() ? 'Redone.' : 'Nothing to redo.'),
discard: (api: GridApi<Row>) => api.rollbackChanges(),
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<button type="button" :disabled="!api" @click="api && actions.paste(api)">Paste</button>
<button
type="button"
:disabled="!api"
@click="api && actions.pasteBad(api)"
>Paste with bad values</button>
<button type="button" :disabled="!api" @click="api && actions.copy(api)">Show copied text</button>
<button type="button" :disabled="!api" @click="api && actions.undo(api)">Undo</button>
<button type="button" :disabled="!api" @click="api && actions.redo(api)">Redo</button>
<button type="button" :disabled="!api" @click="api && actions.discard(api)">Discard changes</button>
</div>
<Grid style="height: 290px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
item: string;
qty: number;
price: number;
}
const rows: Row[] = [
{ id: 1, item: 'A4 paper', qty: 10, price: 4.5 },
{ id: 2, item: 'Toner', qty: 2, price: 79 },
{ id: 3, item: 'Folders', qty: 30, price: 1.2 },
{ id: 4, item: 'Markers', qty: 12, price: 2.5 },
{ id: 5, item: 'Sticky notes', qty: 20, price: 1.8 },
];
// Cells copied from Excel or Google Sheets arrive as text split by tabs into cells and by line breaks into rows.
// pasteCells(text) fills that text starting at the selected cell (or the focused cell if none). It runs the same validation as cell editing,
// and skips cells that do not fit, such as text coming into a number cell, then reports how many cells went in and how many were left out.
export const options: GridOptions<Row> = {
columns: [
{ id: 'item', title: 'Item', width: 150, editable: true },
{
id: 'qty',
title: 'Qty',
kind: 'number',
width: 90,
align: 'end',
editable: true,
validation: { min: 0 },
},
{
id: 'price',
title: 'Unit price',
kind: 'number',
width: 110,
align: 'end',
editable: true,
formatValue: (v) => `$${Number(v).toLocaleString('en-US', { minimumFractionDigits: 2 })}`,
},
],
rows,
rowKey: 'id',
cellSelection: 'multiple',
saveMode: 'batch',
};
let say: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
log('Select one cell, then press a button, or copy cells from Excel and paste them with Ctrl+V.');
return api.on('cellsPasted', ({ pasted, skipped }) =>
log(`Paste: ${pasted} cells changed, ${skipped} skipped.`),
);
}
export const actions = {
// Fills 3 rows x 3 cells starting from the "Item" of row 1.
paste: (api: GridApi<Row>) => {
if (!api.getSelectedCells().length) return say('Select the cell to start pasting from first.');
api.pasteCells('A4 paper\t25\t50\nToner\t3\t750\nFolders\t40\t10');
},
// The quantity on the second line is text, not a number. Only that cell is skipped.
pasteBad: (api: GridApi<Row>) => {
if (!api.getSelectedCells().length) return say('Select the cell to start pasting from first.');
api.pasteCells('A4 paper\t25\t50\nToner\tmany\t750\nFolders\t-5\t10');
},
// Take the selected cells out as text that pastes into Excel. Column titles can be added at the top.
copy: (api: GridApi<Row>) => {
if (!api.getSelectedCells().length) return say('Select the cells to copy first.');
const text = api.copyCells({ header: 'columns' });
say(
`Copied text: ${text.replaceAll('\t', ' | ').replaceAll('\n', ' / ').replaceAll(String.fromCharCode(13), '')}`,
);
},
undo: (api: GridApi<Row>) => say(api.undo() ? 'Undone.' : 'Nothing to undo.'),
redo: (api: GridApi<Row>) => say(api.redo() ? 'Redone.' : 'Nothing to redo.'),
discard: (api: GridApi<Row>) => api.rollbackChanges(),
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<button
type="button"
[disabled]="!api"
(click)="api && actions.paste(api)"
>Paste</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.pasteBad(api)"
>Paste with bad values</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.copy(api)"
>Show copied text</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.undo(api)"
>Undo</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.redo(api)"
>Redo</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.discard(api)"
>Discard changes</button>
</div>
<mx-grid style="height: 290px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
item: string;
qty: number;
price: number;
}
const rows: Row[] = [
{ id: 1, item: 'A4 paper', qty: 10, price: 4.5 },
{ id: 2, item: 'Toner', qty: 2, price: 79 },
{ id: 3, item: 'Folders', qty: 30, price: 1.2 },
{ id: 4, item: 'Markers', qty: 12, price: 2.5 },
{ id: 5, item: 'Sticky notes', qty: 20, price: 1.8 },
];
// Cells copied from Excel or Google Sheets arrive as text split by tabs into cells and by line breaks into rows.
// pasteCells(text) fills that text starting at the selected cell (or the focused cell if none). It runs the same validation as cell editing,
// and skips cells that do not fit, such as text coming into a number cell, then reports how many cells went in and how many were left out.
export const options: GridOptions<Row> = {
columns: [
{ id: 'item', title: 'Item', width: 150, editable: true },
{
id: 'qty',
title: 'Qty',
kind: 'number',
width: 90,
align: 'end',
editable: true,
validation: { min: 0 },
},
{
id: 'price',
title: 'Unit price',
kind: 'number',
width: 110,
align: 'end',
editable: true,
formatValue: (v) => `$${Number(v).toLocaleString('en-US', { minimumFractionDigits: 2 })}`,
},
],
rows,
rowKey: 'id',
cellSelection: 'multiple',
saveMode: 'batch',
};
let say: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
log('Select one cell, then press a button, or copy cells from Excel and paste them with Ctrl+V.');
return api.on('cellsPasted', ({ pasted, skipped }) =>
log(`Paste: ${pasted} cells changed, ${skipped} skipped.`),
);
}
export const actions = {
// Fills 3 rows x 3 cells starting from the "Item" of row 1.
paste: (api: GridApi<Row>) => {
if (!api.getSelectedCells().length) return say('Select the cell to start pasting from first.');
api.pasteCells('A4 paper\t25\t50\nToner\t3\t750\nFolders\t40\t10');
},
// The quantity on the second line is text, not a number. Only that cell is skipped.
pasteBad: (api: GridApi<Row>) => {
if (!api.getSelectedCells().length) return say('Select the cell to start pasting from first.');
api.pasteCells('A4 paper\t25\t50\nToner\tmany\t750\nFolders\t-5\t10');
},
// Take the selected cells out as text that pastes into Excel. Column titles can be added at the top.
copy: (api: GridApi<Row>) => {
if (!api.getSelectedCells().length) return say('Select the cells to copy first.');
const text = api.copyCells({ header: 'columns' });
say(
`Copied text: ${text.replaceAll('\t', ' | ').replaceAll('\n', ' / ').replaceAll(String.fromCharCode(13), '')}`,
);
},
undo: (api: GridApi<Row>) => say(api.undo() ? 'Undone.' : 'Nothing to undo.'),
redo: (api: GridApi<Row>) => say(api.redo() ? 'Redone.' : 'Nothing to redo.'),
discard: (api: GridApi<Row>) => api.rollbackChanges(),
};