Skip to content

Commit 92a6336

Browse files
Or-MiOr-Mi
authored andcommitted
fixed writeWorkbook
1 parent 707ef04 commit 92a6336

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

src/taskpane/components/App.tsx

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,8 @@ const App = () => {
280280
cells: {},
281281
};
282282

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
285285
usedRange.untrack();
286286
lastColumn.untrack();
287287
lastRow.untrack();
@@ -452,16 +452,7 @@ const App = () => {
452452
numberFormats
453453
};
454454
}
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+
465456
const writeWorkbook = async (jsonData: any) => {
466457
setWriteProcessedRows(0);
467458
let totalRows = 0;
@@ -476,12 +467,14 @@ const App = () => {
476467
setWriteTotalRows(totalRows);
477468
let writtenRows = 0;
478469
await Excel.run(async (context) => {
470+
const worksheets = context.workbook.worksheets;
479471
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;
483476

484-
// Loop through each worksheet in the JSON data
477+
// create worksheets
485478
for (const sheetData of jsonData.worksheets) {
486479
let worksheet = worksheets.getItemOrNullObject(sheetData.name);
487480
await context.sync();
@@ -490,7 +483,17 @@ const App = () => {
490483
if (worksheet.isNullObject) {
491484
worksheet = worksheets.add(sheetData.name);
492485
worksheet.activate();
486+
} else {
487+
// Clear existing content in the worksheet
488+
worksheet.getUsedRange().clear(Excel.ClearApplyTo.contents);
493489
}
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();
494497

495498
context.application.suspendScreenUpdatingUntilNextSync();
496499

@@ -504,15 +507,18 @@ const App = () => {
504507
});
505508
sheet_cells.setCellProperties(properties);
506509

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);
512512

513513
sheet_cells.untrack();
514514
await context.sync();
515+
// setWriteProcessedRows(sheetData.cells[Object.keys(sheetData.cells)[Object.keys(sheetData.cells).length - 1]].rowIndex + 1);
515516
}
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+
516522
} catch (error) {
517523
setWriteErrorMessage(error.message);
518524
throw error;

0 commit comments

Comments
 (0)