Skip to content

Commit 707ef04

Browse files
Joshua LeviJoshua Levi
authored andcommitted
add progress bar to write
1 parent c7842d1 commit 707ef04

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/taskpane/components/App.tsx

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ const App = () => {
163163
const textAreaId = useId("textarea");
164164
const [isWriteLoading, setIsWriteLoading] = useState(false);
165165
const [writeErrorMessage, setWriteErrorMessage] = useState<string>();
166+
const [writeProcessedRows, setWriteProcessedRows] = useState(0);
167+
const [writeTotalRows, setWriteTotalRows] = useState(0);
168+
const writeSheetPercent = writeTotalRows > 0 ? ((writeProcessedRows / writeTotalRows) * 100).toFixed(2) : 0;
166169

167170
const incrementProcessedRows = () => {
168171
setProcessedRows((state) => state + 1);
@@ -460,6 +463,18 @@ const App = () => {
460463
return columnName;
461464
}
462465
const writeWorkbook = async (jsonData: any) => {
466+
setWriteProcessedRows(0);
467+
let totalRows = 0;
468+
jsonData.worksheets.forEach(sheet => {
469+
// Count max row index in each worksheet
470+
let maxRow = 0;
471+
Object.values(sheet.cells).forEach((cell: any) => {
472+
maxRow = Math.max(maxRow, cell.rowIndex);
473+
});
474+
totalRows += maxRow + 1;
475+
});
476+
setWriteTotalRows(totalRows);
477+
let writtenRows = 0;
463478
await Excel.run(async (context) => {
464479
try {
465480
const ROW_CHUNK_SIZE = 2; // Adjust based on performance needs
@@ -479,7 +494,6 @@ const App = () => {
479494

480495
context.application.suspendScreenUpdatingUntilNextSync();
481496

482-
483497
let sheet_cells = worksheet.getRange(sheetData.cells[Object.keys(sheetData.cells)[0]].address + ":" + sheetData.cells[Object.keys(sheetData.cells)[Object.keys(sheetData.cells).length - 1]].address);
484498
const { properties, formulas, numberFormats } = parseExcelJsonTo2DArrays(sheetData.cells);
485499

@@ -490,6 +504,12 @@ const App = () => {
490504
});
491505
sheet_cells.setCellProperties(properties);
492506

507+
// Simulate row-wise progress update
508+
for (let row = 0; row < formulas.length; row++) {
509+
writtenRows++;
510+
setWriteProcessedRows(writtenRows);
511+
}
512+
493513
sheet_cells.untrack();
494514
await context.sync();
495515
}
@@ -656,6 +676,15 @@ const App = () => {
656676
</Button>
657677
</div>
658678

679+
{isWriteLoading && (
680+
<div className={styles.progressContainer}>
681+
<Text className={styles.progressText}>
682+
Writing Row {writeProcessedRows} of {writeTotalRows} ({writeSheetPercent}%)
683+
</Text>
684+
<ProgressBar value={Number(writeSheetPercent) / 100} />
685+
</div>
686+
)}
687+
659688
{writeErrorMessage && (
660689
<Text className={styles.errorMessage}>{writeErrorMessage}</Text>
661690
)}

0 commit comments

Comments
 (0)