to-spreadsheet
Version:
npm package to create spreadsheet in node environment and in browser
81 lines (79 loc) • 3.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateSheetXml = void 0;
const __1 = require("../..");
const util_1 = require("../../util");
const generateSheetXml = (sheet, styleMap, hasDateCells = false) => {
const styleToIndex = new Map();
Array.from(styleMap.keys()).forEach((key, index) => {
styleToIndex.set(key, index);
});
return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet
xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
<sheetPr filterMode="false">
<pageSetUpPr fitToPage="false"/>
</sheetPr>
<dimension ref="A1:${(0, util_1.calculateExtant)(sheet.rows)}"/>
<sheetViews>
<sheetView tabSelected="1" zoomScale="79" zoomScaleNormal="79" workbookViewId="0"/>
</sheetViews>
<sheetFormatPr defaultRowHeight="12.8"></sheetFormatPr>
<cols>
<col max="1025" min="1" style="0" width="11.52"/>
</cols>
<sheetData>
${sheet.rows.map((row, rowIndex) => {
let rowContent = '';
rowContent += `<row r="${(0, util_1.indexToVbIndex)(rowIndex)}">\n`;
row.cells.forEach((cell, cellIndex) => {
const cellType = cell.type;
if (cellType !== __1.ICellType.skip) {
const cellPosition = (0, util_1.rowColumnToVbPosition)(cellIndex, rowIndex);
const cellValue = cell.value || '';
let styleIndex = 0;
let isDateCell = cell.type === __1.ICellType.date;
if ('style' in cell && cell.style) {
const styleKey = (0, util_1.getStyleKey)(cell.style);
const baseStyleIndex = styleToIndex.get(styleKey) || 0;
if (isDateCell && hasDateCells) {
styleIndex = baseStyleIndex + styleMap.size;
}
else {
styleIndex = baseStyleIndex;
}
}
else if (isDateCell && hasDateCells) {
styleIndex = styleMap.size;
}
if (cell.type === __1.ICellType.equation) {
rowContent += `
<c r="${cellPosition}" t="n" s="${styleIndex}">
<f aca="false">${cell.value.getEquation()}</f>
</c>\n`;
}
else if (cell.type === __1.ICellType.date) {
const excelDateValue = (0, util_1.dateToExcelSerial)(cell.value);
rowContent += `
<c r="${cellPosition}" t="n" s="${styleIndex}">
<v>${excelDateValue}</v>
</c>\n`;
}
else {
rowContent += `
<c r="${cellPosition}" t="${cellType}" s="${styleIndex}">
<v>${cellValue}</v>
</c>\n`;
}
}
});
rowContent += '</row>\n';
return rowContent;
}).join('')}
</sheetData>
<pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/>
</worksheet>
`;
};
exports.generateSheetXml = generateSheetXml;