-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
209 lines (188 loc) · 5.93 KB
/
index.js
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
let cellState = []
const gridSize = 100
const tableBody = document.getElementById("sheet")
// creates initial grid matrix based on gridSize
const generateGrid = (n) => {
let grid = []
for (let i = 0; i <= n; i++) {
let row = []
for (let j = 0; j <= n; j++) {
let cell = {
value: "",
formula: "",
rowCoord: i,
colCoord: j
}
row.push(cell)
}
grid.push(row)
}
return grid
}
// checks forumala string and returns operators in an array
const isOperator = (inputStr) => {
let operatorArr = inputStr.filter(str => {
if (str === "+") return true
if (str === "-") return true
if (str === "*") return true
if (str === "/") return true
})
return operatorArr
}
// calls creates string from coords and operators to pass to calculate
const cellValuesToFormulaStr = (coordsArr, operatorsArr) => {
let formulaStr = ""
let cellValuesArr = coordsArr.map(coord => {
return cellState[coord.row][coord.col].value
})
operatorsArr.forEach((operator, i) => {
formulaStr += cellValuesArr[i] + operator
})
formulaStr = formulaStr.concat(cellValuesArr.pop())
return formulaStr
}
// function to get coordinates from input value and return in 2d array
const coordFinder = (inputCharArray, operators) => {
let charArr = []
let i
let k
for (i = 0, k = -1; i < inputCharArray.length; i++) {
if((i + 1) >= inputCharArray.length){
charArr[k].push(inputCharArray[i])
charArr[k].pop()
} else if (operators.indexOf(inputCharArray[i + 1]) !== 0) {
k++;
charArr[k] = [];
}
charArr[k].push(inputCharArray[i])
}
let coordArr = charArr.filter(chars => {
return chars.length == 2
})
return coordArr
}
// formatter to convert alphabetic label to column index
const inputValueToFormulaConverter = (inputValue) => {
let charArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
let formula = inputValue.substring(1)
let inputStrArr = formula.split('')
let operatorArr = isOperator(inputStrArr)
// currently working on getting coords into an array so I can operate on more than one cell
let coordArr = coordFinder(inputStrArr, operatorArr)
// needs refactoring, can only operate using one operator
let gridCoordArr = formula.split(operatorArr[0]).map((coords, i) => {
let gridCoords = coords.split('').map((coord, i) => {
if (isNaN(coord)) {
// need to change this so it can handle mutliple letter and number coords e.g. AA10
coord = charArray.indexOf(coord) + 1
}
return coord
})
return {
row: gridCoords[1],
col: gridCoords[0],
}
})
return [...gridCoordArr, operatorArr]
}
const calculate = (formulaCoords) => {
let coordsArr = inputValueToFormulaConverter(formulaCoords)
let operatorArr = coordsArr.pop()
let formula = cellValuesToFormulaStr(coordsArr, operatorArr)
return eval(formula)
}
// refreshes grid with cellState data
const refresh = (e) => {
document.getElementById("sheet").innerHTML = ''
render(cellState)
}
// handles basic formula input to a cell
const basicFormula = (targetCell) => {
cellState = cellState.map(row => {
return row.map(cell => {
if (cell.colCoord == targetCell.data.colCoord && cell.rowCoord == targetCell.data.rowCoord) {
let formula = cell.formula == "" ? targetCell.value : cell.formula
cell.formula = formula
cell.value = calculate(formula)
}
return cell
})
})
refresh()
}
// updates the cellState data following user input
const setCellStateFromInput = (targetCell) => {
cellState = cellState.map(row => {
return row.map(cell => {
if (cell.colCoord == targetCell.data.colCoord && cell.rowCoord == targetCell.data.rowCoord) {
cell.value = targetCell.value
}
return cell
})
})
refresh()
}
// handles input information to update cell data
const handleInput = (e) => {
if (e.target.value[0] == "=") {
basicFormula(e.target)
} else {
setCellStateFromInput(e.target)
}
}
// formatter to convert column index to incrementing alphabetic label
const indexToLetters = (n) => {
let charArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
let firstLetter = (charArray[Math.ceil(n / 26) - 2] || '')
let secondLetter = charArray[(n - 1) % 26]
return firstLetter + secondLetter
}
const createCellNode = (cell) => {
const newCell = document.createElement('td')
const input = document.createElement('input')
input.data = cell
input.onchange = handleInput
// checks to see if first cell and creates button
if (cell.colCoord == 0 && cell.rowCoord == 0) {
const refreshButton = document.createElement('button')
refreshButton.onclick = refresh
refreshButton.setAttribute('id', 'refresh-button')
newCell.appendChild(refreshButton)
// checks to see if first column to add index formatting
} else if (cell.colCoord == 0) {
newCell.innerHTML = cell.rowCoord
newCell.classList.add('first-col')
// checks to see if first row to add index formatting
} else if (cell.rowCoord == 0 && cell.colCoord !== 0) {
newCell.innerHTML = indexToLetters(cell.colCoord)
newCell.classList.add('first-row')
// checks to see if cell has formula
} else if (cell.formula !== "") {
input.value = calculate(cell.formula)
newCell.appendChild(input)
// appends an input to each cell and gives it information references matching cellState data cell
} else {
input.value = cell.value
newCell.appendChild(input)
}
return newCell
}
const createRowNode = (row) => {
let newRow = document.createElement('tr')
row.forEach(cell => {
newRow.appendChild(createCellNode(cell))
})
return newRow
}
// renders grid based on cellState
const render = (grid) => {
return grid.forEach((row, i) => {
tableBody.appendChild(createRowNode(row))
});
}
//initialises the spreadsheet by generating the grid and passing it to render
const start = () => {
cellState = generateGrid(gridSize)
render(cellState)
}
document.addEventListener('DOMContentLoaded', start)