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

병합 그룹 표시를 엑셀 세로 병합으로 내보내기Export merged group display as vertical merges

grouping.merged로 보이는 그룹 컬럼을 mergedGroups: true로 내려받으면 그룹의 줄들에 걸친 세로 병합 셀로 나가요. 값은 첫 칸에 있고 그룹 머리 줄은 없어요.With mergedGroups: true, the group columns shown by grouping.merged are exported as vertically merged cells over the group's rows. The value is in the first cell and there are no group header lines.

활용Intermediate
직접 써 보기Live preview병합 그룹 표시를 엑셀 세로 병합으로 내보내기Export merged group display as vertical merges

코드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="mode">
      <option value="on" selected>그룹 칸 세로 병합</option>
      <option value="off">병합 없이</option>
    </select>
    <button type="button" id="preview">시트 미리보기</button>
    <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('mode').addEventListener('change', (event) => actions.mode(api, event.target.value));
document.getElementById('preview').addEventListener('click', () => actions.preview(api));
document.getElementById('download').addEventListener('click', () => actions.download(api));
example.js
import { withExport } from '@maxgrid/export';
const en = document.documentElement.dataset.lang === 'en';
const rows = [
  {
    id: 1,
    region: en ? 'Capital area' : '수도권',
    team: en ? 'Sales 1' : '영업 1팀',
    owner: en ? 'Minjun Kim' : '김민준',
    amount: 120,
  },
  {
    id: 2,
    region: en ? 'Capital area' : '수도권',
    team: en ? 'Sales 1' : '영업 1팀',
    owner: en ? 'Seoyeon Lee' : '이서연',
    amount: 90,
  },
  {
    id: 3,
    region: en ? 'Capital area' : '수도권',
    team: en ? 'Sales 2' : '영업 2팀',
    owner: en ? 'Jiho Park' : '박지호',
    amount: 140,
  },
  {
    id: 4,
    region: en ? 'Southeast' : '영남',
    team: en ? 'Sales 3' : '영업 3팀',
    owner: en ? 'Haneul Choi' : '최하늘',
    amount: 75,
  },
  {
    id: 5,
    region: en ? 'Southeast' : '영남',
    team: en ? 'Sales 3' : '영업 3팀',
    owner: en ? 'Daon Jung' : '정다온',
    amount: 60,
  },
];
// grouping.merged(1.4.0)는 그룹 컬럼을 값별로 세로 병합해 보여 줘요(그룹 머리 줄 없음).
// 엑셀에서도 같은 모양으로 받으려면 exportXlsx의 mergedGroups: true를 줘요.
export const options = {
  columns: [
    { id: 'region', title: en ? 'Region' : '지역', width: 100 },
    { id: 'team', title: en ? 'Team' : '팀', width: 110 },
    { id: 'owner', title: en ? 'Owner' : '담당', width: 110 },
    { id: 'amount', title: en ? 'Amount' : '금액', kind: 'number', width: 90, align: 'end' },
  ],
  grouping: { by: ['region', 'team'], merged: true },
  rows,
  rowKey: 'id',
  ariaLabel: en ? 'Merged group export' : '그룹 병합 내보내기',
};
let merged = true;
let say = () => { };
export function setup(api, log) {
  say = log;
  merged = true;
  withExport(api);
  log({
    ko: '"시트 미리보기"를 누르면 병합 범위가 기록에 나와요(지역은 3줄·2줄, 팀은 2줄·1줄·2줄).',
    en: 'Press "Preview sheet" to log the merge ranges (region 3 and 2 rows; team 2, 1 and 2 rows).',
  });
}
export const actions = {
  mode: (_api, v) => void (merged = v !== 'off'),
  preview: (api) => {
    const bytes = withExport(api).exportXlsx({ mergedGroups: merged });
    const text = new TextDecoder('utf-8').decode(bytes);
    const xml = text.slice(text.search(/xl\/worksheets\/sheet1\.xml<\?xml/));
    const refs = [...xml.matchAll(/<mergeCell ref="([^"]+)"/g)].map((m) => m[1]);
    say({
      ko: `병합 범위: ${refs.join(', ') || '없음'} (머리글 1줄 + 데이터 ${api.getRowCount()}줄)`,
      en: `Merge ranges: ${refs.join(', ') || 'none'} (1 header row + ${api.getRowCount()} data rows)`,
    });
  },
  download: async (api) => {
    await withExport(api).downloadXlsx('그룹병합.xlsx', { mergedGroups: merged });
    say({ ko: '내려받았어요.', en: 'Downloaded.' });
  },
};

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="Export shape" id="mode">
      <option value="on" selected>Merge group cells</option>
      <option value="off">Without merging</option>
    </select>
    <button type="button" id="preview">Preview sheet</button>
    <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('mode').addEventListener('change', (event) => actions.mode(api, event.target.value));
document.getElementById('preview').addEventListener('click', () => actions.preview(api));
document.getElementById('download').addEventListener('click', () => actions.download(api));
example.js
import { withExport } from '@maxgrid/export';
const rows = [
  { id: 1, region: 'Capital area', team: 'Sales 1', owner: 'Liam Carter', amount: 120 },
  { id: 2, region: 'Capital area', team: 'Sales 1', owner: 'Emma Stone', amount: 90 },
  { id: 3, region: 'Capital area', team: 'Sales 2', owner: 'Noah Kim', amount: 140 },
  { id: 4, region: 'Southeast', team: 'Sales 3', owner: 'Olivia Reed', amount: 75 },
  { id: 5, region: 'Southeast', team: 'Sales 3', owner: 'Ava Brooks', amount: 60 },
];
// grouping.merged (1.4.0) shows the group columns merged vertically by value (no group header row).
// To download the same shape in Excel, pass mergedGroups: true to exportXlsx.
export const options = {
  columns: [
    { id: 'region', title: 'Region', width: 110 },
    { id: 'team', title: 'Team', width: 110 },
    { id: 'owner', title: 'Owner', width: 110 },
    { id: 'amount', title: 'Amount', kind: 'number', width: 90, align: 'end' },
  ],
  grouping: { by: ['region', 'team'], merged: true },
  rows,
  rowKey: 'id',
  ariaLabel: 'Merged group export',
};
let merged = true;
let say = () => { };
export function setup(api, log) {
  say = log;
  merged = true;
  withExport(api);
  log('Press "Preview sheet" to log the merge ranges (region 3 and 2 rows; team 2, 1 and 2 rows).');
}
export const actions = {
  mode: (_api, v) => void (merged = v !== 'off'),
  preview: (api) => {
    const bytes = withExport(api).exportXlsx({ mergedGroups: merged });
    const text = new TextDecoder('utf-8').decode(bytes);
    const xml = text.slice(text.search(/xl\/worksheets\/sheet1\.xml<\?xml/));
    const refs = [...xml.matchAll(/<mergeCell ref="([^"]+)"/g)].map((m) => m[1]);
    say(`Merge ranges: ${refs.join(', ') || 'none'} (1 header row + ${api.getRowCount()} data rows)`);
  },
  download: async (api) => {
    await withExport(api).downloadXlsx('merged-groups.xlsx', { mergedGroups: merged });
    say('Downloaded.');
  },
};