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

코드로 셀 값 쓰기Write cell values from code

setCellValue·fillColumn·updateCells로 셀 하나, 열 전체, 여러 줄의 여러 열을 코드에서 한 번에 써요. 한 번 부르면 되돌리기 한 단계이고, 값이 그대로면 아무 일도 하지 않아요.setCellValue, fillColumn and updateCells write one cell, a whole column or several columns on several rows from code. Each call is one undo step and does nothing when the values are already equal.

활용Intermediate
직접 써 보기Live preview코드로 셀 값 쓰기Write cell values from code

코드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="setOne">셀 하나 쓰기</button>
    <button type="button" id="setInvalid">검증하며 쓰기</button>
    <button type="button" id="fillPriority">열 채우기</button>
    <button type="button" id="updateBlock">여러 줄 쓰기</button>
    <button type="button" id="undo">되돌리기</button>
    <button type="button" id="redo">다시 하기</button>
  </div>
  <div id="grid" style="height: 280px"></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('setOne').addEventListener('click', () => actions.setOne(api));
document.getElementById('setInvalid').addEventListener('click', () => actions.setInvalid(api));
document.getElementById('fillPriority').addEventListener('click', () => actions.fillPriority(api));
document.getElementById('updateBlock').addEventListener('click', () => actions.updateBlock(api));
document.getElementById('undo').addEventListener('click', () => actions.undo(api));
document.getElementById('redo').addEventListener('click', () => actions.redo(api));
example.js
const tickets = [
  '로그인 이동 오류',
  '푸터 링크 정리',
  '검색 결과가 느려요',
  '가격표 오타',
  '알림 설정 저장 안 됨',
];
export const options = {
  columns: [
    { id: 'ticket', title: '티켓', width: 190 },
    { id: 'owner', title: '담당자', width: 100 },
    { id: 'priority', title: '우선순위', width: 90 },
    {
      id: 'hours',
      title: '시간',
      kind: 'number',
      width: 80,
      align: 'end',
      validation: { min: 0, max: 40 },
    },
  ],
  rows: tickets.map((ticket, i) => ({
    id: i + 1,
    ticket,
    owner: '',
    priority: i % 2 ? 'high' : 'low',
    hours: i,
  })),
  rowKey: 'id',
  saveMode: 'batch',
};
let say = () => { };
export function setup(api, log) {
  say = log;
  const off = [
    api.on('validationFailed', ({ message }) => log({
      ko: `검증에 걸려 쓰지 않았어요: ${message}`,
      en: `Rejected by validation: ${message}`,
    })),
    api.on('historyChanged', ({ action }) => action === 'undo' && log({ ko: '한 단계 되돌렸어요.', en: 'Undid one step.' })),
  ];
  return () => off.forEach((fn) => fn());
}
// 셋 다 한 번 부르면 되돌리기 한 단계, rowsChanged 한 번이에요. 값이 그대로면 아무 일도 일어나지 않고 0/false를 돌려줘요.
export const actions = {
  setOne: (api) => {
    const done = api.setCellValue(2, 'owner', '김서연');
    say({
      ko: done ? '2번 줄 담당자를 "김서연"으로 썼어요.' : '값이 같아서 쓰지 않았어요.',
      en: done ? 'Wrote owner "김서연" on row 2.' : 'Same value, nothing written.',
    });
  },
  setInvalid: (api) => {
    const done = api.setCellValue(3, 'hours', 99, { validate: true });
    if (done)
      say({ ko: '썼어요.', en: 'Written.' });
  },
  fillPriority: (api) => {
    const n = api.fillColumn('priority', 'high');
    say({
      ko: `우선순위 열을 "high"로 채웠어요: ${n}줄 바뀜.`,
      en: `Filled priority with "high": ${n} rows changed.`,
    });
  },
  updateBlock: (api) => {
    const n = api.updateCells([1, 2, 3], { owner: '박도윤', hours: 8 });
    say({
      ko: `1~3번 줄의 담당자·시간을 한 번에 썼어요: ${n}줄 바뀜.`,
      en: `Wrote owner and hours on rows 1-3 at once: ${n} rows changed.`,
    });
  },
  undo: (api) => {
    if (!api.undo())
      say({ ko: '되돌릴 단계가 없어요.', en: 'Nothing to undo.' });
  },
  redo: (api) => {
    if (!api.redo())
      say({ ko: '다시 할 단계가 없어요.', en: 'Nothing to redo.' });
  },
};

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="setOne">Write one cell</button>
    <button type="button" id="setInvalid">Write with validation</button>
    <button type="button" id="fillPriority">Fill column</button>
    <button type="button" id="updateBlock">Write rows</button>
    <button type="button" id="undo">Undo</button>
    <button type="button" id="redo">Redo</button>
  </div>
  <div id="grid" style="height: 280px"></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('setOne').addEventListener('click', () => actions.setOne(api));
document.getElementById('setInvalid').addEventListener('click', () => actions.setInvalid(api));
document.getElementById('fillPriority').addEventListener('click', () => actions.fillPriority(api));
document.getElementById('updateBlock').addEventListener('click', () => actions.updateBlock(api));
document.getElementById('undo').addEventListener('click', () => actions.undo(api));
document.getElementById('redo').addEventListener('click', () => actions.redo(api));
example.js
const tickets = [
  'Login redirect error',
  'Clean up footer links',
  'Search results are slow',
  'Typo in price table',
  'Notification settings not saved',
];
export const options = {
  columns: [
    { id: 'ticket', title: 'Ticket', width: 190 },
    { id: 'owner', title: 'Owner', width: 100 },
    { id: 'priority', title: 'Priority', width: 90 },
    {
      id: 'hours',
      title: 'Hours',
      kind: 'number',
      width: 80,
      align: 'end',
      validation: { min: 0, max: 40 },
    },
  ],
  rows: tickets.map((ticket, i) => ({
    id: i + 1,
    ticket,
    owner: '',
    priority: i % 2 ? 'high' : 'low',
    hours: i,
  })),
  rowKey: 'id',
  saveMode: 'batch',
};
let say = () => { };
export function setup(api, log) {
  say = log;
  const off = [
    api.on('validationFailed', ({ message }) => log(`Rejected by validation: ${message}`)),
    api.on('historyChanged', ({ action }) => action === 'undo' && log('Undid one step.')),
  ];
  return () => off.forEach((fn) => fn());
}
// Each call is one undo step and one rowsChanged. If the value is unchanged nothing happens and it returns 0/false.
export const actions = {
  setOne: (api) => {
    const done = api.setCellValue(2, 'owner', 'Emma Stone');
    say(done ? 'Wrote owner "Emma Stone" on row 2.' : 'Same value, nothing written.');
  },
  setInvalid: (api) => {
    const done = api.setCellValue(3, 'hours', 99, { validate: true });
    if (done)
      say('Written.');
  },
  fillPriority: (api) => {
    const n = api.fillColumn('priority', 'high');
    say(`Filled priority with "high": ${n} rows changed.`);
  },
  updateBlock: (api) => {
    const n = api.updateCells([1, 2, 3], { owner: 'Noah Kim', hours: 8 });
    say(`Wrote owner and hours on rows 1-3 at once: ${n} rows changed.`);
  },
  undo: (api) => {
    if (!api.undo())
      say('Nothing to undo.');
  },
  redo: (api) => {
    if (!api.redo())
      say('Nothing to redo.');
  },
};