Skip to content

Commit 6cd78ad

Browse files
claudiamurialdoclaudiamurialdo
andcommitted
Defer AutoFit execution to Save() for improved performance (#1251)
* Issue:207750 Defer AutoFit execution to Save() for improved performance * Add missing using --------- Co-authored-by: claudiamurialdo <c.murialdo@globant.com>
1 parent 0bae406 commit 6cd78ad

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

dotnet/src/dotnetframework/GxExcel/GxExcelEPPlus.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34
using GeneXus.Office.Excel;
45
using OfficeOpenXml;
@@ -104,6 +105,7 @@ public short Save()
104105
{
105106
using (var stream = new MemoryStream())
106107
{
108+
ApplyPendingAutoFit();
107109
p.SaveAs(stream);
108110
p.Dispose();
109111
GxFile file = new GxFile(Path.GetDirectoryName(xlsFileName), xlsFileName, GxFileType.Private);
@@ -328,6 +330,22 @@ public short AutoFit
328330
private String errDescription = "OK";
329331
private short readOnly = 0;
330332
private bool autoFit = false;
333+
private HashSet<(ExcelWorksheet sheet, int col)> _pendingAutoFitColumns = new HashSet<(ExcelWorksheet, int)>();
334+
335+
internal void MarkColumnForAutoFit(ExcelWorksheet sheet, int column)
336+
{
337+
_pendingAutoFitColumns.Add((sheet, column));
338+
}
339+
340+
private void ApplyPendingAutoFit()
341+
{
342+
foreach (var (sheet, col) in _pendingAutoFitColumns)
343+
{
344+
sheet.Column(col).AutoFit();
345+
}
346+
_pendingAutoFitColumns.Clear();
347+
}
348+
331349

332350
public short UnBind() { return -1; }
333351
public short Hide() { return -1; }
@@ -593,9 +611,8 @@ public string Text
593611
}
594612
else
595613
pCells[i].Value = value;
596-
597-
fitColumnWidth();
598614
}
615+
fitColumnWidth();
599616
}
600617
catch (Exception e)
601618
{
@@ -631,7 +648,7 @@ private void fitColumnWidth()
631648
{
632649
for (int columnIndex = pCellsRange.Start.Column; columnIndex <= pCellsRange.End.Column; columnIndex++)
633650
{
634-
pSelectedSheet.Column(columnIndex).AutoFit();
651+
doc.MarkColumnForAutoFit(pSelectedSheet, columnIndex);
635652
}
636653
}
637654
}

0 commit comments

Comments
 (0)