qax-os / excelize

Go language library for reading and writing Microsoft Excel™ (XLAM / XLSM / XLSX / XLTM / XLTX) spreadsheets
https://xuri.me/excelize
BSD 3-Clause "New" or "Revised" License
18.43k stars 1.73k forks source link

Why do we need checkRow function? #2017

Open huantt opened 4 weeks ago

huantt commented 4 weeks ago

Description

This function consumes too much memory, and I don't see the benefit of filling gap columns for rows.

Let's examine the GetColumnCell function:

for colIdx := range rowData.C {
            colData := &rowData.C[colIdx]
            if cell != colData.R {
                continue
            }
            val, ok, err := fn(ws, colData)
            if err != nil {
                return "", err
            }
            if ok {
                return val, nil
            }
        }

You are looping through the columns and checking the cell name to return a value. Therefore, what is the purpose of filling gap columns here?

huantt commented 4 weeks ago
image

Oh, we will encounter a problem when writing to the cell. The prepareCell function will return the wrong cell. Can we update the logic of the prepareCell function to use a loop to find the cell instead of accessing it directly by index?

xuri commented 4 weeks ago

The library pre-allocates some consecutive cells for set cell value randomly. Please reference the documentation to using stream mode API (stream writer for generate worksheet, and rows iterator for reading worksheet) to reduce memory usage.

huantt commented 4 weeks ago

I tried using a Stream Reader (rows) and StreamW riter, but since the Stream Reader only returns an array of string values (without cell style), I cannot use them to maintain the style when writing.

xuri commented 4 weeks ago

There are no plan to change for the prepareCell recently, I dont think that use a loop to find the cell instead of accessing it directly by index can be reduce memory usage without much more time cost, if you'd like to create a pull request for this to improvement performance, that's would be great.