-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
131 lines (106 loc) · 3.59 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
var invioceList;
function upload() {
document.getElementById("copyTextButton").style.display = "block";
document.getElementById("verifyInvoices").style.display = "block";
let files = document.getElementById('fileUpload').files;
if (files.length==0) {
alert("Please choose any file...");
return;
}
let filename = files[0].name;
let extension = filename.substring(filename.lastIndexOf(".")).toUpperCase();
if (extension == '.XLS' || extension == '.XLSX') {
processFile(files[0]);
}
else {
alert("Please select a valid excel file.");
}
}
function doesFileExist(f, directory) {
// need to refernce files, not strings
return f;
}
async function verifyInvoices() {
let directory;
directory = await window.showDirectoryPicker({
startIn: 'desktop'
});
let invoiceArr = invoiceList.split(" ");
for await (const invoice of invoiceArr) {
console.log(doesFileExist(invoice, directory));
}
}
function copyText() {
/* Get the text field */
let copiedText = document.getElementById("sheetResults");
/* Select the text field */
copiedText.select();
copiedText.setSelectionRange(0, 99999); /* For mobile devices */
/* Copy the text inside the text field */
navigator.clipboard.writeText(copiedText.value);
/* Alert the copied text */
alert("Copied the text: " + copiedText.value);
}
function getTotalRows(invoices) {
return invoices.length;
}
function getTotalAmount(sheet) {
let x = 0;
let total = 0;
while (x < sheet.length) {
total += sheet[x]['InvAmt'];
x++;
}
return total;
}
function getInvoices(sheet) {
let x = 0;
let invoiceArr = [];
while (x < sheet.length) {
invoiceArr.push('"' + sheet[x]['Invoice#'] + '"');
x++;
}
invoiceArr = invoiceArr.join(' ');
return invoiceArr;
}
// Method to read and process excel file
function processFile(file) {
try {
let reader = new FileReader();
reader.readAsBinaryString(file);
reader.onload = function(e) {
let data = e.target.result;
let workbook = XLSX.read(data, {
type : 'binary'
});
let lastSheet = workbook.SheetNames[workbook.SheetNames.length-1];
// store sheets in JSON
let result = {};
workbook.SheetNames.forEach(function(sheetName) {
let roa = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
if (roa.length > 0) {
result[sheetName] = roa;
}
});
// get list of invoices
invoiceList = getInvoices(result[lastSheet]);
// get total invoice count
let totalInvoices = getTotalRows(result[lastSheet]);
// get total upload amount
let totalAmount = (getTotalAmount(result[lastSheet])).toLocaleString('en-US', {
style: 'currency',
currency: 'USD',
});;
// display results
var sheetStats = document.getElementById("totalInvoices");
sheetStats.textContent = "TOTAL INVOICES: " + totalInvoices;
var sheetStats = document.getElementById("totalAmount");
sheetStats.textContent = "TOTAL AMOUNT: " + totalAmount;
var resultEle=document.getElementById("sheetResults");
resultEle.textContent = invoiceList;
resultEle.style.display='block';
}
}catch(e) {
console.error(e);
}
}