UNPKG

datainout

Version:

Import and reports data

149 lines (144 loc) 5.26 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/common/core/Template.ts var Template_exports = {}; __export(Template_exports, { ExcelTemplateManager: () => ExcelTemplateManager }); module.exports = __toCommonJS(Template_exports); // src/helpers/get-file-extension.ts function getFileExtension(path) { const fileName = path.replace("\\", "/").split("/").pop(); if (!fileName) throw new Error(`Path [${path}] invalid`); const extension = fileName.split(".").pop(); if (!extension) throw new Error(`Path [${path}] invalid`); if (extension === "js") return "js"; else if (extension === "ts") return "ts"; else throw new Error(`Path [${path}] invalid`); } // src/helpers/sort-by-address.ts function reverseString(str) { return str.split("").reverse().join(""); } function sortByAddress(arr, mode = "ASC") { return arr.sort((a, b) => { if (!a.address) return 1; if (!b.address) return -1; if (a.address === b.address) return 0; const result = reverseString(a.address).localeCompare(reverseString(b.address)); return result * (mode === "DESC" ? -1 : 1); }); } // src/common/core/Template.ts var ExcelTemplateManager = class { constructor(templatePath) { this.sheets = []; this.currentSheetIndex = 0; this.groupCells = {}; this.isNullTemplate = false; this.isNullTemplate = !templatePath; this.sheets = templatePath ? this.getTemplate(templatePath) : []; } get ActualTableStartRow() { return this.actualTableStartRow; } get ActualTableEndRow() { return this.actualTableEndRow; } defineActualTableStartRow(actualTableStartRow) { if (!actualTableStartRow || actualTableStartRow <= 0) return; if (this.ActualTableStartRow) return; this.actualTableStartRow = actualTableStartRow; } defineActualTableEndRow(actualTableEndRow) { if (!actualTableEndRow || actualTableEndRow <= 0) return; if (this.ActualTableEndRow) return; this.actualTableEndRow = actualTableEndRow; } set SheetIndex(sheetIndex) { if (sheetIndex < 0) return; this.currentSheetIndex = sheetIndex; this.groupCells = this.formatSheet(); } get SheetInformation() { return this.SheetTemplate; } get SheetTemplate() { return this.sheets[this.currentSheetIndex]; } get Sheets() { return this.sheets; } get GroupCells() { return this.groupCells; } get(arg) { if (this.isNullTemplate) throw new Error("Template is null. Please check template path"); let index = 0; if (typeof arg === "string") index = this.findIndexByKeyName(arg); else if (typeof arg === "number") index = arg; return this.SheetTemplate.cells[index]; } add(cell) { if (this.isNullTemplate) throw new Error("Template is null. Please check template path"); if (!Array.isArray(cell)) cell = [cell]; this.SheetTemplate.cells.push(...cell); } update(key, cell) { if (this.isNullTemplate) throw new Error("Template is null. Please check template path"); let index = 0; if (typeof key === "string") index = this.findIndexByKeyName(key); else if (typeof key === "number") index = key; this.SheetTemplate.cells[index] = cell; } remove(key) { if (this.isNullTemplate) throw new Error("Template is null. Please check template path"); let index = 0; if (typeof key === "string") index = this.findIndexByKeyName(key); else if (typeof key === "number") index = key; this.SheetTemplate.cells.splice(index, 1); } findIndexByKeyName(key) { const index = this.SheetTemplate.cells.findIndex((e) => e.keyName === key); if (index < 0) throw new Error("Not found template with key: " + key); return index; } getTemplate(templatePath) { const template = getFileExtension(templatePath) === "js" ? require(templatePath) : require(templatePath).default; return template.sheets; } formatSheet() { var _a; const defaultGroup = {}; if (this.isNullTemplate) return defaultGroup; const excel = (_a = this.SheetTemplate) == null ? void 0 : _a.cells.reduce((acc, cell) => { var _a2; if (!acc[cell.section]) acc[cell.section] = [cell]; else (_a2 = acc[cell.section]) == null ? void 0 : _a2.push(cell); return acc; }, {}); const keys = Object.keys(excel); for (let i = 0; i < keys.length; i++) excel[keys[i]] = sortByAddress(excel[keys[i]]); return excel; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ExcelTemplateManager });