renebigot / XlsxReaderWriter

XlsxReaderWriter is an Objective-C library for iPhone / iPad and Mac. It parses and writes MS Excel XLSX files.
MIT License
438 stars 121 forks source link

stringValue() crashes on formula cell pointing to string cell #57

Open marco-m-alves opened 8 years ago

marco-m-alves commented 8 years ago

I'm trying to print out all the cells string values of one worksheet using:

if let worksheets = imported.worksheets {
                    if let firstWS = worksheets.first {
                        let cells = firstWS.cells.flatMap { $0 as? BRACell }
                        cells.forEach { cell in
                            print("\(cell.reference) \(cell.type)")
                            print("\(cell.stringValue())")
                        }
                    }
                }

But I get the following error/crash on I7 after a few cells in:

Optional("I7") BRACellContentType(rawValue: 6)
2016-10-23 09:12:30.129381 SwiftCell[37351:15115644] -[__NSDictionaryI length]: unrecognized selector sent to instance 0x600000e665c0
2016-10-23 09:12:30.130061 SwiftCell[37351:15115644] [General] <private>
2016-10-23 09:12:30.139817 SwiftCell[37351:15115644] [General] <private>

To uncover what might be behind the error, I tried to print the v property of the dictionaryRepresentation by using:

print("\(cell.dictionaryRepresentation[AnyHashable("v")])") // instead of printing the string value

When I do that, it no longer crashes and I get the following printout for the same I7 cell reference where it previous crashed when printing out the string value:

Optional("I7") BRACellContentType(rawValue: 6)
Optional({
    "__text" = "Vendas e servic\U0327os prestados ";
    "_xml:space" = preserve;
})

Additional context on I7 cell:

I7 is formula cell and the formula is "=A7"
A7 has the string "Vendas e serviços prestados".
renebigot commented 7 years ago

Can't have the same problem here with your code. Are you using the latest version from git (cocoapods is not up to date) ?

Note that reading the string value of cells with formula is never a good idea since these values are never updated.