Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: export to xls for compatibility mode #1983

Merged
merged 9 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 57 additions & 32 deletions packages/toast-ui.grid/cypress/integration/export.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,34 @@ import { cls } from '@/helper/dom';
import { RowKey } from '@t/store/data';
import { SinonStub } from 'cypress/types/sinon';
import { invokeFilter } from '../helper/util';
import { ExportFormat } from '@t/store/export';

before(() => {
cy.visit('/dist');
});

const DATA = [
{ name: 'Sugar', artist: 'Maroon5', price: 1000, typeCode: '2' },
{ name: 'Get Lucky', artist: 'Daft Punk', price: 2000, typeCode: '3' },
{ name: '21', artist: 'Adele', price: 3000, typeCode: '1' },
];
const COLUMNS = [
{
header: 'Name',
name: 'name',
filter: { type: 'text', showApplyBtn: true, showClearBtn: true },
},
{
header: 'Artist',
name: 'artist',
},
{
header: 'Price',
name: 'price',
hidden: true,
},
];

function showContextMenu(rowKey: RowKey, columnName: string) {
cy.getCell(rowKey, columnName).trigger('contextmenu');
}
Expand All @@ -16,7 +39,7 @@ function getMenuItemByText(text: string) {
return cy.contains(`.${cls('context-menu')} .menu-item`, text);
}

