-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
135 lines (118 loc) · 4.4 KB
/
index.html
File metadata and controls
135 lines (118 loc) · 4.4 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GridXcel App</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f8f9fa;
}
.container {
margin-top: 20px;
overflow-x: auto;
}
table {
border-collapse: collapse;
width: 100%;
}
td, th {
width: 100px;
height: 50px;
border: 1px solid #dee2e6;
text-align: center;
vertical-align: middle;
cursor: pointer;
background-color: #ffffff;
}
input {
width: 30px auto;
padding: 5px;
border: none;
text-align: center;
background-color: transparent;
}
.selected {
background-color: #d0e0f0;
}
.formula-bar {
margin-bottom: 15px;
}
</style>
</head>
<body>
<div class="container">
<div class="formula-bar">
<input type="text" id="formulaInput" class="form-control" placeholder="Enter a formula (e.g., =SUM(A1:A5))" />
</div>
<table id="excelTable" class="table table-bordered"></table>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
<script>
const rows = 1200;
const cols = 800;
const table = document.getElementById('excelTable');
const formulaInput = document.getElementById('formulaInput');
// Initialize the table
for (let i = 0; i < rows; i++) {
const row = table.insertRow();
for (let j = 0; j < cols; j++) {
const cell = row.insertCell();
const input = document.createElement('input');
input.type = 'text';
input.id = `cell-${i}-${j}`;
input.addEventListener('input', updateCell);
input.addEventListener('click', selectCell);
cell.appendChild(input);
}
}
function updateCell(event) {
const input = event.target;
let value = input.value.trim();
if (value.startsWith('=') && value.length > 1) {
input.value = evaluateFormula(value.slice(1));
}
}
function selectCell(event) {
document.querySelectorAll('input').forEach(cell => cell.classList.remove('selected'));
event.target.classList.add('selected');
}
function evaluateFormula(formula) {
try {
if (formula.startsWith('SUM')) return calculateRange(formula, 'SUM');
if (formula.startsWith('AVERAGE')) return calculateRange(formula, 'AVERAGE');
if (formula.startsWith('MIN')) return calculateRange(formula, 'MIN');
if (formula.startsWith('MAX')) return calculateRange(formula, 'MAX');
return formula;
} catch (error) {
return 'ERROR';
}
}
function calculateRange(formula, type) {
const match = formula.match(/([A-Z]+)(\d+):([A-Z]+)(\d+)/);
if (!match) return 'INVALID';
const [_, col1, row1, col2, row2] = match;
const startRow = parseInt(row1) - 1;
const startCol = col1.charCodeAt(0) - 65;
const endRow = parseInt(row2) - 1;
const endCol = col2.charCodeAt(0) - 65;
let values = [];
for (let r = startRow; r <= endRow; r++) {
for (let c = startCol; c <= endCol; c++) {
const cell = document.getElementById(`cell-${r}-${c}`);
if (cell) values.push(parseFloat(cell.value) || 0);
}
}
if (type === 'SUM') return values.reduce((a, b) => a + b, 0);
if (type === 'AVERAGE') return values.length ? (values.reduce((a, b) => a + b, 0) / values.length) : 0;
if (type === 'MIN') return Math.min(...values);
if (type === 'MAX') return Math.max(...values);
return 'INVALID';
}
</script>
</body>
</html>