수식으로 내려받기Download with Excel formulas
금액 칸과 합계 줄을 값 대신 수식으로 내려받아서 엑셀에서 고치면 다시 계산돼요.Download the amount cells and the total row as formulas so Excel recalculates when you edit.
코드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)
이 예제는 추가 패키지가 필요해요. 내 컴퓨터에서 돌리려면 npm i maxgrid @maxgrid/export로 설치하세요.
버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<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>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));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.',
});
} 이 HTML은 추가 번들(위 script 태그)도 함께 불러오고 전역 변수로 써요. 별도 설치는 필요 없어요.
<!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">
<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>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.export.min.js"></script>
<script>
const { withExport } = MaxGridExport;
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으로 바꿔요.
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 = () => { };
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'}`,
});
},
};
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.',
});
}
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('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));
</script>
</body>
</html> 이 예제는 추가 패키지가 필요해요. 내 컴퓨터에서 돌리려면 npm i @maxgrid/react @maxgrid/export로 설치하세요.
버튼을 누르면 이 예제의 코드가 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">
<select
aria-label="금액 수식"
disabled={!api}
onChange={(event) => api && actions.formulas(api, event.target.value)}
>
<option value="yes">켬</option>
<option value="no">끔</option>
</select>
<select defaultValue="yes"
aria-label="합계 수식"
disabled={!api}
onChange={(event) => api && actions.footer(api, event.target.value)}
>
<option value="yes">켬</option>
<option value="no">끔</option>
</select>
<button
type="button"
disabled={!api}
onClick={() => api && actions.download(api)}
>내려받기</button>
</div>
<Grid style={{ height: 330 }} ref={gridRef} options={options} />
</>
);
}import { withExport } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
item: string;
qty: number;
price: number;
amount: number;
}
const items = ['볼펜', '공책', '스테이플러', '파일철', '포스트잇', '가위'];
const rows: Row[] = 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] as string, qty, price, amount: qty * price };
});
// 금액 컬럼의 xlsxFormula가 줄 번호(rowNumber)로 수식을 만들어요. 합계 줄은 footerFormulas가 SUM으로 바꿔요.
export const options: GridOptions<Row> = {
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: (message: LogText) => void = () => {};
export const actions = {
formulas: (_api: GridApi<Row>, v?: string) => void (formulas = v !== 'no'),
footer: (_api: GridApi<Row>, v?: string) => void (footer = v !== 'no'),
download: async (api: GridApi<Row>) => {
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: GridApi<Row>, log: (message: LogText) => void) {
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.',
});
} 이 예제는 추가 패키지가 필요해요. 내 컴퓨터에서 돌리려면 npm i @maxgrid/vue @maxgrid/export로 설치하세요.
버튼을 누르면 이 예제의 코드가 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">
<select
aria-label="금액 수식"
:disabled="!api"
@change="api && actions.formulas(api, ($event.target as HTMLSelectElement).value)"
>
<option value="yes" selected>켬</option>
<option value="no">끔</option>
</select>
<select
aria-label="합계 수식"
:disabled="!api"
@change="api && actions.footer(api, ($event.target as HTMLSelectElement).value)"
>
<option value="yes" selected>켬</option>
<option value="no">끔</option>
</select>
<button
type="button"
:disabled="!api"
@click="api && actions.download(api)"
>내려받기</button>
</div>
<Grid style="height: 330px" :options="options" @ready="onReady" />
</template>import { withExport } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
item: string;
qty: number;
price: number;
amount: number;
}
const items = ['볼펜', '공책', '스테이플러', '파일철', '포스트잇', '가위'];
const rows: Row[] = 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] as string, qty, price, amount: qty * price };
});
// 금액 컬럼의 xlsxFormula가 줄 번호(rowNumber)로 수식을 만들어요. 합계 줄은 footerFormulas가 SUM으로 바꿔요.
export const options: GridOptions<Row> = {
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: (message: LogText) => void = () => {};
export const actions = {
formulas: (_api: GridApi<Row>, v?: string) => void (formulas = v !== 'no'),
footer: (_api: GridApi<Row>, v?: string) => void (footer = v !== 'no'),
download: async (api: GridApi<Row>) => {
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: GridApi<Row>, log: (message: LogText) => void) {
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.',
});
} 이 예제는 추가 패키지가 필요해요. 내 컴퓨터에서 돌리려면 npm i @maxgrid/angular @maxgrid/export로 설치하세요.
버튼을 누르면 이 예제의 코드가 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">
<select
aria-label="금액 수식"
[disabled]="!api"
(change)="api && actions.formulas(api, $any($event.target).value)"
>
<option value="yes" selected>켬</option>
<option value="no">끔</option>
</select>
<select
aria-label="합계 수식"
[disabled]="!api"
(change)="api && actions.footer(api, $any($event.target).value)"
>
<option value="yes" selected>켬</option>
<option value="no">끔</option>
</select>
<button
type="button"
[disabled]="!api"
(click)="api && actions.download(api)"
>내려받기</button>
</div>
<mx-grid style="height: 330px" [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 { withExport } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
item: string;
qty: number;
price: number;
amount: number;
}
const items = ['볼펜', '공책', '스테이플러', '파일철', '포스트잇', '가위'];
const rows: Row[] = 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] as string, qty, price, amount: qty * price };
});
// 금액 컬럼의 xlsxFormula가 줄 번호(rowNumber)로 수식을 만들어요. 합계 줄은 footerFormulas가 SUM으로 바꿔요.
export const options: GridOptions<Row> = {
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: (message: LogText) => void = () => {};
export const actions = {
formulas: (_api: GridApi<Row>, v?: string) => void (formulas = v !== 'no'),
footer: (_api: GridApi<Row>, v?: string) => void (footer = v !== 'no'),
download: async (api: GridApi<Row>) => {
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: GridApi<Row>, log: (message: LogText) => void) {
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.',
});
} 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.
<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>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));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.');
}This HTML also loads an extra bundle (the extra script tag above) and uses it as a global. No install is needed.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Download with Excel formulas</title>
</head>
<body>
<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>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.export.min.js"></script>
<script>
const { withExport } = MaxGridExport;
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.
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 = () => { };
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'}`);
},
};
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.');
}
options.ariaLabel = "Download with Excel formulas";
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('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));
</script>
</body>
</html> This example needs an extra package. To run it locally, install with npm i @maxgrid/react @maxgrid/export.
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">
<select
aria-label="Amount formulas"
disabled={!api}
onChange={(event) => api && actions.formulas(api, event.target.value)}
>
<option value="yes">On</option>
<option value="no">Off</option>
</select>
<select defaultValue="yes"
aria-label="Total formulas"
disabled={!api}
onChange={(event) => api && actions.footer(api, event.target.value)}
>
<option value="yes">On</option>
<option value="no">Off</option>
</select>
<button
type="button"
disabled={!api}
onClick={() => api && actions.download(api)}
>Download</button>
</div>
<Grid style={{ height: 330 }} ref={gridRef} options={options} />
</>
);
}import { withExport } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string;
export interface Row {
id: number;
item: string;
qty: number;
price: number;
amount: number;
}
const items = ['Pen', 'Notebook', 'Stapler', 'Binder', 'Sticky notes', 'Scissors'];
const rows: Row[] = 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] as string,
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: GridOptions<Row> = {
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: (message: LogText) => void = () => {};
export const actions = {
formulas: (_api: GridApi<Row>, v?: string) => void (formulas = v !== 'no'),
footer: (_api: GridApi<Row>, v?: string) => void (footer = v !== 'no'),
download: async (api: GridApi<Row>) => {
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: GridApi<Row>, log: (message: LogText) => void) {
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.',
);
} This example needs an extra package. To run it locally, install with npm i @maxgrid/vue @maxgrid/export.
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">
<select
aria-label="Amount formulas"
:disabled="!api"
@change="api && actions.formulas(api, ($event.target as HTMLSelectElement).value)"
>
<option value="yes" selected>On</option>
<option value="no">Off</option>
</select>
<select
aria-label="Total formulas"
:disabled="!api"
@change="api && actions.footer(api, ($event.target as HTMLSelectElement).value)"
>
<option value="yes" selected>On</option>
<option value="no">Off</option>
</select>
<button
type="button"
:disabled="!api"
@click="api && actions.download(api)"
>Download</button>
</div>
<Grid style="height: 330px" :options="options" @ready="onReady" />
</template>import { withExport } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string;
export interface Row {
id: number;
item: string;
qty: number;
price: number;
amount: number;
}
const items = ['Pen', 'Notebook', 'Stapler', 'Binder', 'Sticky notes', 'Scissors'];
const rows: Row[] = 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] as string,
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: GridOptions<Row> = {
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: (message: LogText) => void = () => {};
export const actions = {
formulas: (_api: GridApi<Row>, v?: string) => void (formulas = v !== 'no'),
footer: (_api: GridApi<Row>, v?: string) => void (footer = v !== 'no'),
download: async (api: GridApi<Row>) => {
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: GridApi<Row>, log: (message: LogText) => void) {
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.',
);
} This example needs an extra package. To run it locally, install with npm i @maxgrid/angular @maxgrid/export.
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">
<select
aria-label="Amount formulas"
[disabled]="!api"
(change)="api && actions.formulas(api, $any($event.target).value)"
>
<option value="yes" selected>On</option>
<option value="no">Off</option>
</select>
<select
aria-label="Total formulas"
[disabled]="!api"
(change)="api && actions.footer(api, $any($event.target).value)"
>
<option value="yes" selected>On</option>
<option value="no">Off</option>
</select>
<button
type="button"
[disabled]="!api"
(click)="api && actions.download(api)"
>Download</button>
</div>
<mx-grid style="height: 330px" [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 { withExport } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string;
export interface Row {
id: number;
item: string;
qty: number;
price: number;
amount: number;
}
const items = ['Pen', 'Notebook', 'Stapler', 'Binder', 'Sticky notes', 'Scissors'];
const rows: Row[] = 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] as string,
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: GridOptions<Row> = {
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: (message: LogText) => void = () => {};
export const actions = {
formulas: (_api: GridApi<Row>, v?: string) => void (formulas = v !== 'no'),
footer: (_api: GridApi<Row>, v?: string) => void (footer = v !== 'no'),
download: async (api: GridApi<Row>) => {
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: GridApi<Row>, log: (message: LogText) => void) {
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.',
);
}