datainout
Version:
Import and reports data
129 lines (125 loc) • 4.55 kB
JavaScript
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
}) : x)(function(x) {
if (typeof require !== "undefined") return require.apply(this, arguments);
throw Error('Dynamic require of "' + x + '" is not supported');
});
// 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;
}
};
export {
ExcelTemplateManager
};