Skip to content

Commit

Permalink
Parse correct DataType from OpenXML #73
Browse files Browse the repository at this point in the history
  • Loading branch information
Freymaurer committed Oct 13, 2023
1 parent 1ad6c9d commit f468fd0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
1 change: 0 additions & 1 deletion src/FsSpreadsheet.ExcelIO/Cell.fs
Expand Up @@ -256,7 +256,6 @@ module Cell =
match cell |> tryGetType with
| Some (CellValues.SharedString) when sharedStringTable.IsSome->
let sharedStringTable = sharedStringTable.Value

let sharedStringTableIndex =
cell
|> getCellValue
Expand Down
31 changes: 27 additions & 4 deletions src/FsSpreadsheet.ExcelIO/FsExtensions.fs
Expand Up @@ -39,14 +39,22 @@ module FsExtensions =
/// Creates an FsCell on the basis of an XlsxCell. Uses a SharedStringTable if present to get the XlsxCell's value.
/// </summary>
static member ofXlsxCell (sst : SharedStringTable option) (xlsxCell : Cell) =
let v = Cell.getValue sst xlsxCell
let mutable v = Cell.getValue sst xlsxCell
let col, row = xlsxCell.CellReference.Value |> CellReference.toIndices
let dt =
try DataType.ofXlsxCellValues xlsxCell.DataType.Value
with _ -> DataType.Empty
with _ -> DataType.Number // default is number
match dt with
| Date ->
try
// datetime is written as float counting days since 1900.
// We use the .NET helper because we really do not want to deal with datetime issues.
v <- System.DateTime.FromOADate(float v).ToString()
with
| _ -> ()
| _ -> ()
FsCell.createWithDataType dt (int row) (int col) v


type FsTable with

/// <summary>
Expand Down Expand Up @@ -183,7 +191,22 @@ module FsExtensions =
let sheetId = Sheet.getID xlsxSheet
let xlsxCells =
Spreadsheet.getCellsBySheetID sheetId doc
|> Seq.map (FsCell.ofXlsxCell sst)
|> Seq.map (fun c ->
//https://stackoverflow.com/a/13178043/12858021
//https://stackoverflow.com/a/55425719/12858021
// check if styleindex exists
if c.StyleIndex <> null then
try
// get cellformat from stylesheet
let cellFormat : CellFormat = xlsxWorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats.ChildElements.GetItem (int c.StyleIndex.InnerText) :?> CellFormat
if cellFormat <> null then
// if numberformatid is between 14 and 18 it is date time.
if cellFormat.NumberFormatId >= UInt32Value(uint32 14) && cellFormat.NumberFormatId <= UInt32Value(uint32 18) then
c.DataType <- CellValues.Date
with
| _ -> ()
FsCell.ofXlsxCell sst c
)
let assocXlsxTables =
xlsxTables
|> Seq.tryPick (fun (sid,ts) -> if sid = sheetId then Some ts else None)
Expand Down
17 changes: 17 additions & 0 deletions src/FsSpreadsheet/Cells/FsCell.fs
Expand Up @@ -385,3 +385,20 @@ type FsCell (value : IConvertible, ?dataType : DataType, ?address : FsAddress) =
static member setValueAs<'T> value (cell : FsCell)=
cell.SetValueAs<'T>(value)
cell

member this.StructurallyEquals (other: FsCell) =
[|
this.Value = other.Value
this.DataType = other.DataType
[|
this.Address.Address = other.Address.Address
this.Address.ColumnNumber = other.Address.ColumnNumber
this.Address.RowNumber = other.Address.RowNumber
this.Address.FixedColumn = other.Address.FixedColumn
this.Address.FixedRow = other.Address.FixedRow
|]
|> Seq.forall (fun x -> x=true)
this.ColumnNumber = other.ColumnNumber
this.RowNumber = other.RowNumber
|]
|> Seq.forall (fun x -> x=true)

0 comments on commit f468fd0

Please sign in to comment.