Skip to content

Commit 1e60afe

Browse files
Parser (streamed): fix default value for cell type
According to specification if cell has no `t` attribute, it has a default value as `n` - number.
1 parent 210f0de commit 1e60afe

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

data/floats.xlsx

8.26 KB
Binary file not shown.

src/Codec/Xlsx/Parser/Stream.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ parseStyle list =
688688
parseType :: SheetValues -> Either TypeError ExcelValueType
689689
parseType list =
690690
case findName "t" list of
691-
Nothing -> pure Untyped
691+
Nothing -> Right TN -- according to spec by default if `t` is missing then default value is `n` - number
692692
Just (_nm, valText)->
693693
case valText of
694694
"n" -> Right TN

test/StreamTests.hs

+23
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ tests =
9090

9191
testGroup "Reader/inline strings"
9292
[ testCase "Can parse row with inline strings" inlineStringsAreParsed
93+
],
94+
95+
testGroup "Reader/floats parsing"
96+
[ testCase "Can parse untyped values as floats" untypedCellsAreParsedAsFloats
9397
]
9498
]
9599

@@ -205,4 +209,23 @@ inlineStringsAreParsed = do
205209
]
206210
expected @==? (items ^.. traversed . si_row . ri_cell_row)
207211

212+
untypedCellsAreParsedAsFloats :: IO ()
213+
untypedCellsAreParsedAsFloats = do
214+
-- values in that file are under `General` cell-type and are not marked
215+
-- as numbers explicitly in `t` attribute.
216+
items <- runXlsxM "data/floats.xlsx" $ collectItems $ makeIndex 1
217+
let defCell v = def
218+
{ _cellValue = Just v
219+
}
220+
expected =
221+
[ IM.fromList [ (1, def & cellValue ?~ CellDouble 12.0) ]
222+
, IM.fromList [ (1, def & cellValue ?~ CellDouble 13.0) ]
223+
-- cell below has explicit `Numeric` type, while others are all `General`,
224+
-- but sometimes excel does not add a `t="n"` attr even to numeric cells
225+
-- but it should be default as number in any cases if `t` is missing
226+
, IM.fromList [ (1, def & cellValue ?~ CellDouble 14.0 & cellStyle ?~ 1 ) ]
227+
, IM.fromList [ (1, def & cellValue ?~ CellDouble 15.0) ]
228+
]
229+
expected @==? (_ri_cell_row . _si_row <$> items)
230+
208231
#endif

0 commit comments

Comments
 (0)