-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXLSconversion.js
More file actions
132 lines (101 loc) · 3.18 KB
/
XLSconversion.js
File metadata and controls
132 lines (101 loc) · 3.18 KB
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
132
// Transform email attachment to generate file for ERP System
/* eslint linebreak-style: ["error", "windows"] */
const moment = require('moment');
const XLSX = require('xlsx');
const fs = require('fs');
const ACCOUNT = 'ACCOUNT_NUM';
const HEADERstr = 'HEADER_NUM';
const SPACEPAD1 = ' ';
const SPACEPAD2 = ' ';
// Data from S3, Dynamo, etc. import
const excelFile = 'mySpreadsheet.xls';
exports.handler = async (event, context, callback) => {
try {
let x = await excel(excelFile);
}
catch (err) {
callback(err.message);
}
};
async function excel () {
const sorted = {};
const workbook = XLSX.readFile(excelFile);
const firstSheet = workbook.SheetNames[0];
const theExcel = workbook.Sheets[firstSheet];
const arrExcel = sheet2arr(theExcel);
// Verify the total and numbers
if (verifyTotal(arrExcel)) {
const items = arrExcel.slice(0, arrExcel.length - 1);
// Split the items by date and insert into 1 Object
items.forEach((v) => {
sorted[v[4]] = sorted[v[4]] ? [...sorted[v[4]], v] : [v];
});
// iterate over the object for items sorted by date paid
Object.entries(sorted).forEach(([key, value]) => {
processData(key, value);
});
}
//return ...
}
function processData(key, val) {
const startDate = moment(key, 'MM/DD/YY').format('YYYYMMDD');
const stream = fs.createWriteStream(`${startDate}Wells`);
const begin = HEADERstr + startDate;
// Add windows CRLF
stream.write(`${begin}\r\n`);
const shortDate = startDate.slice(2);
val.forEach((v) => {
const amountPaid = removeCommas(v[3]);
const padded = padEleven(removeDecimal(amountPaid));
const line = ACCOUNT + shortDate + v[0] + SPACEPAD1 + padded + SPACEPAD2 + startDate;
stream.write(`${line}\r\n`);
console.log(line);
});
stream.end();
//return
}
//async function change
function sheet2arr(sheet) {
const result = [];
let row;
let rowNum;
let colNum;
const end = (sheet['!ref']).split(':')[1];
const myRef = 'A10:' + end;
const range = XLSX.utils.decode_range(myRef);
for (rowNum = range.s.r; rowNum <= range.e.r; rowNum++) {
row = [];
for (colNum = range.s.c; colNum <= range.e.c; colNum++) {
const nextCell = sheet[XLSX.utils.encode_cell({ r: rowNum, c: colNum })];
if (typeof nextCell !== 'undefined') {
row.push(nextCell.w);
}
}
if (row.length > 0) {
result.push(row);
}
}
return result;
}
function verifyTotal(sheet) {
const items = sheet.slice(0, sheet.length - 1);
console.log('Total Items: ', items.length);
const total = removeCommas(sheet[sheet.length - 1][0]) * 100;
console.log('Listed Total: ', total);
const itemTotal = items.reduce((pv, cv) => {
const amount = Number(cv[3].replace(/,/g, '')) * 100;
return pv + amount;
}, 0)
console.log('Tabulated Total: ', itemTotal);
console.log('Same total: ', itemTotal == total);
return itemTotal == total;
}
const removeCommas = str => Number(str.replace(/,/g, ''));
const removeDecimal = num => (num * 100).toFixed(0);
const padEleven = (amt) => {
let returnVal;
if (amt <= 99999999999) {
returnVal = ('00000000000' + amt).slice(-11);
}
return returnVal;
};