|
55 | 55 | <div class="formula-bar"> |
56 | 56 | <input type="text" id="formulaInput" class="form-control" placeholder="Enter a formula (e.g., =SUM(A1:A5))" /> |
57 | 57 | </div> |
58 | | - <table id="excelTable" class="table table-bordered"> |
59 | | - <!-- Table will be dynamically populated by JavaScript --> |
60 | | - </table> |
| 58 | + <table id="excelTable" class="table table-bordered"></table> |
61 | 59 | </div> |
62 | 60 |
|
63 | 61 | <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script> |
64 | 62 | <script> |
65 | | - const rows = 20; // Adjust to manage performance |
66 | | - const cols = 20; // Adjust to manage performance |
| 63 | + const rows = 20; |
| 64 | + const cols = 20; |
67 | 65 | const table = document.getElementById('excelTable'); |
68 | 66 | const formulaInput = document.getElementById('formulaInput'); |
69 | 67 |
|
|
81 | 79 | } |
82 | 80 | } |
83 | 81 |
|
84 | | - // Update cell when input is changed |
85 | 82 | function updateCell(event) { |
86 | 83 | const input = event.target; |
87 | | - const [_, row, col] = input.id.split('-').map(Number); |
88 | 84 | let value = input.value.trim(); |
89 | 85 |
|
90 | | - // Formula handling |
91 | 86 | if (value.startsWith('=') && value.length > 1) { |
92 | | - value = parseFormula(value.slice(1), row, col); |
93 | | - input.value = value; |
| 87 | + input.value = evaluateFormula(value.slice(1)); |
94 | 88 | } |
95 | 89 | } |
96 | 90 |
|
97 | | - // Handle cell selection |
98 | 91 | function selectCell(event) { |
99 | | - const allCells = document.querySelectorAll('input'); |
100 | | - allCells.forEach(cell => cell.classList.remove('selected')); |
| 92 | + document.querySelectorAll('input').forEach(cell => cell.classList.remove('selected')); |
101 | 93 | event.target.classList.add('selected'); |
102 | 94 | } |
103 | 95 |
|
104 | | - // Formula parser (example: SUM, AVERAGE, etc.) |
105 | | - function parseFormula(formula, row, col) { |
106 | | - if (formula.startsWith('SUM')) { |
107 | | - return parseSumFormula(formula, row, col); |
108 | | - } else if (formula.startsWith('AVERAGE')) { |
109 | | - return parseAverageFormula(formula, row, col); |
110 | | - } else if (formula.startsWith('MIN')) { |
111 | | - return parseMinFormula(formula, row, col); |
112 | | - } else if (formula.startsWith('MAX')) { |
113 | | - return parseMaxFormula(formula, row, col); |
114 | | - } else if (formula.startsWith('COUNT')) { |
115 | | - return parseCountFormula(formula, row, col); |
116 | | - } else if (formula.startsWith('IF')) { |
117 | | - return parseIfFormula(formula, row, col); |
118 | | - } else if (formula.startsWith('ROUND')) { |
119 | | - return parseRoundFormula(formula, row, col); |
120 | | - } else if (formula.startsWith('CONCATENATE')) { |
121 | | - return parseConcatenateFormula(formula, row, col); |
122 | | - } else if (formula.startsWith('LEN')) { |
123 | | - return parseLenFormula(formula, row, col); |
124 | | - } else if (formula.startsWith('PRODUCT')) { |
125 | | - return parseProductFormula(formula, row, col); |
126 | | - } else if (formula.startsWith('SQRT')) { |
127 | | - return parseSqrtFormula(formula, row, col); |
128 | | - } else if (formula.startsWith('MOD')) { |
129 | | - return parseModFormula(formula, row, col); |
130 | | - } else if (formula.startsWith('POWER')) { |
131 | | - return parsePowerFormula(formula, row, col); |
132 | | - } else if (formula.startsWith('ABS')) { |
133 | | - return parseAbsFormula(formula, row, col); |
134 | | - } else if (formula.startsWith('MAXA')) { |
135 | | - return parseMaxAFormula(formula, row, col); |
136 | | - } else if (formula.startsWith('MINA')) { |
137 | | - return parseMinAFormula(formula, row, col); |
138 | | - } else if (formula.startsWith('TODAY')) { |
139 | | - return parseTodayFormula(formula, row, col); |
140 | | - } else if (formula.startsWith('NOW')) { |
141 | | - return parseNowFormula(formula, row, col); |
142 | | - } else if (formula.startsWith('TRIM')) { |
143 | | - return parseTrimFormula(formula, row, col); |
144 | | - } else if (formula.startsWith('ISNUMBER')) { |
145 | | - return parseIsNumberFormula(formula, row, col); |
146 | | - } else if (formula.startsWith('ISEVEN')) { |
147 | | - return parseIsEvenFormula(formula, row, col); |
148 | | - } else if (formula.startsWith('ISODD')) { |
149 | | - return parseIsOddFormula(formula, row, col); |
150 | | - } else if (formula.startsWith('IFERROR')) { |
151 | | - return parseIfErrorFormula(formula, row, col); |
| 96 | + function evaluateFormula(formula) { |
| 97 | + try { |
| 98 | + if (formula.startsWith('SUM')) return calculateRange(formula, 'SUM'); |
| 99 | + if (formula.startsWith('AVERAGE')) return calculateRange(formula, 'AVERAGE'); |
| 100 | + if (formula.startsWith('MIN')) return calculateRange(formula, 'MIN'); |
| 101 | + if (formula.startsWith('MAX')) return calculateRange(formula, 'MAX'); |
| 102 | + return formula; |
| 103 | + } catch (error) { |
| 104 | + return 'ERROR'; |
152 | 105 | } |
153 | | - // Add more formulas as needed |
154 | | - return formula; |
155 | 106 | } |
156 | 107 |
|
157 | | - // Example formulas |
158 | | - function parseSumFormula(formula, row, col) { |
159 | | - const rangeMatch = formula.match(/SUM(\w+)(\d+):(\w+)(\d+)/); |
160 | | - if (rangeMatch) { |
161 | | - const startCol = rangeMatch[1].charCodeAt(0) - 65; |
162 | | - const startRow = parseInt(rangeMatch[2]) - 1; |
163 | | - const endCol = rangeMatch[3].charCodeAt(0) - 65; |
164 | | - const endRow = parseInt(rangeMatch[4]) - 1; |
165 | | - let sum = 0; |
166 | | - for (let r = startRow; r <= endRow; r++) { |
167 | | - for (let c = startCol; c <= endCol; c++) { |
168 | | - const targetCell = document.getElementById(`cell-${r}-${c}`); |
169 | | - const value = parseFloat(targetCell.value) || 0; |
170 | | - sum += value; |
171 | | - } |
| 108 | + function calculateRange(formula, type) { |
| 109 | + const match = formula.match(/([A-Z]+)(\d+):([A-Z]+)(\d+)/); |
| 110 | + if (!match) return 'INVALID'; |
| 111 | + |
| 112 | + const [_, col1, row1, col2, row2] = match; |
| 113 | + const startRow = parseInt(row1) - 1; |
| 114 | + const startCol = col1.charCodeAt(0) - 65; |
| 115 | + const endRow = parseInt(row2) - 1; |
| 116 | + const endCol = col2.charCodeAt(0) - 65; |
| 117 | + |
| 118 | + let values = []; |
| 119 | + for (let r = startRow; r <= endRow; r++) { |
| 120 | + for (let c = startCol; c <= endCol; c++) { |
| 121 | + const cell = document.getElementById(`cell-${r}-${c}`); |
| 122 | + if (cell) values.push(parseFloat(cell.value) || 0); |
172 | 123 | } |
173 | | - return sum; |
174 | 124 | } |
175 | | - return formula; |
176 | | - } |
177 | 125 |
|
178 | | - function parseAverageFormula(formula, row, col) { |
179 | | - const rangeMatch = formula.match(/AVERAGE(\w+)(\d+):(\w+)(\d+)/); |
180 | | - if (rangeMatch) { |
181 | | - const startCol = rangeMatch[1].charCodeAt(0) - 65; |
182 | | - const startRow = parseInt(rangeMatch[2]) - 1; |
183 | | - const endCol = rangeMatch[3].charCodeAt(0) - 65; |
184 | | - const endRow = parseInt(rangeMatch[4]) - 1; |
185 | | - let sum = 0, count = 0; |
186 | | - for (let r = startRow; r <= endRow; r++) { |
187 | | - for (let c = startCol; c <= endCol; c++) { |
188 | | - const targetCell = document.getElementById(`cell-${r}-${c}`); |
189 | | - const value = parseFloat(targetCell.value) || 0; |
190 | | - sum += value; |
191 | | - count++; |
192 | | - } |
193 | | - } |
194 | | - return count > 0 ? sum / count : 0; |
195 | | - } |
196 | | - return formula; |
197 | | - } |
| 126 | + if (type === 'SUM') return values.reduce((a, b) => a + b, 0); |
| 127 | + if (type === 'AVERAGE') return values.length ? (values.reduce((a, b) => a + b, 0) / values.length) : 0; |
| 128 | + if (type === 'MIN') return Math.min(...values); |
| 129 | + if (type === 'MAX') return Math.max(...values); |
198 | 130 |
|
199 | | - function parseMinFormula(formula, row, col) { |
200 | | - const rangeMatch = formula.match(/MIN(\w+)(\d+):(\w+)(\d+)/); |
201 | | - if (rangeMatch) { |
202 | | - const startCol = rangeMatch[1].charCodeAt(0) - 65; |
203 | | - const startRow = parseInt(rangeMatch[2]) - 1; |
204 | | - const endCol = rangeMatch[3].charCodeAt(0) - 65; |
205 | | - const endRow = parseInt(rangeMatch[4]) - 1; |
206 | | - let min = Infinity; |
207 | | - for (let r = startRow; r <= endRow; r++) { |
208 | | - for (let c = startCol; c <= endCol; c++) { |
209 | | - const targetCell = document.getElementById(`cell-${r}-${c}`); |
210 | | - const value = parseFloat(targetCell.value) || Infinity; |
211 | | - min = Math.min(min, value); |
212 | | - } |
213 | | - } |
214 | | - return min; |
215 | | - } |
216 | | - return formula; |
| 131 | + return 'INVALID'; |
217 | 132 | } |
218 | | - |
219 | | - // Add more formulas here like MAX, COUNT, etc. |
220 | | - function parseMaxFormula(formula, row, col) { ... } |
221 | | - function parseCountFormula(formula, row, col) { ... } |
222 | | - function parseIfFormula(formula, row, col) { ... } |
223 | | - function parseRoundFormula(formula, row, col) { ... } |
224 | | - function parseConcatenateFormula(formula, row, col) { ... } |
225 | | - function parseLenFormula(formula, row, col) { ... } |
226 | | - function parseProductFormula(formula, row, col) { ... } |
227 | | - function parseSqrtFormula(formula, row, col) { ... } |
228 | | - function parseModFormula(formula, row, col) { ... } |
229 | | - function parsePowerFormula(formula, row, col) { ... } |
230 | | - function parseAbsFormula(formula, row, col) { ... } |
231 | | - function parseMaxAFormula(formula, row, col) { ... } |
232 | | - function parseMinAFormula(formula, row, col) { ... } |
233 | | - function parseTodayFormula(formula, row, col) { ... } |
234 | | - function parseNowFormula(formula, row, col) { ... } |
235 | | - function parseTrimFormula(formula, row, col) { ... } |
236 | | - function parseIsNumberFormula(formula, row, col) { ... } |
237 | | - function parseIsEvenFormula(formula, row, col) { ... } |
238 | | - function parseIsOddFormula(formula, row, col) { ... } |
239 | | - function parseIfErrorFormula(formula, row, col) { ... } |
240 | | - |
241 | 133 | </script> |
242 | 134 | </body> |
243 | 135 | </html> |
0 commit comments