sheetjs_sheetjs/demos/electron/index.js

80 lines
2.4 KiB
JavaScript
Raw Normal View History

/* xlsx.js (C) 2013-present SheetJS -- https://sheetjs.com */
const XLSX = require('xlsx');
2022-03-07 07:36:36 +00:00
const electron = require('@electron/remote');
const EXTENSIONS = "xls|xlsx|xlsm|xlsb|xml|csv|txt|dif|sylk|slk|prn|ods|fods|htm|html".split("|");
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;
});
};
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'}));
};
reader.readAsArrayBuffer(f);
};
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]));
};
const exportXlsx = async function() {
const HTMLOUT = document.getElementById('htmlout');
2022-06-27 05:47:58 +00:00
const wb = XLSX.utils.table_to_book(HTMLOUT.getElementsByTagName("TABLE")[0]);
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"] });
};
// add event listeners
const readBtn = document.getElementById('readBtn');
const readIn = document.getElementById('readIn');
const exportBtn = document.getElementById('exportBtn');
const drop = document.getElementById('drop');
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);