|
1 | 1 | /* xlsx.js (C) 2013-present SheetJS -- https://sheetjs.com */
|
2 |
| -/* global Uint8Array, console */ |
3 |
| -/* exported export_xlsx */ |
4 |
| -/* eslint no-use-before-define:0 */ |
5 |
| -var XLSX = require('xlsx'); |
6 |
| -var electron = require('electron').remote; |
| 2 | +const XLSX = require('xlsx'); |
| 3 | +const electron = require('electron').remote; |
7 | 4 |
|
8 |
| -var process_wb = (function() { |
9 |
| - var HTMLOUT = document.getElementById('htmlout'); |
10 |
| - var XPORT = document.getElementById('xport'); |
| 5 | +const EXTENSIONS = "xls|xlsx|xlsm|xlsb|xml|csv|txt|dif|sylk|slk|prn|ods|fods|htm|html".split("|"); |
11 | 6 |
|
12 |
| - return function process_wb(wb) { |
13 |
| - XPORT.disabled = false; |
14 |
| - HTMLOUT.innerHTML = ""; |
15 |
| - wb.SheetNames.forEach(function(sheetName) { |
16 |
| - var htmlstr = XLSX.utils.sheet_to_html(wb.Sheets[sheetName],{editable:true}); |
17 |
| - HTMLOUT.innerHTML += htmlstr; |
18 |
| - }); |
19 |
| - }; |
20 |
| -})(); |
| 7 | +const processWb = function(wb) { |
| 8 | + const HTMLOUT = document.getElementById('htmlout'); |
| 9 | + const XPORT = document.getElementById('exportBtn'); |
| 10 | + XPORT.disabled = false; |
| 11 | + HTMLOUT.innerHTML = ""; |
| 12 | + wb.SheetNames.forEach(function(sheetName) { |
| 13 | + const htmlstr = XLSX.utils.sheet_to_html(wb.Sheets[sheetName],{editable:true}); |
| 14 | + HTMLOUT.innerHTML += htmlstr; |
| 15 | + }); |
| 16 | +}; |
21 | 17 |
|
22 |
| -var do_file = (function() { |
23 |
| - return function do_file(files) { |
24 |
| - var f = files[0]; |
25 |
| - var reader = new FileReader(); |
26 |
| - reader.onload = function(e) { |
27 |
| - var data = e.target.result; |
28 |
| - data = new Uint8Array(data); |
29 |
| - process_wb(XLSX.read(data, {type: 'array'})); |
30 |
| - }; |
31 |
| - reader.readAsArrayBuffer(f); |
| 18 | +const readFile = function(files) { |
| 19 | + const f = files[0]; |
| 20 | + const reader = new FileReader(); |
| 21 | + reader.onload = function(e) { |
| 22 | + let data = e.target.result; |
| 23 | + data = new Uint8Array(data); |
| 24 | + processWb(XLSX.read(data, {type: 'array'})); |
32 | 25 | };
|
33 |
| -})(); |
34 |
| - |
35 |
| -(function() { |
36 |
| - var drop = document.getElementById('drop'); |
37 |
| - |
38 |
| - function handleDrop(e) { |
39 |
| - e.stopPropagation(); |
40 |
| - e.preventDefault(); |
41 |
| - do_file(e.dataTransfer.files); |
42 |
| - } |
| 26 | + reader.readAsArrayBuffer(f); |
| 27 | +}; |
43 | 28 |
|
44 |
| - function handleDragover(e) { |
45 |
| - e.stopPropagation(); |
46 |
| - e.preventDefault(); |
47 |
| - e.dataTransfer.dropEffect = 'copy'; |
48 |
| - } |
| 29 | +const handleReadBtn = async function() { |
| 30 | + const o = await electron.dialog.showOpenDialog({ |
| 31 | + title: 'Select a file', |
| 32 | + filters: [{ |
| 33 | + name: "Spreadsheets", |
| 34 | + extensions: EXTENSIONS |
| 35 | + }], |
| 36 | + properties: ['openFile'] |
| 37 | + }); |
| 38 | + if(o.filePaths.length > 0) processWb(XLSX.readFile(o.filePaths[0])); |
| 39 | +}; |
49 | 40 |
|
50 |
| - drop.addEventListener('dragenter', handleDragover, false); |
51 |
| - drop.addEventListener('dragover', handleDragover, false); |
52 |
| - drop.addEventListener('drop', handleDrop, false); |
53 |
| -})(); |
| 41 | +const exportXlsx = async function() { |
| 42 | + const HTMLOUT = document.getElementById('htmlout'); |
| 43 | + const wb = XLSX.utils.table_to_book(HTMLOUT); |
| 44 | + const o = await electron.dialog.showSaveDialog({ |
| 45 | + title: 'Save file as', |
| 46 | + filters: [{ |
| 47 | + name: "Spreadsheets", |
| 48 | + extensions: EXTENSIONS |
| 49 | + }] |
| 50 | + }); |
| 51 | + console.log(o.filePath); |
| 52 | + XLSX.writeFile(wb, o.filePath); |
| 53 | + electron.dialog.showMessageBox({ message: "Exported data to " + o.filePath, buttons: ["OK"] }); |
| 54 | +}; |
54 | 55 |
|
55 |
| -(function() { |
56 |
| - var readf = document.getElementById('readf'); |
57 |
| - async function handleF(/*e*/) { |
58 |
| - var o = await electron.dialog.showOpenDialog({ |
59 |
| - title: 'Select a file', |
60 |
| - filters: [{ |
61 |
| - name: "Spreadsheets", |
62 |
| - 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("|") |
63 |
| - }], |
64 |
| - properties: ['openFile'] |
65 |
| - }); |
66 |
| - if(o.filePaths.length > 0) process_wb(XLSX.readFile(o.filePaths[0])); |
67 |
| - } |
68 |
| - readf.addEventListener('click', handleF, false); |
69 |
| -})(); |
| 56 | +// add event listeners |
| 57 | +const readBtn = document.getElementById('readBtn'); |
| 58 | +const readIn = document.getElementById('readIn'); |
| 59 | +const exportBtn = document.getElementById('exportBtn'); |
| 60 | +const drop = document.getElementById('drop'); |
70 | 61 |
|
71 |
| -(function() { |
72 |
| - var xlf = document.getElementById('xlf'); |
73 |
| - function handleFile(e) { do_file(e.target.files); } |
74 |
| - xlf.addEventListener('change', handleFile, false); |
75 |
| -})(); |
76 |
| - |
77 |
| -var export_xlsx = (function() { |
78 |
| - var HTMLOUT = document.getElementById('htmlout'); |
79 |
| - var XTENSION = "xls|xlsx|xlsm|xlsb|xml|csv|txt|dif|sylk|slk|prn|ods|fods|htm|html".split("|") |
80 |
| - return async function() { |
81 |
| - var wb = XLSX.utils.table_to_book(HTMLOUT); |
82 |
| - var o = await electron.dialog.showSaveDialog({ |
83 |
| - title: 'Save file as', |
84 |
| - filters: [{ |
85 |
| - name: "Spreadsheets", |
86 |
| - extensions: XTENSION |
87 |
| - }] |
88 |
| - }); |
89 |
| - console.log(o.filePath); |
90 |
| - XLSX.writeFile(wb, o.filePath); |
91 |
| - electron.dialog.showMessageBox({ message: "Exported data to " + o.filePath, buttons: ["OK"] }); |
92 |
| - }; |
93 |
| -})(); |
94 |
| -void export_xlsx; |
| 62 | +readBtn.addEventListener('click', handleReadBtn, false); |
| 63 | +readIn.addEventListener('change', (e) => { readFile(e.target.files); }, false); |
| 64 | +exportBtn.addEventListener('click', exportXlsx, false); |
| 65 | +drop.addEventListener('dragenter', (e) => { |
| 66 | + e.stopPropagation(); |
| 67 | + e.preventDefault(); |
| 68 | + e.dataTransfer.dropEffect = 'copy'; |
| 69 | +}, false); |
| 70 | +drop.addEventListener('dragover', (e) => { |
| 71 | + e.stopPropagation(); |
| 72 | + e.preventDefault(); |
| 73 | + e.dataTransfer.dropEffect = 'copy'; |
| 74 | +}, false); |
| 75 | +drop.addEventListener('drop', (e) => { |
| 76 | + e.stopPropagation(); |
| 77 | + e.preventDefault(); |
| 78 | + readFile(e.dataTransfer.files); |
| 79 | +}, false); |
0 commit comments