forked from sheetjs/sheetjs
Fix write not working and refactor
[ci skip]
This commit is contained in:
parent
57b296e5b5
commit
9add78a290
@ -26,12 +26,12 @@ a { text-decoration: none }
|
||||
<a href="https://github.com/SheetJS/js-xlsx">Source Code Repo</a>
|
||||
<a href="https://github.com/SheetJS/js-xlsx/issues">Issues? Something look weird? Click here and report an issue</a>
|
||||
<br />
|
||||
<button id="readf">Click here to select a file from your computer</button><br />
|
||||
<button id="readBtn">Click here to select a file from your computer</button><br />
|
||||
<div id="drop">Drop a spreadsheet file here to see sheet data</div>
|
||||
<input type="file" name="xlfile" id="xlf" /> ... or click here to select a file
|
||||
<input type="file" name="xlfile" id="readIn" /> ... or click here to select a file
|
||||
|
||||
</pre>
|
||||
<p><input type="submit" value="Export Data!" id="xport" onclick="export_xlsx();" disabled="true"></p>
|
||||
<p><input type="submit" value="Export Data!" id="exportBtn" disabled="true"></p>
|
||||
<div id="htmlout"></div>
|
||||
<br />
|
||||
<script src="index.js"></script>
|
||||
|
@ -1,94 +1,79 @@
|
||||
/* xlsx.js (C) 2013-present SheetJS -- https://sheetjs.com */
|
||||
/* global Uint8Array, console */
|
||||
/* exported export_xlsx */
|
||||
/* eslint no-use-before-define:0 */
|
||||
var XLSX = require('xlsx');
|
||||
var electron = require('electron').remote;
|
||||
const XLSX = require('xlsx');
|
||||
const electron = require('electron').remote;
|
||||
|
||||
var process_wb = (function() {
|
||||
var HTMLOUT = document.getElementById('htmlout');
|
||||
var XPORT = document.getElementById('xport');
|
||||
const EXTENSIONS = "xls|xlsx|xlsm|xlsb|xml|csv|txt|dif|sylk|slk|prn|ods|fods|htm|html".split("|");
|
||||
|
||||
return function process_wb(wb) {
|
||||
XPORT.disabled = false;
|
||||
HTMLOUT.innerHTML = "";
|
||||
wb.SheetNames.forEach(function(sheetName) {
|
||||
var htmlstr = XLSX.utils.sheet_to_html(wb.Sheets[sheetName],{editable:true});
|
||||
HTMLOUT.innerHTML += htmlstr;
|
||||
});
|
||||
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);
|
||||
};
|
||||
|
||||
var do_file = (function() {
|
||||
return function do_file(files) {
|
||||
var f = files[0];
|
||||
var reader = new FileReader();
|
||||
reader.onload = function(e) {
|
||||
var data = e.target.result;
|
||||
data = new Uint8Array(data);
|
||||
process_wb(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]));
|
||||
};
|
||||
|
||||
(function() {
|
||||
var drop = document.getElementById('drop');
|
||||
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"] });
|
||||
};
|
||||
|
||||
function handleDrop(e) {
|
||||
e.stopPropagation();
|
||||
e.preventDefault();
|
||||
do_file(e.dataTransfer.files);
|
||||
}
|
||||
// add event listeners
|
||||
const readBtn = document.getElementById('readBtn');
|
||||
const readIn = document.getElementById('readIn');
|
||||
const exportBtn = document.getElementById('exportBtn');
|
||||
const drop = document.getElementById('drop');
|
||||
|
||||
function handleDragover(e) {
|
||||
e.stopPropagation();
|
||||
e.preventDefault();
|
||||
e.dataTransfer.dropEffect = 'copy';
|
||||
}
|
||||
|
||||
drop.addEventListener('dragenter', handleDragover, false);
|
||||
drop.addEventListener('dragover', handleDragover, false);
|
||||
drop.addEventListener('drop', handleDrop, false);
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var readf = document.getElementById('readf');
|
||||
async function handleF(/*e*/) {
|
||||
var o = await electron.dialog.showOpenDialog({
|
||||
title: 'Select a file',
|
||||
filters: [{
|
||||
name: "Spreadsheets",
|
||||
extensions: "xls|xlsx|xlsm|xlsb|xml|xlw|xlc|csv|txt|dif|sylk|slk|prn|ods|fods|uos|dbf|wks|123|wq1|qpw|htm|html".split("|")
|
||||
}],
|
||||
properties: ['openFile']
|
||||
});
|
||||
if(o.filePaths.length > 0) process_wb(XLSX.readFile(o.filePaths[0]));
|
||||
}
|
||||
readf.addEventListener('click', handleF, false);
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var xlf = document.getElementById('xlf');
|
||||
function handleFile(e) { do_file(e.target.files); }
|
||||
xlf.addEventListener('change', handleFile, false);
|
||||
})();
|
||||
|
||||
var export_xlsx = (function() {
|
||||
var HTMLOUT = document.getElementById('htmlout');
|
||||
var XTENSION = "xls|xlsx|xlsm|xlsb|xml|csv|txt|dif|sylk|slk|prn|ods|fods|htm|html".split("|")
|
||||
return async function() {
|
||||
var wb = XLSX.utils.table_to_book(HTMLOUT);
|
||||
var o = await electron.dialog.showSaveDialog({
|
||||
title: 'Save file as',
|
||||
filters: [{
|
||||
name: "Spreadsheets",
|
||||
extensions: XTENSION
|
||||
}]
|
||||
});
|
||||
console.log(o.filePath);
|
||||
XLSX.writeFile(wb, o.filePath);
|
||||
electron.dialog.showMessageBox({ message: "Exported data to " + o.filePath, buttons: ["OK"] });
|
||||
};
|
||||
})();
|
||||
void export_xlsx;
|
||||
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);
|
||||
|
Loading…
Reference in New Issue
Block a user