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

붙여넣기 전에 미리 보고 오류 줄 알려 주기Preview pasted data and report bad lines

엑셀에서 복사한 내용을 바로 넣지 않고 그리드에 미리 보여 주며, 문제가 있는 줄을 줄 번호와 함께 알려 줘요.Show pasted spreadsheet data in the grid first and report problem lines with their line numbers.

활용Intermediate
직접 써 보기Live preview붙여넣기 전에 미리 보고 오류 줄 알려 주기Preview pasted data and report bad lines

코드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="clean">깨끗한 예시</button>
    <button type="button" id="messy">문제 있는 예시</button>
    <button type="button" id="clear">비우기</button>
  </div>
  <div id="grid" style="height: 300px"></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('clean').addEventListener('click', () => actions.clean(api));
document.getElementById('messy').addEventListener('click', () => actions.messy(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(api));
example.js
// 엑셀에서 복사한 내용을 바로 넣지 않고, 먼저 그리드에 미리 보여 주면서 줄마다 문제를 찾아 알려 줘요.
export const options = {
  columns: [
    { id: 'line', title: '줄', kind: 'number', width: 60, align: 'end' },
    { id: 'sku', title: '품번', width: 120 },
    { id: 'qty', title: '수량', width: 80, align: 'end' },
    { id: 'problem', title: '문제', width: 260 },
  ],
  rows: [],
  rowKey: 'id',
  rowClass: (row) => (row.problem ? 'mxg-row-failed' : undefined),
};
const SAMPLES = {
  clean: 'A-100\t12\nA-101\t8\nB-200\t40',
  messy: 'A-100\t12\nA-1O1\t8x\n\t40\nB-200\t-3\nB-201\t5',
};
let say = () => { };
function check(sku, qty, en = false) {
  const problems = [];
  if (!sku.trim())
    problems.push(en ? 'The item number is empty' : '품번이 비어 있어요');
  if (!/^\d+$/.test(qty.trim()))
    problems.push(en
      ? `Quantity "${qty}" must be an integer of 0 or more`
      : `수량 "${qty}" 값은 0 이상의 정수여야 해요`);
  return problems;
}
function preview(api, text) {
  const rows = text
    .split(/\r?\n/)
    .filter((l) => l !== '')
    .map((line, i) => {
    const [sku = '', qty = ''] = line.split('\t');
    return { id: i + 1, line: i + 1, sku, qty, problem: check(sku, qty).join('; ') };
  });
  api.setRows(rows);
  const bad = rows.filter((r) => r.problem);
  for (const r of [...bad].reverse())
    say({
      ko: `${r.line}번째 줄: ${r.problem}`,
      en: `Line ${r.line}: ${check(r.sku, r.qty, true).join('; ')}`,
    }); // 기록은 위가 최신
  say({
    ko: `${rows.length}줄을 붙였고 ${bad.length}줄에 문제가 있어요.`,
    en: `Pasted ${rows.length} lines and ${bad.length} have problems.`,
  });
}
export function setup(api, log) {
  say = log;
  // 진짜 붙여넣기: 클립보드 이벤트에서 글자를 받아요.
  const onPaste = (event) => {
    const text = event.clipboardData?.getData('text/plain');
    if (text)
      preview(api, text);
  };
  document.addEventListener('paste', onPaste);
  log({
    ko: '엑셀에서 두 칸(품번, 수량)을 복사해서 이 화면에 붙여 넣거나, 아래 예시를 눌러 보세요.',
    en: 'Copy two cells (item number, quantity) from Excel and paste them on this screen, or press an example below.',
  });
  return () => document.removeEventListener('paste', onPaste);
}
export const actions = {
  clean: (api) => preview(api, SAMPLES['clean'] ?? ''),
  messy: (api) => preview(api, SAMPLES['messy'] ?? ''),
  clear: (api) => api.setRows([]),
};

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="clean">Clean sample</button>
    <button type="button" id="messy">Messy sample</button>
    <button type="button" id="clear">Clear</button>
  </div>
  <div id="grid" style="height: 300px"></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('clean').addEventListener('click', () => actions.clean(api));
document.getElementById('messy').addEventListener('click', () => actions.messy(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(api));
example.js
// Content copied from Excel is not inserted right away. It is previewed in the grid first, and problems are reported line by line.
export const options = {
  columns: [
    { id: 'line', title: 'Line', kind: 'number', width: 60, align: 'end' },
    { id: 'sku', title: 'SKU', width: 120 },
    { id: 'qty', title: 'Qty', width: 80, align: 'end' },
    { id: 'problem', title: 'Problem', width: 300 },
  ],
  rows: [],
  rowKey: 'id',
  rowClass: (row) => (row.problem ? 'mxg-row-failed' : undefined),
};
const SAMPLES = {
  clean: 'A-100\t12\nA-101\t8\nB-200\t40',
  messy: 'A-100\t12\nA-1O1\t8x\n\t40\nB-200\t-3\nB-201\t5',
};
let say = () => { };
function check(sku, qty) {
  const problems = [];
  if (!sku.trim())
    problems.push('The item number is empty');
  if (!/^\d+$/.test(qty.trim()))
    problems.push(`Quantity "${qty}" must be an integer of 0 or more`);
  return problems;
}
function preview(api, text) {
  const rows = text
    .split(/\r?\n/)
    .filter((l) => l !== '')
    .map((line, i) => {
    const [sku = '', qty = ''] = line.split('\t');
    return { id: i + 1, line: i + 1, sku, qty, problem: check(sku, qty).join('; ') };
  });
  api.setRows(rows);
  const bad = rows.filter((r) => r.problem);
  for (const r of [...bad].reverse())
    say(`Line ${r.line}: ${r.problem}`); // The log shows the newest on top
  say(`Pasted ${rows.length} lines and ${bad.length} have problems.`);
}
export function setup(api, log) {
  say = log;
  // Real paste: receive the text from the clipboard event.
  const onPaste = (event) => {
    const text = event.clipboardData?.getData('text/plain');
    if (text)
      preview(api, text);
  };
  document.addEventListener('paste', onPaste);
  log('Copy two cells (item number, quantity) from Excel and paste them on this screen, or press an example below.');
  return () => document.removeEventListener('paste', onPaste);
}
export const actions = {
  clean: (api) => preview(api, SAMPLES['clean'] ?? ''),
  messy: (api) => preview(api, SAMPLES['messy'] ?? ''),
  clear: (api) => api.setRows([]),
};