Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Row.cells does not include empty cells. #107

Open
matzsoft opened this issue Apr 23, 2020 · 14 comments
Open

Row.cells does not include empty cells. #107

matzsoft opened this issue Apr 23, 2020 · 14 comments
Labels
documentation Documentation improvements enhancement New feature or request

Comments

@matzsoft
Copy link

See the attached Package.swift, main.swift, and test.xlsx. The spreadsheet contains one worksheet with 3 rows and 3 columns. Yet when reading them Rows 1 and 3 show only 2 columns because ColumnB is empty. There should be 3 cells in each of the 3 rows.

If I am missing something in the documentation please let me know.
Thanks.

Archive.zip

@MaxDesiatov
Copy link
Collaborator

Hey @matzsoft, in the XLSX format there's a difference between an empty cell and absence of a cell, like a difference between "" and nil. If you look at the internals of this file, it doesn't contain these cells. In general this is more efficient, especially for sparse spreadsheets. If you had a spreadsheet with a single cell Z1000000, it wouldn't contain millions of empty cells and a single cell with a value, it would only store a single cell, which is great as sparse spreadsheets can be quickly saved, loaded and also take less storage.

Please also see a similar conversation here.

Does that resolve your issue?

@MaxDesiatov MaxDesiatov added the documentation Documentation improvements label Apr 23, 2020
@matzsoft
Copy link
Author

Hey Max, you are of course correct about the structure of the XML and why they chose to implement it that way. And if your intent is to have CoreXLSX reflect the structure of the XML rather than the spreadsheet that it abstracts, then that is the way the library should behave.

However I am more interested in the table structure of the spreadsheet and need those missing cells to be there in the data I'm extracting. So here is a simple suggested patch that would make it easier for me to do that.

In ColumnReference.swift at line 13, make intValue public. Then in my

for c in wsRow.cells {
I could more easily see which cells were not represented and treat them as empty.

Thanks.

@MaxDesiatov
Copy link
Collaborator

Sorry, after re-reading this I'm not sure I'm completely following how would you like it to work? Could you post a bigger code snippet of how you want the API to look like?

@matzsoft
Copy link
Author

Sure thing. Here's my file ExcelTable.swift after I made my suggested patch to ColumnReference.swift. After the patch I was able to create the extension for WorkSheet and add the two

row.append( contentsOf: Array( repeating: ...

and now it does what I need. Let me know if you have any other questions.
Thanks.
ExcelTable.swift.zip

@MaxDesiatov MaxDesiatov added the enhancement New feature or request label Apr 23, 2020
@jagan510710
Copy link

Hi

for (index, cell) in row.cells.enumerated() giving 6 cells in first row and 4 cells in second row as I have empty cells in second row. how can I get rows[1].cells.count = 6 (including empty rows).

@YangEhz
Copy link

YangEhz commented Mar 2, 2021

i have this problem too :( make me headache

@jagan510710
Copy link

i have this problem too :( make me headache

You found any solution?

@MaxDesiatov
Copy link
Collaborator

I would like to reiterate that an empty cell and absence of a cell are different things. If you're not getting a cell or a row during the iteration, this means that there is no such cell or row in your document. The document should have empty cells and rows written in it in the first place for you to be able to read them.

@jagan510710
Copy link

Because of absence of cell we have index problem. in first row I have email at index 3. in second row I have email at index 2.

@MaxDesiatov
Copy link
Collaborator

You should not address cells via their indices in an array of cells. Every cell has a reference property, which you can read to understand where exactly a given cell is located. Corresponding properties on the CellReference struct give you the exact position of a cell.

@jagan510710
Copy link

You should not address cells via their indices in an array of cells. Every cell has a reference property, which you can read to understand where exactly a given cell is located. Corresponding properties on the CellReference struct give you the exact position of a cell.

Thanks @MaxDesiatov it is working now...

@YangEhz
Copy link

YangEhz commented Mar 3, 2021

Sorry, I haven't figured it out yet, can I have a code reference?

@YangEhz
Copy link

YangEhz commented Mar 3, 2021

My method is like this
`func stringValue(_ sharedStrings: SharedStrings) -> String? {
guard type == .sharedString, let index = value.flatMap(Int.init) else {

        if value == nil {
            return ""
        }
        return value
        
    }
    
    return sharedStrings.items[index].text
}`

In CellQueries.swift -> public extension Cell, i added a line of code,But I don’t know if it will cause any bugs
P.S. apologize for my poor English...

@Ververtom
Copy link

You should not address cells via their indices in an array of cells. Every cell has a reference property, which you can read to understand where exactly a given cell is located. Corresponding properties on the CellReference struct give you the exact position of a cell.

Thanks @MaxDesiatov it is working now...

Hi, may I ask how did you solve the problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Documentation improvements enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants