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

시트 보호하기Protect the Excel sheet

잠글 컬럼과 암호를 정해서 실수로 고치는 것을 막는 시트로 내려받아요.Choose the locked columns and a password to download a sheet that guards against accidental edits.

심화Advanced
직접 써 보기Live preview시트 보호하기Protect the Excel sheet

코드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 탭이나 직접 해보기를 쓰세요.

이 예제는 추가 패키지가 필요해요. 내 컴퓨터에서 돌리려면 npm i maxgrid @maxgrid/export로 설치하세요.

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

index.html
<div id="app">
  <div class="toolbar">
    <select aria-label="잠글 컬럼" id="locked">
      <option value="codes" selected>코드·품목</option>
      <option value="none">코드만</option>
      <option value="all">모두</option>
    </select>
    <input
      type="text"
      aria-label="암호"
      placeholder="암호 (비우면 없음)"
      id="password"
    />
    <select aria-label="정렬 허용" id="sort">
      <option value="no" selected>안 함</option>
      <option value="yes">허용</option>
    </select>
    <button type="button" id="download">내려받기</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('locked').addEventListener('change', (event) => actions.locked(api, event.target.value));
document.getElementById('password').addEventListener('input', (event) => actions.password(api, event.target.value));
document.getElementById('sort').addEventListener('change', (event) => actions.sort(api, event.target.value));
document.getElementById('download').addEventListener('click', () => actions.download(api));
example.js
import { withExport } from '@maxgrid/export';
const names = ['A4 용지', '토너', '마우스', '키보드', '모니터 받침대', '멀티탭'];
const rows = names.map((name, i) => ({
  id: i + 1,
  code: `ST-${100 + i}`,
  name,
  qty: 10 + i * 6,
  memo: '',
}));
// 시트 보호(protect)는 실수로 고치는 것을 막을 뿐 보안이 아니에요. 암호는 엑셀의 옛 해시라서 누구나 쉽게 풀 수 있어요.
export const options = {
  columns: [
    { id: 'code', title: '코드', width: 90 },
    { id: 'name', title: '품목', width: 140 },
    { id: 'qty', title: '재고', kind: 'number', width: 80, align: 'end' },
    { id: 'memo', title: '메모', width: 160 },
  ],
  rows,
  rowKey: 'id',
};
const locks = {
  all: undefined,
  codes: ['code', 'name'],
  none: ['code'],
};
let locked = 'codes';
let password = '';
let sort = false;
let say = () => { };
export const actions = {
  locked: (_api, v) => void (locked = v ?? 'codes'),
  password: (_api, v) => void (password = (v ?? '').trim()),
  sort: (_api, v) => void (sort = v === 'yes'),
  download: async (api) => {
    const ex = withExport(api);
    const protect = {
      ...(password ? { password } : {}),
      ...(locks[locked] ? { lockedColumns: locks[locked] } : {}),
      allowSort: sort,
    };
    const text = new TextDecoder('utf-8').decode(ex.exportXlsx({ protect }));
    const tag = /<sheetProtection[^>]*>/.exec(text)?.[0] ?? '(없음)';
    await ex.downloadXlsx('재고-보호.xlsx', { protect });
    say({
      ko: `내려받았어요. 잠금 컬럼 ${protect.lockedColumns?.join(', ') ?? '전체'} · 암호 ${password ? '있음' : '없음'} · 보호 태그 ${tag}`,
      en: `Downloaded. Locked columns ${protect.lockedColumns?.join(', ') ?? 'all'} · password ${password ? 'set' : 'none'} · tag ${tag}`,
    });
  },
};
export function setup(api, log) {
  say = log;
  withExport(api);
  log({
    ko: '코드·품목만 잠그고 재고·메모는 고칠 수 있게 내려받아요. 보안이 아니라 실수 방지예요.',
    en: 'Downloads with only code and item locked; stock and memo stay editable. This prevents slips, it is not security.',
  });
}

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

This example needs an extra package. To run it locally, install with npm i maxgrid @maxgrid/export.

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">
    <select aria-label="Locked columns" id="locked">
      <option value="codes" selected>Code and item</option>
      <option value="none">Code only</option>
      <option value="all">All</option>
    </select>
    <input
      type="text"
      aria-label="Password"
      placeholder="Password (blank = none)"
      id="password"
    />
    <select aria-label="Allow sort" id="sort">
      <option value="no" selected>No</option>
      <option value="yes">Yes</option>
    </select>
    <button type="button" id="download">Download</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('locked').addEventListener('change', (event) => actions.locked(api, event.target.value));
document.getElementById('password').addEventListener('input', (event) => actions.password(api, event.target.value));
document.getElementById('sort').addEventListener('change', (event) => actions.sort(api, event.target.value));
document.getElementById('download').addEventListener('click', () => actions.download(api));
example.js
import { withExport } from '@maxgrid/export';
const names = ['A4 paper', 'Toner', 'Mouse', 'Keyboard', 'Monitor stand', 'Power strip'];
const rows = names.map((name, i) => ({
  id: i + 1,
  code: `ST-${100 + i}`,
  name,
  qty: 10 + i * 6,
  memo: '',
}));
// Sheet protection (protect) only prevents slips; it is not security. The password uses Excel's old hash, so anyone can remove it.
export const options = {
  columns: [
    { id: 'code', title: 'Code', width: 90 },
    { id: 'name', title: 'Item', width: 140 },
    { id: 'qty', title: 'Stock', kind: 'number', width: 80, align: 'end' },
    { id: 'memo', title: 'Memo', width: 160 },
  ],
  rows,
  rowKey: 'id',
};
const locks = {
  all: undefined,
  codes: ['code', 'name'],
  none: ['code'],
};
let locked = 'codes';
let password = '';
let sort = false;
let say = () => { };
export const actions = {
  locked: (_api, v) => void (locked = v ?? 'codes'),
  password: (_api, v) => void (password = (v ?? '').trim()),
  sort: (_api, v) => void (sort = v === 'yes'),
  download: async (api) => {
    const ex = withExport(api);
    const protect = {
      ...(password ? { password } : {}),
      ...(locks[locked] ? { lockedColumns: locks[locked] } : {}),
      allowSort: sort,
    };
    const text = new TextDecoder('utf-8').decode(ex.exportXlsx({ protect }));
    const tag = /<sheetProtection[^>]*>/.exec(text)?.[0] ?? '(none)';
    await ex.downloadXlsx('stock-protected.xlsx', { protect });
    say(`Downloaded. Locked columns ${protect.lockedColumns?.join(', ') ?? 'all'} · password ${password ? 'set' : 'none'} · tag ${tag}`);
  },
};
export function setup(api, log) {
  say = log;
  withExport(api);
  log('Downloads with only code and item locked; stock and memo stay editable. This prevents slips, it is not security.');
}