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

수식으로 내려받기Download with Excel formulas

금액 칸과 합계 줄을 값 대신 수식으로 내려받아서 엑셀에서 고치면 다시 계산돼요.Download the amount cells and the total row as formulas so Excel recalculates when you edit.

활용Intermediate
직접 써 보기Live preview수식으로 내려받기Download with Excel formulas

코드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="formulas">
      <option value="yes" selected>켬</option>
      <option value="no">끔</option>
    </select>
    <select aria-label="합계 수식" id="footer">
      <option value="yes" selected>켬</option>
      <option value="no">끔</option>
    </select>
    <button type="button" id="download">내려받기</button>
  </div>
  <div id="grid" style="height: 330px"></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('formulas').addEventListener('change', (event) => actions.formulas(api, event.target.value));
document.getElementById('footer').addEventListener('change', (event) => actions.footer(api, event.target.value));
document.getElementById('download').addEventListener('click', () => actions.download(api));
example.js
import { withExport } from '@maxgrid/export';
const items = ['볼펜', '공책', '스테이플러', '파일철', '포스트잇', '가위'];
const rows = Array.from({ length: 8 }, (_, i) => {
  const qty = 5 + ((i * 7) % 20);
  const price = 800 + ((i * 1300) % 6000);
  return { id: i + 1, item: items[i % items.length], qty, price, amount: qty * price };
});
// 금액 컬럼의 xlsxFormula가 줄 번호(rowNumber)로 수식을 만들어요. 합계 줄은 footerFormulas가 SUM으로 바꿔요.
export const options = {
  columns: [
    { id: 'item', title: '품목', width: 120 },
    { id: 'qty', title: '수량', kind: 'number', width: 80, align: 'end', aggregate: 'sum' },
    { id: 'price', title: '단가(원)', kind: 'number', width: 100, align: 'end' },
    {
      id: 'amount',
      title: '금액(원)',
      kind: 'number',
      width: 110,
      align: 'end',
      aggregate: 'sum',
      xlsxFormula: (_row, n) => `B${n}*C${n}`,
    },
  ],
  rows,
  rowKey: 'id',
  summary: { label: '합계' },
};
let formulas = true;
let footer = true;
let say = () => { };
export const actions = {
  formulas: (_api, v) => void (formulas = v !== 'no'),
  footer: (_api, v) => void (footer = v !== 'no'),
  download: async (api) => {
    const ex = withExport(api);
    const o = { formulas, footerFormulas: footer, freezeHeader: true, applyFormatValue: false };
    const text = new TextDecoder('utf-8').decode(ex.exportXlsx(o));
    const count = (text.match(/<f>/g) ?? []).length;
    await ex.downloadXlsx('문구-수식.xlsx', o);
    say({
      ko: `내려받았어요. 수식 칸 ${count}개 · 수식 ${formulas ? '켬' : '끔'} · 합계 수식 ${footer ? '켬' : '끔'}`,
      en: `Downloaded. ${count} formula cells · formulas ${formulas ? 'on' : 'off'} · footer formulas ${footer ? 'on' : 'off'}`,
    });
  },
};
export function setup(api, log) {
  say = log;
  withExport(api);
  log({
    ko: '금액 칸은 =B*C 수식, 합계 줄은 SUM 수식으로 내려받아요. 엑셀에서 수량을 고치면 금액이 다시 계산돼요.',
    en: 'The amount cells become =B*C formulas and the total row uses SUM. Edit a quantity in Excel and the amount recalculates.',
  });
}

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="Amount formulas" id="formulas">
      <option value="yes" selected>On</option>
      <option value="no">Off</option>
    </select>
    <select aria-label="Total formulas" id="footer">
      <option value="yes" selected>On</option>
      <option value="no">Off</option>
    </select>
    <button type="button" id="download">Download</button>
  </div>
  <div id="grid" style="height: 330px"></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('formulas').addEventListener('change', (event) => actions.formulas(api, event.target.value));
document.getElementById('footer').addEventListener('change', (event) => actions.footer(api, event.target.value));
document.getElementById('download').addEventListener('click', () => actions.download(api));
example.js
import { withExport } from '@maxgrid/export';
const items = ['Pen', 'Notebook', 'Stapler', 'Binder', 'Sticky notes', 'Scissors'];
const rows = Array.from({ length: 8 }, (_, i) => {
  const qty = 5 + ((i * 7) % 20);
  const price = (800 + ((i * 1300) % 6000)) / 1000;
  return {
    id: i + 1,
    item: items[i % items.length],
    qty,
    price,
    amount: Math.round(qty * price * 100) / 100,
  };
});
// The xlsxFormula of the amount column builds the formula from the row number. footerFormulas turns the total row into SUM.
export const options = {
  columns: [
    { id: 'item', title: 'Item', width: 120 },
    { id: 'qty', title: 'Qty', kind: 'number', width: 80, align: 'end', aggregate: 'sum' },
    { id: 'price', title: 'Price (USD)', kind: 'number', width: 100, align: 'end' },
    {
      id: 'amount',
      title: 'Amount (USD)',
      kind: 'number',
      width: 110,
      align: 'end',
      aggregate: 'sum',
      xlsxFormula: (_row, n) => `B${n}*C${n}`,
    },
  ],
  rows,
  rowKey: 'id',
  summary: { label: 'Total' },
};
let formulas = true;
let footer = true;
let say = () => { };
export const actions = {
  formulas: (_api, v) => void (formulas = v !== 'no'),
  footer: (_api, v) => void (footer = v !== 'no'),
  download: async (api) => {
    const ex = withExport(api);
    const o = { formulas, footerFormulas: footer, freezeHeader: true, applyFormatValue: false };
    const text = new TextDecoder('utf-8').decode(ex.exportXlsx(o));
    const count = (text.match(/<f>/g) ?? []).length;
    await ex.downloadXlsx('stationery-formulas.xlsx', o);
    say(`Downloaded. ${count} formula cells · formulas ${formulas ? 'on' : 'off'} · footer formulas ${footer ? 'on' : 'off'}`);
  },
};
export function setup(api, log) {
  say = log;
  withExport(api);
  log('The amount cells become =B*C formulas and the total row uses SUM. Edit a quantity in Excel and the amount recalculates.');
}