function clickExportMenuItemByFormat(format: 'txt' | 'csv' | 'xlsx') {
function clickExportMenuItemByFormat(format: ExportFormat) {
let text;

switch (format) {
Expand All @@ -36,32 +59,10 @@ function clickExportMenuItemByFormat(format: 'txt' | 'csv' | 'xlsx') {
}

describe('Export data', () => {
const data = [
{ name: 'Sugar', artist: 'Maroon5', price: 1000, typeCode: '2' },
{ name: 'Get Lucky', artist: 'Daft Punk', price: 2000, typeCode: '3' },
{ name: '21', artist: 'Adele', price: 3000, typeCode: '1' },
];
const columns = [
{
header: 'Name',
name: 'name',
filter: { type: 'text', showApplyBtn: true, showClearBtn: true },
},
{
header: 'Artist',
name: 'artist',
},
{
header: 'Price',
name: 'price',
hidden: true,
},
];

let callback: SinonStub;

beforeEach(() => {
cy.createGrid({ data, columns });
cy.createGrid({ data: DATA, columns: COLUMNS });
callback = cy.stub().callsFake((ev: GridEvent) => ev.stop());
cy.gridInstance().invoke('on', 'beforeExport', callback);
});
Expand All @@ -73,7 +74,7 @@ describe('Export data', () => {
describe('Default options', () => {
['txt', 'csv', 'xlsx'].forEach((format) => {
it(`should export data according to default export options to '${format}'`, () => {
clickExportMenuItemByFormat(format as 'txt' | 'csv' | 'xlsx');
clickExportMenuItemByFormat(format as ExportFormat);

cy.wrap(callback).should('be.calledWithMatch', {
data: [
Expand Down Expand Up @@ -157,10 +158,34 @@ describe('Export data', () => {
],
});
});

it(`should export data with complex column headers to 'xls'`, () => {
cy.gridInstance().invoke('setHeader', {
complexColumns: [
{
header: 'Basic',
name: 'complexColumn',
childNames: ['name', 'artist'],
},
],
});

cy.gridInstance().invoke('export', 'xls');

cy.wrap(callback).should('be.calledWithMatch', {
data: [
['Basic', 'Basic'],
['Name', 'Artist'],
['Sugar', 'Maroon5'],
['Get Lucky', 'Daft Punk'],
['21', 'Adele'],
],
});
});
});

describe('Without column headers (includeHeader = false)', () => {
['txt', 'csv', 'xlsx'].forEach((format) => {
['txt', 'csv', 'xlsx', 'xls'].forEach((format) => {
it(`should export data without column headers to '${format}'`, () => {
cy.gridInstance().invoke('export', format, { includeHeader: false });

Expand All @@ -176,7 +201,7 @@ describe('Export data', () => {
});

describe('With hidden columns (includeHiddenColumns = true)', () => {
['txt', 'csv', 'xlsx'].forEach((format) => {
['txt', 'csv', 'xlsx', 'xls'].forEach((format) => {
it(`should export data with hidden column to '${format}'`, () => {
cy.gridInstance().invoke('export', format, { includeHiddenColumns: true });

Expand All @@ -193,7 +218,7 @@ describe('Export data', () => {
});

describe('With column names (columnNames = [...selectedColumnNames])', () => {
['txt', 'csv', 'xlsx'].forEach((format) => {
['txt', 'csv', 'xlsx', 'xls'].forEach((format) => {
it(`should export data with selected columns to '${format}'`, () => {
cy.gridInstance().invoke('export', format, { columnNames: ['name', 'price'] });

Expand All @@ -210,7 +235,7 @@ describe('Export data', () => {
});

describe('With only filtered (onlyFiltered = true)', () => {
['txt', 'csv', 'xlsx'].forEach((format) => {
['txt', 'csv', 'xlsx', 'xls'].forEach((format) => {
it(`should export only filtered data to '${format}'`, () => {
invokeFilter('name', [{ code: 'eq', value: '21' }]);
cy.gridInstance().invoke('export', format, { onlyFiltered: true });
Expand All @@ -227,7 +252,7 @@ describe('Export data', () => {

describe('With only selected (onlySelected = true)', () => {
describe('With selected range', () => {
['txt', 'csv', 'xlsx'].forEach((format) => {
['txt', 'csv', 'xlsx', 'xls'].forEach((format) => {
it(`should export data of selected range to '${format}'`, () => {
const range = { start: [0, 0], end: [1, 1] };
cy.gridInstance().invoke('setSelectionRange', range);
Expand Down Expand Up @@ -279,7 +304,7 @@ describe('Export data', () => {
});

describe('Without selected range', () => {
['txt', 'csv', 'xlsx'].forEach((format) => {
['txt', 'csv', 'xlsx', 'xls'].forEach((format) => {
it(`should export all visible data to '${format}' when no selected range`, () => {
cy.gridInstance().invoke('export', format, { onlySelected: true });

Expand Down Expand Up @@ -366,7 +391,7 @@ describe('Export data', () => {
cy.gridInstance().invoke('setColumns', formatColumn);
});

['txt', 'csv', 'xlsx'].forEach((format) => {
['txt', 'csv', 'xlsx', 'xls'].forEach((format) => {
it(`should export formatted data to '${format}' (useFormattedValue = true)`, () => {
cy.gridInstance().invoke('export', format, { useFormattedValue: true });

Expand Down
21 changes: 17 additions & 4 deletions packages/toast-ui.grid/docs/ko/export.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ๋‚ด๋ณด๋‚ด๊ธฐ ๐Ÿ’พ

TOAST UI Grid๋Š” `v4.19.0` ๋ฒ„์ „ ๋ถ€ํ„ฐ `csv`์™€ ์—‘์…€(`xlsx`)๋กœ ๋‚ด๋ณด๋‚ด๊ธฐ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค. `export` API ๋˜๋Š” ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด์˜ `๋‚ด๋ณด๋‚ด๊ธฐ` ํ•˜์œ„ ๋ฉ”๋‰ด๋ฅผ ํ†ตํ•ด ๋‚ด๋ณด๋‚ด๊ธฐ๋ฅผ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
TOAST UI Grid๋Š” `v4.19.0` ๋ฒ„์ „ ๋ถ€ํ„ฐ `csv`์™€ ์—‘์…€(`xlsx`, `xls`(`v4.21.19` ์ดํ›„))๋กœ ๋‚ด๋ณด๋‚ด๊ธฐ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค. `export` API ๋˜๋Š” ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด์˜ `๋‚ด๋ณด๋‚ด๊ธฐ` ํ•˜์œ„ ๋ฉ”๋‰ด๋ฅผ ํ†ตํ•ด ๋‚ด๋ณด๋‚ด๊ธฐ๋ฅผ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

## ์˜ต์…˜

Expand All @@ -23,7 +23,7 @@ TOAST UI Grid๋Š” `v4.19.0` ๋ฒ„์ „ ๋ถ€ํ„ฐ `csv`์™€ ์—‘์…€(`xlsx`)๋กœ ๋‚ด๋ณด๋‚ด๊ธฐ

์ธ์ž๋กœ ์ฃผ์–ด์ง„ ํฌ๋งท๊ณผ ๋‚ด๋ณด๋‚ด๊ธฐ ์˜ต์…˜์— ๋”ฐ๋ผ ํŒŒ์ผ์„ ๋‚ด๋ณด๋‚ธ๋‹ค.(`ExportOpt`์€ ์œ„์—์„œ ์„ค๋ช…ํ•œ ์˜ต์…˜๊ณผ ๊ฐ™์€ ๊ฐ์ฒด์ด๋‹ค)

`export(format: 'txt' | 'csv' | 'xlsx', exportOpt?: ExportOpt)`
`export(format: 'txt' | 'csv' | 'xlsx' | 'xls', exportOpt?: ExportOpt)`

```js
const options = {
Expand Down Expand Up @@ -125,7 +125,7 @@ const grid = Grid({
```js
grid.on('beforeExport', ev => {
console.log(ev);
// ev.exportFormat - Export format (csv or xlsx)
// ev.exportFormat - Export format ('txt' | 'csv' | 'xlsx' | 'xls')
// ev.exportOptions - Used export options
// ev.data - Data to be finally exported (string[][])
});
Expand Down Expand Up @@ -160,7 +160,7 @@ grid.on('beforeExport', ev => {
```js
grid.on('afterExport', ev => {
console.log(ev);
// ev.exportFormat - Export format (csv or xlsx)
// ev.exportFormat - Export format ('txt' | 'csv' | 'xlsx' | 'xls')
// ev.exportOptions - Used export options
// ev.data - Data to be finally exported (string[][])
});
Expand All @@ -184,6 +184,19 @@ grid.on('afterExport', ev => {
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.17.1/xlsx.full.min.js"></script>
```

`xlsx` ํฌ๋งท์„ ์ง€์›ํ•˜์ง€ ์•Š๋Š” ๊ตฌ ๋ฒ„์ „ Excel ๋“ฑ์—์„œ ์—ด ์ˆ˜ ์žˆ๋Š” ํŒŒ์ผ ๋‚ด๋ณด๋‚ด๊ธฐ๋ฅผ ์œ„ํ•ด `xls` ํฌ๋งท์„ ์ง€์›ํ•œ๋‹ค.
๋˜ํ•œ ๊ธฐ๋ณธ ์—‘์…€ ๋‚ด๋ณด๋‚ด๊ธฐ ํฌ๋งท์„ `xlsx`์—์„œ `xls`๋กœ ๋ณ€๊ฒฝํ•˜๊ธฐ ์œ„ํ•ด `excelCompatibilityMode` ์˜ต์…˜์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

```js
const grid = Grid({
// ...
exportOptions: {
excelCompatibilityMode: true,
},
// ...
});
```

### ๋ธŒ๋ผ์šฐ์ € ์ง€์› ๋ฒ”์œ„
| <img src="https://user-images.githubusercontent.com/1215767/34348387-a2e64588-ea4d-11e7-8267-a43365103afe.png" alt="Chrome" width="16px" height="16px" /> Chrome | <img src="https://user-images.githubusercontent.com/1215767/34348590-250b3ca2-ea4f-11e7-9efb-da953359321f.png" alt="IE" width="16px" height="16px" /> Internet Explorer | <img src="https://user-images.githubusercontent.com/1215767/34348380-93e77ae8-ea4d-11e7-8696-9a989ddbbbf5.png" alt="Edge" width="16px" height="16px" /> Edge | <img src="https://user-images.githubusercontent.com/1215767/34348394-a981f892-ea4d-11e7-9156-d128d58386b9.png" alt="Safari" width="16px" height="16px" /> Safari | <img src="https://user-images.githubusercontent.com/1215767/34348383-9e7ed492-ea4d-11e7-910c-03b39d52f496.png" alt="Firefox" width="16px" height="16px" /> Firefox |
| :---: | :---: | :---: | :---: | :---: |
Expand Down
Loading
Loading