@@ -280,8 +280,8 @@ const App = () => {
280
280
cells : { } ,
281
281
} ;
282
282
283
- var rowsCount = lastRow . rowIndex ;
284
- var columnsCount = lastColumn . columnIndex ;
283
+ var rowsCount = lastRow . rowIndex + 1 ; // zero-based index, so add 1
284
+ var columnsCount = lastColumn . columnIndex + 1 ; // zero-based index, so add 1
285
285
usedRange . untrack ( ) ;
286
286
lastColumn . untrack ( ) ;
287
287
lastRow . untrack ( ) ;
@@ -452,16 +452,7 @@ const App = () => {
452
452
numberFormats
453
453
} ;
454
454
}
455
- // Helper function to convert column number to Excel column letter
456
- function getExcelColumn ( colNum : number ) : string {
457
- let columnName = '' ;
458
- while ( colNum > 0 ) {
459
- const remainder = ( colNum - 1 ) % 26 ;
460
- columnName = String . fromCharCode ( 65 + remainder ) + columnName ;
461
- colNum = Math . floor ( ( colNum - 1 ) / 26 ) ;
462
- }
463
- return columnName ;
464
- }
455
+
465
456
const writeWorkbook = async ( jsonData : any ) => {
466
457
setWriteProcessedRows ( 0 ) ;
467
458
let totalRows = 0 ;
@@ -476,12 +467,14 @@ const App = () => {
476
467
setWriteTotalRows ( totalRows ) ;
477
468
let writtenRows = 0 ;
478
469
await Excel . run ( async ( context ) => {
470
+ const worksheets = context . workbook . worksheets ;
479
471
try {
480
- const ROW_CHUNK_SIZE = 2 ; // Adjust based on performance needs
481
- const COL_CHUNK_SIZE = 50 ; // Adjust based on performance needs
482
- const worksheets = context . workbook . worksheets ;
472
+ await context . sync ( ) ;
473
+
474
+ // Suspend automatic calculation
475
+ context . workbook . application . calculationMode = Excel . CalculationMode . manual ;
483
476
484
- // Loop through each worksheet in the JSON data
477
+ // create worksheets
485
478
for ( const sheetData of jsonData . worksheets ) {
486
479
let worksheet = worksheets . getItemOrNullObject ( sheetData . name ) ;
487
480
await context . sync ( ) ;
@@ -490,7 +483,17 @@ const App = () => {
490
483
if ( worksheet . isNullObject ) {
491
484
worksheet = worksheets . add ( sheetData . name ) ;
492
485
worksheet . activate ( ) ;
486
+ } else {
487
+ // Clear existing content in the worksheet
488
+ worksheet . getUsedRange ( ) . clear ( Excel . ClearApplyTo . contents ) ;
493
489
}
490
+ }
491
+
492
+
493
+ // Loop through each worksheet in the JSON data
494
+ for ( const sheetData of jsonData . worksheets ) {
495
+ let worksheet = worksheets . getItemOrNullObject ( sheetData . name ) ;
496
+ await context . sync ( ) ;
494
497
495
498
context . application . suspendScreenUpdatingUntilNextSync ( ) ;
496
499
@@ -504,15 +507,18 @@ const App = () => {
504
507
} ) ;
505
508
sheet_cells . setCellProperties ( properties ) ;
506
509
507
- // Simulate row-wise progress update
508
- for ( let row = 0 ; row < formulas . length ; row ++ ) {
509
- writtenRows ++ ;
510
- setWriteProcessedRows ( writtenRows ) ;
511
- }
510
+ writtenRows += sheetData . cells [ Object . keys ( sheetData . cells ) [ Object . keys ( sheetData . cells ) . length - 1 ] ] . rowIndex + 1
511
+ setWriteProcessedRows ( writtenRows ) ;
512
512
513
513
sheet_cells . untrack ( ) ;
514
514
await context . sync ( ) ;
515
+ // setWriteProcessedRows(sheetData.cells[Object.keys(sheetData.cells)[Object.keys(sheetData.cells).length - 1]].rowIndex + 1);
515
516
}
517
+
518
+ // Rebuild the calculation engine to ensure all formulas are recalculated
519
+ context . workbook . application . calculate ( Excel . CalculationType . fullRebuild ) ;
520
+ await context . sync ( ) ;
521
+
516
522
} catch ( error ) {
517
523
setWriteErrorMessage ( error . message ) ;
518
524
throw error ;
0 commit comments