Skip to content

Commit d31c9f7

Browse files
Or-MiOr-Mi
authored andcommitted
change write function to load a file
1 parent eb1a637 commit d31c9f7

File tree

1 file changed

+15
-78
lines changed

1 file changed

+15
-78
lines changed

src/taskpane/components/App.tsx

Lines changed: 15 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -355,12 +355,6 @@ const App = () => {
355355
throw new Error(`HTTP error! status: ${response.status}`);
356356
}
357357

358-
// // Handle compressed response
359-
// const responseBuffer = await response.arrayBuffer();
360-
// const responseUint8Array = new Uint8Array(responseBuffer);
361-
// const responseString = responseUint8Array.toString();
362-
// // const decompressedResponse = pako.ungzip(responseUint8Array, { to: 'string' });
363-
364358
// return JSON.parse(responseString);
365359
return await response.json();
366360
} catch (error) {
@@ -375,64 +369,6 @@ const App = () => {
375369
}
376370
};
377371

378-
// const convertTo2DArray = async function (cellDict) {
379-
// // Helper function to parse row and column from address (e.g., "Summary!A3" -> row 3, col A)
380-
// function parseAddress(address) {
381-
// const match = address.match(/!([A-Z]+)(\d+)/);
382-
// if (!match) return { row: 0, col: 0 };
383-
// const colLetter = match[1];
384-
// const row = parseInt(match[2], 10) - 1; // Convert to 0-based index
385-
// // Convert column letter to 0-based index (A=0, B=1, etc.)
386-
// let col = 0;
387-
// for (let i = 0; i < colLetter.length; i++) {
388-
// col = col * 26 + (colLetter.charCodeAt(i) - 65 + 1);
389-
// }
390-
// return { row, col: col - 1 }; // Adjust to 0-based
391-
// }
392-
393-
// // Find maximum row and column indices
394-
// let maxRow = 0;
395-
// let maxCol = 0;
396-
// for (const cell of Object.values(cellDict)) {
397-
// const { row, col } = parseAddress(cell.address);
398-
// maxRow = Math.max(maxRow, row);
399-
// maxCol = Math.max(maxCol, col);
400-
// }
401-
402-
// // Initialize three 2D arrays
403-
// const properties = Array(maxRow + 1).fill().map(() => Array(maxCol + 1).fill(undefined));
404-
// const formulas = Array(maxRow + 1).fill().map(() => Array(maxCol + 1).fill(undefined));
405-
// const numberFormats = Array(maxRow + 1).fill().map(() => Array(maxCol + 1).fill(undefined));
406-
407-
// // Populate the arrays
408-
// for (const cell of Object.values(cellDict)) {
409-
// const { row, col } = parseAddress(cell.address);
410-
// // Properties array
411-
// properties[row][col] = {
412-
// format: {
413-
// fill: {
414-
// color: cell.format.backgroundColor
415-
// },
416-
// font: {
417-
// bold: cell.format.font.bold,
418-
// color: cell.format.font.color,
419-
// italic: cell.format.font.italic,
420-
// name: cell.format.font.name,
421-
// size: cell.format.font.size,
422-
// strikethrough: cell.format.font.strikethrough || false, // Default to false if undefined
423-
// underline: cell.format.font.underline
424-
// }
425-
// }
426-
// };
427-
// // Formulas array
428-
// formulas[row][col] = cell.formulaR1C1;
429-
// // Number formats array
430-
// numberFormats[row][col] = cell.format.numberFormat;
431-
// }
432-
433-
// return { properties, formulas, numberFormats };
434-
// }
435-
436372
function parseExcelJsonTo2DArrays(jsonData) {
437373
// Find the maximum row and column indices
438374
let maxRow = 0;
@@ -537,7 +473,7 @@ function parseExcelJsonTo2DArrays(jsonData) {
537473
formulasR1C1: formulas,
538474
numberFormat: numberFormats
539475
});
540-
sheet_cells.setCellProperties(properties);
476+
// sheet_cells.setCellProperties(properties);
541477
sheet_cells.untrack();
542478

543479
await context.sync();
@@ -556,8 +492,10 @@ function parseExcelJsonTo2DArrays(jsonData) {
556492

557493
try {
558494
const formData = new FormData(e.currentTarget);
559-
const textArea = formData.get("textArea");
560-
const jsonData = JSON.parse(textArea as string);
495+
const file = formData.get("jsonFile") as File;
496+
if (!file) throw new Error("No file selected");
497+
const text = await file.text();
498+
const jsonData = JSON.parse(text);
561499
await writeWorkbook(jsonData);
562500
} catch (error) {
563501
// Compose a detailed error message with stack trace
@@ -672,17 +610,16 @@ function parseExcelJsonTo2DArrays(jsonData) {
672610
<form className={styles.form} onSubmit={handleSubmitWrite}>
673611
<div className={styles.formContent}>
674612
<div className={styles.inputWrapper}>
675-
<Label className={styles.label} htmlFor={textAreaId}>JSON Data</Label>
676-
<Textarea
677-
resize="vertical"
678-
required
679-
id={textAreaId}
680-
name="textArea"
681-
placeholder="Paste your JSON data here..."
682-
style={{ minHeight: "200px" }}
683-
className={styles.input}
684-
/>
685-
</div>
613+
<Label className={styles.label} htmlFor={textAreaId}>JSON Data File</Label>
614+
<input
615+
type="file"
616+
accept=".json,application/json"
617+
id={textAreaId}
618+
name="jsonFile"
619+
required
620+
className={styles.input}
621+
/>
622+
</div>
686623

687624
<div className={styles.buttonContainer}>
688625
<Button

0 commit comments

Comments
 (0)