-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathExcelViewer.jsx
More file actions
322 lines (278 loc) · 9.4 KB
/
ExcelViewer.jsx
File metadata and controls
322 lines (278 loc) · 9.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import React, { useState, useEffect, useRef } from 'react';
import { Box, Typography, CircularProgress, Alert, Tabs, Tab } from '@mui/material';
import * as XLSX from 'xlsx';
import Table from '@wolf-table/table';
import { useTranslation } from 'react-i18next';
import { apiFetch } from '../services/api';
import '@wolf-table/table/dist/table.min.css';
// Add custom CSS to override wolf-table fonts
const customStyle = document.createElement('style');
customStyle.textContent = `
.wolf-table,
.wolf-table *,
.wolf-table-cell,
.wolf-table-cell-text {
font-family: Roboto, sans-serif !important;
}
`;
if (!document.getElementById('wolf-table-custom-font')) {
customStyle.id = 'wolf-table-custom-font';
document.head.appendChild(customStyle);
}
/**
* ExcelViewer - Displays Excel files using SheetJS and wolf-table
*
* Supports: .xls, .xlsx
* Features:
* - Interactive spreadsheet grid with familiar Excel-like interface
* - Multiple sheet support with tabs
* - Read-only view (editing disabled)
* - Scrollable, resizable, and selectable cells
*/
export default function ExcelViewer({ filename, projectName }) {
const { t } = useTranslation();
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const [metadata, setMetadata] = useState(null);
const [workbook, setWorkbook] = useState(null);
const [activeSheet, setActiveSheet] = useState(0);
const [sheetDimensions, setSheetDimensions] = useState(null);
const containerRef = useRef(null);
const tableRef = useRef(null);
// First effect: Load and parse the Excel file
useEffect(() => {
if (!filename || !projectName) return;
const loadExcelFile = async () => {
try {
setLoading(true);
setError(null);
// Fetch the Excel file
const response = await apiFetch(`/api/workspace/${projectName}/files/${filename}`);
if (!response.ok) {
throw new Error(`Failed to load Excel file: ${response.statusText}`);
}
const arrayBuffer = await response.arrayBuffer();
// Parse the Excel file using SheetJS
const wb = XLSX.read(arrayBuffer, {
type: 'array',
cellStyles: true,
cellNF: true,
cellDates: true
});
// Extract metadata
const sheetNames = wb.SheetNames;
const sheetCount = sheetNames.length;
setMetadata({
sheetCount,
sheetNames,
fileSize: formatFileSize(arrayBuffer.byteLength)
});
setWorkbook(wb);
setLoading(false);
} catch (err) {
console.error('Error loading Excel file:', err);
setError(err.message);
setLoading(false);
}
};
loadExcelFile();
}, [filename, projectName]);
// Second effect: Initialize wolf-table when sheet changes
useEffect(() => {
if (!workbook || !containerRef.current) return;
try {
// Clean up existing table instance
if (tableRef.current) {
tableRef.current = null;
}
// Clear container
containerRef.current.innerHTML = '';
const sheetName = workbook.SheetNames[activeSheet];
const sheet = workbook.Sheets[sheetName];
if (!sheet || !sheet['!ref']) {
console.warn(`Sheet "${sheetName}" has no range (!ref)`);
setSheetDimensions(null);
return;
}
// Calculate sheet dimensions
const range = XLSX.utils.decode_range(sheet['!ref']);
const rows = range.e.r + 1;
const cols = range.e.c + 1;
setSheetDimensions({ rows, cols });
// Convert sheet to wolf-table format
const tableData = convertSheetToWolfTable(sheet);
console.log(`Rendering sheet "${sheetName}" with ${tableData.cells.length} cells`);
// Get container dimensions
const width = containerRef.current.offsetWidth;
const height = containerRef.current.offsetHeight;
console.log(`Creating table with dimensions: ${width}x${height}`);
// Create wolf-table instance
const table = Table.create(
containerRef.current,
() => width,
() => height,
{
scrollable: true,
resizable: true,
selectable: true,
editable: false, // Read-only
copyable: true,
}
);
// Load data into table
table.data(tableData).render();
tableRef.current = table;
} catch (err) {
console.error('Error initializing wolf-table:', err);
console.error('Full stack:', err.stack);
setError(err.message + ' (Check console for details)');
}
// Cleanup on unmount
return () => {
if (tableRef.current) {
tableRef.current = null;
}
};
}, [workbook, activeSheet]);
/**
* Convert SheetJS sheet to wolf-table format
* wolf-table expects: { styles: [...], cells: [[row, col, value], ...] }
*/
const convertSheetToWolfTable = (sheet) => {
const range = XLSX.utils.decode_range(sheet['!ref']);
const cells = [];
const styles = [];
const styleMap = new Map(); // Map to reuse styles
console.log(`Processing range: ${sheet['!ref']}, Rows: ${range.e.r + 1}, Cols: ${range.e.c + 1}`);
// Process cells
for (let R = range.s.r; R <= range.e.r; ++R) {
for (let C = range.s.c; C <= range.e.c; ++C) {
const cellRef = XLSX.utils.encode_cell({ r: R, c: C });
const cell = sheet[cellRef];
if (!cell) {
// Skip empty cells
continue;
}
// Get cell value
const value = cell.w || String(cell.v || '');
// Handle cell styling
let styleIndex = undefined;
if (cell.s) {
const styleKey = JSON.stringify(cell.s);
if (styleMap.has(styleKey)) {
styleIndex = styleMap.get(styleKey);
} else {
const style = {};
// Font
if (cell.s.font) {
if (cell.s.font.bold) style.bold = true;
if (cell.s.font.italic) style.italic = true;
if (cell.s.font.underline) style.underline = true;
if (cell.s.font.strike) style.strikethrough = true;
if (cell.s.font.sz) style.fontSize = cell.s.font.sz;
if (cell.s.font.color && cell.s.font.color.rgb) {
style.color = '#' + cell.s.font.color.rgb;
}
}
// Alignment
if (cell.s.alignment) {
if (cell.s.alignment.horizontal) {
style.align = cell.s.alignment.horizontal;
}
}
if (Object.keys(style).length > 0) {
styleIndex = styles.length;
styles.push(style);
styleMap.set(styleKey, styleIndex);
}
}
}
// Add cell to array
if (styleIndex !== undefined) {
cells.push([R, C, { value, style: styleIndex }]);
} else {
cells.push([R, C, value]);
}
}
}
console.log(`Converted ${cells.length} cells with ${styles.length} unique styles`);
return { styles, cells };
};
/**
* Format file size to human-readable format
*/
const formatFileSize = (bytes) => {
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(2)} KB`;
return `${(bytes / (1024 * 1024)).toFixed(2)} MB`;
};
if (loading) {
return (
<Box sx={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '100%', p: 4 }}>
<CircularProgress />
</Box>
);
}
if (error) {
return (
<Box sx={{ p: 4 }}>
<Alert severity="error">
<Typography variant="h6" gutterBottom>{t('excelViewer.errorLoading')}</Typography>
<Typography>{error}</Typography>
</Alert>
</Box>
);
}
return (
<Box sx={{ height: '100%', display: 'flex', flexDirection: 'column' }}>
{/* Metadata Header */}
{metadata && (
<Box sx={{ p: 2, backgroundColor: '#f5f5f5', borderBottom: '1px solid #ddd' }}>
<Typography variant="body2" sx={{ fontWeight: 'bold', mb: 0.5 }}>
{filename}
</Typography>
<Typography variant="caption" sx={{ color: '#666' }}>
{metadata.sheetCount} sheet{metadata.sheetCount !== 1 ? 's' : ''}
{' • '}
{metadata.fileSize}
{sheetDimensions && (
<>
{' • '}
{sheetDimensions.rows} row{sheetDimensions.rows !== 1 ? 's' : ''} × {sheetDimensions.cols} column{sheetDimensions.cols !== 1 ? 's' : ''}
</>
)}
</Typography>
</Box>
)}
{/* Sheet Tabs */}
{metadata && metadata.sheetCount > 1 && (
<Box sx={{ borderBottom: 1, borderColor: 'divider', backgroundColor: '#fff' }}>
<Tabs
value={activeSheet}
onChange={(e, newValue) => setActiveSheet(newValue)}
variant="scrollable"
scrollButtons="auto"
>
{metadata.sheetNames.map((name, idx) => (
<Tab key={idx} label={name} />
))}
</Tabs>
</Box>
)}
{/* Table Container */}
<Box
ref={containerRef}
sx={{
flex: 1,
overflow: 'hidden',
position: 'relative',
backgroundColor: '#fff',
fontFamily: 'Roboto, sans-serif',
'& *': {
fontFamily: 'Roboto, sans-serif !important',
},
}}
/>
</Box>
);
}