Skip to navigation
Example to fill textarea with excel and csv files
27.04.23
with the help of https://www.npmjs.com/package/xlsx window.onload = async function() { const api = await get_api_url('sapir'); const url = api + '/' + page; const host = await get_grid_url('https://grid.salamander-jewelry.net', 'http://127.0.0.1'); let styles = JSON.parse(await vwu.aget_api(api + '/style/name')); document.querySelector("#btn_check_now").addEventListener("click", function(){check_order(api, url, host);}, false); document.querySelector("#input").addEventListener("change", function(){ input_select(this,styles) }, false); document.querySelector("#btn_example").addEventListener("click", function(){random_filler(styles)}, false); document.querySelector("#btn_clear").addEventListener("click", function(){ clear_textarea();}, false); }; async function clear_textarea() { let t = document.getElementsByTagName("textarea") for(let i in t) { if(t[i].type === 'textarea') { t[i].value = ""; } } } async function random_filler(styles) { let rows = [] for(let x=0;x<=10;x++) { let rkey = Math.floor(Math.random() * styles.length); let key = styles[rkey]; let rvalue = Math.floor(Math.random() * 1000); let r = {style:key, qty:rvalue}; rows.push(r); } await set_to_input(rows); } async function input_select(o,styles) { for(let i in o.files) { if(o.files[i].type === 'text/csv') { log.info("...load csv"); const rows = await csv_file_reader(o.files[i],styles); const v = await validate_rows(rows, styles); await set_to_input(v); } else if(o.files[i].type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' || o.files[i].type === 'application/vnd.ms-excel') { log.info("...load xlsx file"); const rows = await xlsx_file_reader(o.files[i],styles); const v = await validate_rows(rows, styles); await set_to_input(v); } } } async function xlsx_file_reader(file, styles) { let rows = []; const workbook = XLSX.read(await file.arrayBuffer(), {type: 'binary'}); for(let i in workbook.Sheets) { row = XLSX.utils.sheet_to_json(workbook.Sheets[i],{raw:true,header:1}); rows = rows.concat(row); } return rows; } async function csv_file_reader(file, styles) { let rows = []; const str = await file.text(); const lines = str.split(/[\r\n]+/); for(let i in lines) { let row = lines[i].split(/\s*,\s*/).map(x => (x === '') ? '' : (isNaN(Number(x)) ? x : Number(x)) ) rows.push(row); } return rows; } async function validate_rows(rows, styles) { let result =[] if(rows.length <=0 ) { return result; } for(let r in rows) { for(let c in rows[r]) { let cell = rows[r][c]; if(typeof(cell) == 'string') { cell = cell.replace("__","/"); } if(styles.includes(cell)) { let n = parseInt(c) +1; let qty = rows[r][n]; if (qty === parseInt(qty, 10) ) { result.push({style:cell,qty:qty}); } else{ let n2 = parseInt(c) +2; let qty2 = rows[r][n2]; if (qty2 === parseInt(qty2, 10) ) { result.push({style:cell,qty:qty2}); } } } } } return result; } async function set_to_input(r) { for(let i in r) { const keys = Object.keys(r[i]); for(let k in keys) { let e = document.querySelector('#'+keys[k]) || false if(e) { if (e.tagName === 'TEXTAREA' && e.type === 'textarea') { e.value = e.value.trim() + "\n" + r[i][keys[k]]; } } } } }
https://www.npmjs.com/package/xlsx
Reply
Anonymous
Information Epoch 1742226491
Files are bags of bytes.
Home
Notebook
Contact us