@papb/json-excel
Version:
Create a pretty Excel table from JSON data with a very simple API
89 lines • 3.83 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.jsonToExcel = void 0;
const exceljs_1 = __importDefault(require("exceljs"));
const assert_dimensions_acceptable_1 = require("./assert-dimensions-acceptable");
const assert_valid_cell_content_1 = require("./assert-valid-cell-content");
const assert_valid_sheet_names_1 = require("./assert-valid-sheet-names");
const check_square_matrix_1 = require("./check-square-matrix");
const auto_sizes_1 = require("./auto-sizes");
const defaults_1 = require("./defaults");
const ALIGNMENT_STYLE = {
vertical: 'middle',
horizontal: 'center',
wrapText: true
};
const FONT_STYLE = {
name: 'Calibri',
size: 11
};
function _jsonToExcel(jsonSheets, options) {
if (jsonSheets.length === 0) {
throw new TypeError('Expected non-empty list of json sheets, got empty list');
}
assert_valid_sheet_names_1.assertValidSheetNames(jsonSheets.map(sheet => sheet.sheetName));
const workbook = new exceljs_1.default.Workbook();
let tableCount = 0;
for (const jsonSheet of jsonSheets) {
check_square_matrix_1.assertNonemptyStringSquareMatrix(jsonSheet.data);
const rowCount = jsonSheet.data.length;
const columnCount = jsonSheet.data[0].length;
assert_dimensions_acceptable_1.assertDimensionsAcceptable(rowCount, columnCount);
const data = jsonSheet.data.map(row => row.map(cell => {
if (options.normalizeLinefeeds) {
cell = cell.replace(/\r\n/g, '\n');
}
cell = cell.replace(/\t/g, ' ');
/// cell = cell.replace(/_x[0-9a-fA-F]{4}_/g, '_x005f$&').replace(/\r?\n/g, '_x000a_');
return jsonSheet.autoTrimWhitespace ? cell.trim() : cell;
}));
const sheet = workbook.addWorksheet(jsonSheet.sheetName);
if (jsonSheet.formatAsTable) {
sheet.addTable({
name: `Table${++tableCount}`,
ref: 'A1',
headerRow: true,
totalsRow: false,
style: {
theme: jsonSheet.tableTheme,
showRowStripes: true
},
columns: data[0].map(name => ({ name, filterButton: true })),
rows: data.slice(1)
});
}
for (let ri = 0; ri < rowCount; ri++) {
for (let ci = 0; ci < columnCount; ci++) {
assert_valid_cell_content_1.assertValidCellContent(data[ri][ci], options.linefeedLimitChecking);
const cell = sheet.getCell(ri + 1, ci + 1);
cell.value = data[ri][ci];
cell.alignment = ALIGNMENT_STYLE;
cell.font = FONT_STYLE;
}
}
if (jsonSheet.autoFitCellSizes) {
const sizes = auto_sizes_1.getAutoFitCellSizes(data, jsonSheet.formatAsTable, jsonSheet.autoFitCellSizesOptions);
for (let ri = 0; ri < rowCount; ri++) {
sheet.getRow(ri + 1).height = sizes.rowHeights[ri];
}
for (let ci = 0; ci < columnCount; ci++) {
sheet.getColumn(ci + 1).width = sizes.columnWidths[ci];
}
}
}
return workbook;
}
/**
* Convert a list of JSON sheets into an ExcelJS Workbook.
* @param sheets Array of JsonSheets to convert
* @param options Options
* @returns ExcelJS.Workbook
*/
function jsonToExcel(sheets, options) {
return _jsonToExcel(sheets.map(sheet => defaults_1.expandJsonSheet(sheet)), defaults_1.expandJsonToExcelOptions(options !== null && options !== void 0 ? options : {}));
}
exports.jsonToExcel = jsonToExcel;
//# sourceMappingURL=json-to-excel.js.map