es-grid-template
Version:
es-grid-template
139 lines (135 loc) • 4.45 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exportTableAsExcel;
var _SpanManager = _interopRequireDefault(require("../base-table/helpers/SpanManager"));
var _internals = require("./../internals");
var _collectNodes = _interopRequireDefault(require("./collectNodes"));
var _getTreeDepth = _interopRequireDefault(require("./getTreeDepth"));
var _isLeafNode = _interopRequireDefault(require("./isLeafNode"));
function safeGetSpanRect(column, record, rowIndex, colIndex) {
let colSpan = 1;
let rowSpan = 1;
if (column.getSpanRect) {
const value = _internals.internals.safeGetValue(column, record, rowIndex);
const spanRect = column.getSpanRect(value, record, rowIndex);
colSpan = spanRect == null ? 1 : spanRect.right - colIndex;
rowSpan = spanRect == null ? 1 : spanRect.bottom - rowIndex;
} else {
const cellProps = _internals.internals.safeGetCellProps(column, record, rowIndex);
if (cellProps.colSpan != null) {
colSpan = cellProps.colSpan;
}
if (cellProps.rowSpan != null) {
rowSpan = cellProps.rowSpan;
}
}
// Note: this does not consider rowSpan/colSpan being too large; such spans may affect cells not rendered due to virtual scrolling
return {
top: rowIndex,
bottom: rowIndex + rowSpan,
left: colIndex,
right: colIndex + colSpan
};
}
function move({
c,
r
}, dx, dy) {
return {
c: c + dx,
r: r + dy
};
}
function sanitizeCellDatum(value) {
if (value === Infinity || value === -Infinity || typeof value === 'number' && isNaN(value)) {
return null;
} else {
return value;
}
}
/** Export table data to an Excel file based on BaseTable's dataSource and columns */
function exportTableAsExcel(xlsxPackage, dataSource, columns, filename) {
const sheet = xlsxPackage.utils.aoa_to_sheet([]);
const topHeaderHeight = (0, _getTreeDepth.default)(columns) + 1;
const origin = {
c: 0,
r: 0
};
addTopHeaders(origin);
addDataPart(move(origin, 0, topHeaderHeight));
xlsxPackage.writeFile({
SheetNames: ['Sheet1'],
Sheets: {
Sheet1: sheet
}
}, filename);
function addTopHeaders(orig) {
dfs(columns, 0, 0);
function dfs(cols, startDx, startDy) {
const start = move(orig, startDx, startDy);
let offsetX = 0;
for (const col of cols) {
if (col.features?.noExport) {
continue;
}
const current = move(start, offsetX, 0);
addOne(col?.headerText ?? '', current);
if ((0, _isLeafNode.default)(col)) {
offsetX += 1;
mergeCells(current, 1, topHeaderHeight - startDy);
} else {
const childrenWidth = dfs(col.children ?? [], startDx + offsetX, startDy + 1);
mergeCells(current, childrenWidth, 1);
offsetX += childrenWidth;
}
}
return offsetX;
}
}
function addDataPart(orig) {
const leafColumns = (0, _collectNodes.default)(columns, 'leaf-only').filter(col => !col.features?.noExport);
const spanManager = new _SpanManager.default();
const dataPart = dataSource.map((record, rowIndex) => {
spanManager.stripUpwards(rowIndex);
return leafColumns.map((col, colIndex) => {
if (spanManager.testSkip(rowIndex, colIndex)) {
return null;
}
const spanRect = safeGetSpanRect(col, record, rowIndex, colIndex);
const rowSpan = spanRect.bottom - spanRect.top;
const colSpan = spanRect.right - spanRect.left;
if (rowSpan > 1 || colSpan > 1) {
spanManager.add(spanRect.top, spanRect.left, colSpan, rowSpan);
mergeCells(move(orig, spanRect.left, spanRect.top), colSpan, rowSpan);
}
return sanitizeCellDatum(_internals.internals.safeGetValue(col, record, rowIndex));
});
});
add(dataPart, orig);
}
function add(data, orig) {
xlsxPackage.utils.sheet_add_aoa(sheet, data, {
origin: orig
});
}
function addOne(datum, orig) {
xlsxPackage.utils.sheet_add_aoa(sheet, [[datum]], {
origin: orig
});
}
function mergeCells(addr, width, height) {
if (width === 1 && height === 1) {
return;
}
if (sheet['!merges'] == null) {
sheet['!merges'] = [];
}
sheet['!merges'].push({
s: addr,
e: move(addr, width - 1, height - 1)
});
}
}