2020-06-28 07:22:46 +00:00
|
|
|
/* xlsx.js (C) 2013-present SheetJS -- https://sheetjs.com */
|
2020-07-07 07:49:36 +00:00
|
|
|
const XLSX = require('xlsx');
|
2022-03-07 07:36:36 +00:00
|
|
|
const electron = require('@electron/remote');
|
2017-09-12 20:02:06 +00:00
|
|
|
|
2020-07-07 07:49:36 +00:00
|
|
|
const EXTENSIONS = "xls|xlsx|xlsm|xlsb|xml|csv|txt|dif|sylk|slk|prn|ods|fods|htm|html".split("|");
|
2017-09-12 20:02:06 +00:00
|
|
|
|
2020-07-07 07:49:36 +00:00
|
|
|
const processWb = function(wb) {
|
|
|
|
const HTMLOUT = document.getElementById('htmlout');
|
|
|
|
const XPORT = document.getElementById('exportBtn');
|
|
|
|
XPORT.disabled = false;
|
|
|
|
HTMLOUT.innerHTML = "";
|
|
|
|
wb.SheetNames.forEach(function(sheetName) {
|
|
|
|
const htmlstr = XLSX.utils.sheet_to_html(wb.Sheets[sheetName],{editable:true});
|
|
|
|
HTMLOUT.innerHTML += htmlstr;
|
|
|
|
});
|
|
|
|
};
|
2017-09-12 20:02:06 +00:00
|
|
|
|
2020-07-07 07:49:36 +00:00
|
|
|
const readFile = function(files) {
|
|
|
|
const f = files[0];
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.onload = function(e) {
|
|
|
|
let data = e.target.result;
|
|
|
|
data = new Uint8Array(data);
|
|
|
|
processWb(XLSX.read(data, {type: 'array'}));
|
2017-09-12 20:02:06 +00:00
|
|
|
};
|
2020-07-07 07:49:36 +00:00
|
|
|
reader.readAsArrayBuffer(f);
|
|
|
|
};
|
2017-09-12 20:02:06 +00:00
|
|
|
|
2020-07-07 07:49:36 +00:00
|
|
|
const handleReadBtn = async function() {
|
|
|
|
const o = await electron.dialog.showOpenDialog({
|
|
|
|
title: 'Select a file',
|
|
|
|
filters: [{
|
|
|
|
name: "Spreadsheets",
|
|
|
|
extensions: EXTENSIONS
|
|
|
|
}],
|
|
|
|
properties: ['openFile']
|
|
|
|
});
|
|
|
|
if(o.filePaths.length > 0) processWb(XLSX.readFile(o.filePaths[0]));
|
|
|
|
};
|
2017-09-12 20:02:06 +00:00
|
|
|
|
2020-07-07 07:49:36 +00:00
|
|
|
const exportXlsx = async function() {
|
|
|
|
const HTMLOUT = document.getElementById('htmlout');
|
|
|
|
const wb = XLSX.utils.table_to_book(HTMLOUT);
|
|
|
|
const o = await electron.dialog.showSaveDialog({
|
|
|
|
title: 'Save file as',
|
|
|
|
filters: [{
|
|
|
|
name: "Spreadsheets",
|
|
|
|
extensions: EXTENSIONS
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
console.log(o.filePath);
|
|
|
|
XLSX.writeFile(wb, o.filePath);
|
|
|
|
electron.dialog.showMessageBox({ message: "Exported data to " + o.filePath, buttons: ["OK"] });
|
|
|
|
};
|
2017-09-12 20:02:06 +00:00
|
|
|
|
2020-07-07 07:49:36 +00:00
|
|
|
// add event listeners
|
|
|
|
const readBtn = document.getElementById('readBtn');
|
|
|
|
const readIn = document.getElementById('readIn');
|
|
|
|
const exportBtn = document.getElementById('exportBtn');
|
|
|
|
const drop = document.getElementById('drop');
|
2017-09-12 20:02:06 +00:00
|
|
|
|
2020-07-07 07:49:36 +00:00
|
|
|
readBtn.addEventListener('click', handleReadBtn, false);
|
|
|
|
readIn.addEventListener('change', (e) => { readFile(e.target.files); }, false);
|
|
|
|
exportBtn.addEventListener('click', exportXlsx, false);
|
|
|
|
drop.addEventListener('dragenter', (e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
e.dataTransfer.dropEffect = 'copy';
|
|
|
|
}, false);
|
|
|
|
drop.addEventListener('dragover', (e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
e.dataTransfer.dropEffect = 'copy';
|
|
|
|
}, false);
|
|
|
|
drop.addEventListener('drop', (e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
readFile(e.dataTransfer.files);
|
|
|
|
}, false);
|