붙여넣기 전에 미리 보고 오류 줄 알려 주기Preview pasted data and report bad lines
엑셀에서 복사한 내용을 바로 넣지 않고 그리드에 미리 보여 주며, 문제가 있는 줄을 줄 번호와 함께 알려 줘요.Show pasted spreadsheet data in the grid first and report problem lines with their line numbers.
코드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="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>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));// 엑셀에서 복사한 내용을 바로 넣지 않고, 먼저 그리드에 미리 보여 주면서 줄마다 문제를 찾아 알려 줘요.
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([]),
};<!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="clean">깨끗한 예시</button>
<button type="button" id="messy">문제 있는 예시</button>
<button type="button" id="clear">비우기</button>
</div>
<div id="grid" style="height: 300px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
// 엑셀에서 복사한 내용을 바로 넣지 않고, 먼저 그리드에 미리 보여 주면서 줄마다 문제를 찾아 알려 줘요.
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.`,
});
}
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);
}
const actions = {
clean: (api) => preview(api, SAMPLES['clean'] ?? ''),
messy: (api) => preview(api, SAMPLES['messy'] ?? ''),
clear: (api) => api.setRows([]),
};
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('clean').addEventListener('click', () => actions.clean(api));
document.getElementById('messy').addEventListener('click', () => actions.messy(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(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.clean(api)}
>깨끗한 예시</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.messy(api)}
>문제 있는 예시</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.clear(api)}
>비우기</button>
</div>
<Grid style={{ height: 300 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
line: number;
sku: string;
qty: string;
problem: string;
}
// 엑셀에서 복사한 내용을 바로 넣지 않고, 먼저 그리드에 미리 보여 주면서 줄마다 문제를 찾아 알려 줘요.
export const options: GridOptions<Row> = {
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: Record<string, string> = {
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: (message: LogText) => void = () => {};
function check(sku: string, qty: string, en = false): string[] {
const problems: string[] = [];
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: GridApi<Row>, text: string): void {
const rows = text
.split(/\r?\n/)
.filter((l) => l !== '')
.map<Row>((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: GridApi<Row>, log: (message: LogText) => void) {
say = log;
// 진짜 붙여넣기: 클립보드 이벤트에서 글자를 받아요.
const onPaste = (event: ClipboardEvent) => {
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: GridApi<Row>) => preview(api, SAMPLES['clean'] ?? ''),
messy: (api: GridApi<Row>) => preview(api, SAMPLES['messy'] ?? ''),
clear: (api: GridApi<Row>) => api.setRows([]),
};버튼을 누르면 이 예제의 코드가 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.clean(api)">깨끗한 예시</button>
<button type="button" :disabled="!api" @click="api && actions.messy(api)">문제 있는 예시</button>
<button type="button" :disabled="!api" @click="api && actions.clear(api)">비우기</button>
</div>
<Grid style="height: 300px" :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;
line: number;
sku: string;
qty: string;
problem: string;
}
// 엑셀에서 복사한 내용을 바로 넣지 않고, 먼저 그리드에 미리 보여 주면서 줄마다 문제를 찾아 알려 줘요.
export const options: GridOptions<Row> = {
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: Record<string, string> = {
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: (message: LogText) => void = () => {};
function check(sku: string, qty: string, en = false): string[] {
const problems: string[] = [];
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: GridApi<Row>, text: string): void {
const rows = text
.split(/\r?\n/)
.filter((l) => l !== '')
.map<Row>((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: GridApi<Row>, log: (message: LogText) => void) {
say = log;
// 진짜 붙여넣기: 클립보드 이벤트에서 글자를 받아요.
const onPaste = (event: ClipboardEvent) => {
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: GridApi<Row>) => preview(api, SAMPLES['clean'] ?? ''),
messy: (api: GridApi<Row>) => preview(api, SAMPLES['messy'] ?? ''),
clear: (api: GridApi<Row>) => api.setRows([]),
};버튼을 누르면 이 예제의 코드가 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.clean(api)"
>깨끗한 예시</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.messy(api)"
>문제 있는 예시</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.clear(api)"
>비우기</button>
</div>
<mx-grid style="height: 300px" [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;
line: number;
sku: string;
qty: string;
problem: string;
}
// 엑셀에서 복사한 내용을 바로 넣지 않고, 먼저 그리드에 미리 보여 주면서 줄마다 문제를 찾아 알려 줘요.
export const options: GridOptions<Row> = {
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: Record<string, string> = {
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: (message: LogText) => void = () => {};
function check(sku: string, qty: string, en = false): string[] {
const problems: string[] = [];
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: GridApi<Row>, text: string): void {
const rows = text
.split(/\r?\n/)
.filter((l) => l !== '')
.map<Row>((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: GridApi<Row>, log: (message: LogText) => void) {
say = log;
// 진짜 붙여넣기: 클립보드 이벤트에서 글자를 받아요.
const onPaste = (event: ClipboardEvent) => {
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: GridApi<Row>) => preview(api, SAMPLES['clean'] ?? ''),
messy: (api: GridApi<Row>) => preview(api, SAMPLES['messy'] ?? ''),
clear: (api: GridApi<Row>) => api.setRows([]),
};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="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>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));// 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([]),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Preview pasted data and report bad lines</title>
</head>
<body>
<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>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
// Content copied from Excel is not inserted right away. It is previewed in the grid first, and problems are reported line by line.
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.`);
}
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);
}
const actions = {
clean: (api) => preview(api, SAMPLES['clean'] ?? ''),
messy: (api) => preview(api, SAMPLES['messy'] ?? ''),
clear: (api) => api.setRows([]),
};
options.ariaLabel = "Preview pasted data and report bad lines";
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('clean').addEventListener('click', () => actions.clean(api));
document.getElementById('messy').addEventListener('click', () => actions.messy(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(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.clean(api)}
>Clean sample</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.messy(api)}
>Messy sample</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.clear(api)}
>Clear</button>
</div>
<Grid style={{ height: 300 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string;
export interface Row {
id: number;
line: number;
sku: string;
qty: string;
problem: string;
}
// 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: GridOptions<Row> = {
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: Record<string, string> = {
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: (message: LogText) => void = () => {};
function check(sku: string, qty: string): string[] {
const problems: string[] = [];
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: GridApi<Row>, text: string): void {
const rows = text
.split(/\r?\n/)
.filter((l) => l !== '')
.map<Row>((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: GridApi<Row>, log: (message: LogText) => void) {
say = log;
// Real paste: receive the text from the clipboard event.
const onPaste = (event: ClipboardEvent) => {
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: GridApi<Row>) => preview(api, SAMPLES['clean'] ?? ''),
messy: (api: GridApi<Row>) => preview(api, SAMPLES['messy'] ?? ''),
clear: (api: GridApi<Row>) => api.setRows([]),
};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.clean(api)">Clean sample</button>
<button type="button" :disabled="!api" @click="api && actions.messy(api)">Messy sample</button>
<button type="button" :disabled="!api" @click="api && actions.clear(api)">Clear</button>
</div>
<Grid style="height: 300px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string;
export interface Row {
id: number;
line: number;
sku: string;
qty: string;
problem: string;
}
// 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: GridOptions<Row> = {
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: Record<string, string> = {
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: (message: LogText) => void = () => {};
function check(sku: string, qty: string): string[] {
const problems: string[] = [];
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: GridApi<Row>, text: string): void {
const rows = text
.split(/\r?\n/)
.filter((l) => l !== '')
.map<Row>((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: GridApi<Row>, log: (message: LogText) => void) {
say = log;
// Real paste: receive the text from the clipboard event.
const onPaste = (event: ClipboardEvent) => {
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: GridApi<Row>) => preview(api, SAMPLES['clean'] ?? ''),
messy: (api: GridApi<Row>) => preview(api, SAMPLES['messy'] ?? ''),
clear: (api: GridApi<Row>) => api.setRows([]),
};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.clean(api)"
>Clean sample</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.messy(api)"
>Messy sample</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.clear(api)"
>Clear</button>
</div>
<mx-grid style="height: 300px" [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;
export interface Row {
id: number;
line: number;
sku: string;
qty: string;
problem: string;
}
// 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: GridOptions<Row> = {
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: Record<string, string> = {
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: (message: LogText) => void = () => {};
function check(sku: string, qty: string): string[] {
const problems: string[] = [];
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: GridApi<Row>, text: string): void {
const rows = text
.split(/\r?\n/)
.filter((l) => l !== '')
.map<Row>((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: GridApi<Row>, log: (message: LogText) => void) {
say = log;
// Real paste: receive the text from the clipboard event.
const onPaste = (event: ClipboardEvent) => {
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: GridApi<Row>) => preview(api, SAMPLES['clean'] ?? ''),
messy: (api: GridApi<Row>) => preview(api, SAMPLES['messy'] ?? ''),
clear: (api: GridApi<Row>) => api.setRows([]),
};