Get and Set Style for cells with Golang via C++
Cell.GetStyle and Cell.SetStyle. This article examines the Cell.GetStyle/SetStyle approach to help you determine which technique best suits you.
Formatting Cells
There are two ways to format a cell, illustrated below.
Using GetStyle()
With the following piece of code, a Style object is instantiated for each cell when formatting it. If many cells are being formatted, a large amount of memory is consumed because the Style object is sizable. These Style objects are not freed until the Workbook.Save method is called.
C++
Using SetStyle()
The first approach is easy and straightforward, so why add a second approach?
We added the second approach to optimise memory usage. After using the Cell.GetStyle method to retrieve a Style object, modify it, and use the Cell.SetStyle method to set it back to the cell, the Style object is not preserved, and the C++ runtime will collect it when it is not referenced.
When calling the Cell.SetStyle method, the Style object isn’t saved for each cell. Instead, we compare this Style object to an internal Style object pool to see if it can be reused. Only Style objects that differ from the existing ones are kept for each Workbook object. This means that there are only a few hundred Style objects for each Excel file instead of thousands. For each cell, only an index to the Style object pool is preserved.
C++