Untitled
unknown
plain_text
13 days ago
1.7 kB
2
Indexable
Never
// We want to support the following methods for a spreadsheet software // GetCell(key) // SetCel(key, value) // the value of any cell can consists of either plain values or sometimes a formulae something like `A1 + B2`. In those cases we are supposed to compute the value and provide it to us. let excel = { // A : [] // 0 -> A1... An } function setCel (key, value) { let colNum = parseInt(key.substring(1, key.length)); let rowNum = key.split("")[0]; if (!excel[rowNum]) { excel[rowNum] = []; } excel[rowNum][colNum] = value; } function getCel (key) { // formula // A12 let colNum = parseInt(key.substring(1, key.length)); let rowNum = key.split("")[0]; let value = excel[rowNum][colNum]; // A12 console.log(isFormula(value)) if (!isFormula(value)) return value let computeStr = ""; // A3 = A1 + A2; value.split(" ").forEach(item => { let colNum = parseInt(item.substring(1, key.length)); let rowNum = item.split("")[0]; // ["A1", "+", "A2"] let ele = item; if (!["+", "-"].includes(item)) { ele = excel[rowNum][colNum] } computeStr += ele; // 12+50 }); // console.log("computeStrcomputeStr ", computeStr); return eval(computeStr); } "123" function isFormula (value) { // let rowsKeys = Object.keys(excel); // console.log("fbhibf ", value , isNaN(Number(value))) return isNaN(Number(value)); } setCel("A1", 5) setCel("A2", 10) setCel("A3", "A1 + A2") let v = getCel("A3") let v2 = getCel("A1") console.log(v, v2) // setCel("A3", 5)
Leave a Comment