md-curcuma
Version:
A Typescript library for transporting and converting markdown and other data.
105 lines (104 loc) • 3.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.XLSX_IO_Writer = exports.XLSX_IO_Reader = void 0;
const filesystem_1 = require("../core/filesystem");
const observer_1 = require("../core/observer");
const types_1 = require("./types");
const XLSX = require("xlsx");
const utils_1 = require("../core/utils");
class XLSX_IO_Reader {
constructor(props) {
this.observer_subject = new observer_1.Observer_Subject();
this.props = null;
this.props = props;
}
add_observer(observer, id) {
this.observer_subject.add_observer(observer, id);
}
notify_all(props) {
this.observer_subject.notify_all(props);
}
notify(props) {
this.observer_subject.notify(props);
}
read() {
var file_list = [];
if (filesystem_1.Filesystem.isFolder(this.props.path)) {
file_list = filesystem_1.Filesystem.get_files_list(this.props.path);
}
else if (filesystem_1.Filesystem.isFile(this.props.path)) {
file_list.push(this.props.path);
}
else {
console.log(`not supported: '${this.props.path}'`);
}
console.log("Xlsx_IO.read: ", file_list);
file_list.forEach((file) => {
let o_props = new types_1.IO_Observer_Props();
o_props.from = "xlsx-io";
o_props.to = "runner";
o_props.command = "perform-tasks";
let wb = filesystem_1.Filesystem.read_file_xlsx(this.props.path);
let wb_json = new Map();
wb.SheetNames.forEach((sn) => {
let d = XLSX.utils.sheet_to_json(wb.Sheets[sn], {
header: 1,
});
wb_json.set(sn, d);
});
let io_meta = new types_1.IO_Meta();
io_meta.file_list_reader = file_list;
io_meta.file_name_reader = file;
let the_dao = {
data: Object.fromEntries(wb_json),
io_meta: io_meta,
};
o_props.dao = the_dao;
console.log("xlsx-io.do_command: perform tasks for", file);
this.notify_all(o_props);
});
let m_props = new types_1.IO_Observer_Props();
m_props.from = "markdown-io";
m_props.to = "runner";
m_props.command = "tasks-finished";
this.notify_all(m_props);
}
}
exports.XLSX_IO_Reader = XLSX_IO_Reader;
class XLSX_IO_Writer {
constructor(props) {
this.observer_subject = new observer_1.Observer_Subject();
this.props = null;
this.props = props;
}
add_observer(observer, id) {
this.observer_subject.add_observer(observer, id);
}
notify_all(props) {
this.observer_subject.notify_all(props);
}
notify(props) {
this.observer_subject.notify(props);
}
write(dao) {
console.log("### XLSX_IO.write: ", dao);
let filtered_data = utils_1.Utils.remove_property_from_object(dao.data, this.props.exclude_columns);
let aoo = Array.isArray(filtered_data) ? filtered_data : [filtered_data];
if (filesystem_1.Filesystem.is_file_exist(this.props.path)) {
let workbook = filesystem_1.Filesystem.read_file_xlsx(this.props.path);
var worksheet = workbook.Sheets[this.props.worksheet];
XLSX.utils.sheet_add_json(worksheet, aoo, {
skipHeader: true,
origin: -1,
});
filesystem_1.Filesystem.write_file_xlsx(this.props.path, workbook);
}
else {
const worksheet = XLSX.utils.json_to_sheet(aoo);
const workbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workbook, worksheet, this.props.worksheet);
filesystem_1.Filesystem.write_file_xlsx(this.props.path, workbook);
}
}
}
exports.XLSX_IO_Writer = XLSX_IO_Writer;