Last active
November 24, 2023 16:26
-
-
Save TechplexEngineer/c8e9ee8dce41a62e87987c9db7dadf4b to your computer and use it in GitHub Desktop.
Extract first choice priority list to tab separated values for pasting into a spreadsheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ---- First extract the table headers | |
hdrs = [] | |
$("#PriorityTable tr.cart-header-row").each((idx, row) => { | |
$("th", row).each((idx2, col)=>{ | |
hdrs.push($(col).text()) | |
}) | |
}) | |
hdrs.push("Part Number") | |
// console.log("Headers:",hdrs) | |
// ---- Next extract the data from the rows | |
data = [] | |
$("#PriorityTable tr.cart-item-row").each((ridx, row) => { | |
data[ridx] = {} | |
$("td", row).each((cidx, col)=>{ | |
hdr = hdrs[cidx] | |
txt = $(col).text().trim() | |
if (cidx == 0) { //remove | |
return true; //continue (hide the remove column) | |
} | |
if (cidx == 1) { //priority | |
txt = $(".priority-number", col).val() | |
} | |
if (cidx == 4) { //quantity | |
txt = $(".qty-input", col).val() | |
} | |
if (cidx == 6) { //link | |
data[ridx]["Part Number"] = txt | |
txt = "https://firstchoicebyandymark.com/"+txt | |
} | |
data[ridx][hdr] = txt | |
}) | |
// return false // equivalent of break (if you only want one row processed) | |
}) | |
// If you just want the raw JSON: | |
// console.log(JSON.stringify(data, null, 4)) | |
// ---- convert the row data to tab separated values | |
cleanHeaders = hdrs.map(e=>{ | |
if (e !== hdrs[0]) return e // remove the first column | |
}).filter(Boolean).filter(h=>h != "Part Number") | |
cleanHeaders.unshift("Part Number") | |
tsv = "" | |
tsv = cleanHeaders.join("\t") | |
tsv += "\n" | |
data.map((r) => { | |
tsv += cleanHeaders.map((key, idx)=>{ | |
return r[key] | |
}).join("\t") | |
tsv += "\n" | |
}) | |
prompt("Copy the exported data:", tsv); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment