Browser version
This commit is contained in:
parent
9a08b22055
commit
c34ef257ca
57
bom2table.js
Normal file
57
bom2table.js
Normal file
@ -0,0 +1,57 @@
|
||||
const csvPicker = document.getElementById('csvFile')
|
||||
csvPicker.addEventListener("change", handleFiles, false);
|
||||
|
||||
function handleFiles() {
|
||||
const csvFilePath = this.files[0]
|
||||
let reader = new FileReader()
|
||||
reader.readAsText(csvFilePath)
|
||||
reader.onload = () => makeTable(reader.result)
|
||||
}
|
||||
|
||||
// Which components should we remove from the BOM?
|
||||
const rejectedParts = [
|
||||
'TP1',
|
||||
'TP2',
|
||||
'TP3',
|
||||
'G',
|
||||
'U$1',
|
||||
'J1',
|
||||
'J2',
|
||||
'INPUT'
|
||||
]
|
||||
|
||||
function generateTableHead(table, data) {
|
||||
let thead = table.createTHead();
|
||||
let row = thead.insertRow();
|
||||
// Populate Header row
|
||||
for (let key of data) {
|
||||
let th = document.createElement("th");
|
||||
let text = document. createTextNode(key);
|
||||
th.appendChild(text);
|
||||
row.appendChild(th);
|
||||
}
|
||||
}
|
||||
|
||||
// Return false if the Part value of the object passed in is in the list to remove
|
||||
function isJunk (element) {
|
||||
return !rejectedParts.includes(element.Part)
|
||||
}
|
||||
|
||||
function makeTable(csvString) {
|
||||
csv({
|
||||
delimiter: ";",
|
||||
includeColumns: /(Part|Value)/,
|
||||
ignoreEmpty: true
|
||||
})
|
||||
.fromString(csvString)
|
||||
.then(jsonObj => {
|
||||
// Create array containing only relevant parts
|
||||
let parts = jsonObj.filter(isJunk)
|
||||
console.log(parts)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
// let table = document.querySelector("table")
|
||||
// let headerData = Object.keys(parts[0])
|
||||
// generateTableHead(table, headerData)
|
17
index.html
Normal file
17
index.html
Normal file
@ -0,0 +1,17 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>BOM2Table</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h1>BOM to table</h1>
|
||||
|
||||
<input id="csvFile" type="file" accept=".csv" />
|
||||
|
||||
<script src="https://cdn.rawgit.com/Keyang/node-csvtojson/d41f44aa/browser/csvtojson.min.js"></script>
|
||||
<script src="bom2table.js" defer></script>
|
||||
</body>
|
||||
</html>
|
Loading…
x
Reference in New Issue
Block a user