본문으로 건너뛰기Skip to content
예제 첫화면Examples home

엑셀에서 복사한 칸 붙여넣기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.

활용Intermediate
직접 써 보기Live preview엑셀에서 복사한 칸 붙여넣기Paste cells copied from a spreadsheet

코드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)

빌드 도구(Vite 등)를 쓰는 프로젝트용 모듈 파일이에요. 설치 없이 바로 돌려 보려면 HTML 탭이나 직접 해보기를 쓰세요.

버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.

index.html
<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>
main.js
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));
example.js
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(),
};

Module files for a project with a bundler such as Vite. To run it with no setup, use the HTML tab or Try it.

When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.

index.html
<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>
main.js
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));
example.js
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(),
};