Skip to content

Commit

Permalink
fix dataType of cells always being text
Browse files Browse the repository at this point in the history
  • Loading branch information
HLWeil committed May 10, 2022
1 parent 6d12edf commit 7250efd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/FsSpreadsheet.ExcelIO/Cell.fs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ module Cell =
/// Sets the value inside the CellValue.
let setValue (value : string) (cellValue : CellValue) = cellValue.Text <- value

let cellValuesFromDataType (dataType : DataType) =
match dataType with
| String -> CellValues.String
| Boolean -> CellValues.Boolean
| Number -> CellValues.Number
| Date -> CellValues.Date
| Empty -> CellValues.Error

/// Creates an empty cell.
let empty () = Cell()
Expand Down Expand Up @@ -82,6 +89,33 @@ module Cell =
let reference = CellReference.ofIndices columnIndex (rowIndex)
create valType reference (CellValue.create value)

/// Create a cell using a shared string table, also returns the updated shared string table.
let fromValueWithDataType (sharedStringTable : SharedStringTable Option) columnIndex rowIndex (value : string) (dataType : DataType) =
match dataType with
| DataType.String when sharedStringTable.IsSome->
let sharedStringTable = sharedStringTable.Value
let reference = CellReference.ofIndices columnIndex (rowIndex)
match SharedStringTable.tryGetIndexByString value sharedStringTable with
| Some i ->
i
|> string
|> CellValue.create
|> create CellValues.SharedString reference
| None ->
let updatedSharedStringTable =
sharedStringTable
|> SharedStringTable.SharedStringItem.add (SharedStringTable.SharedStringItem.create value)

updatedSharedStringTable
|> SharedStringTable.count
|> string
|> CellValue.create
|> create CellValues.SharedString reference
| _ ->
let valType = cellValuesFromDataType dataType
let reference = CellReference.ofIndices columnIndex (rowIndex)
create valType reference (CellValue.create value)

/// Gets "A1"-style cell reference.
let getReference (cell : Cell) = cell.CellReference.Value

Expand Down
2 changes: 1 addition & 1 deletion src/FsSpreadsheet.ExcelIO/FsExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module FsExtensions =
let cells =
cells
|> List.map (fun cell ->
Cell.fromValue None (uint32 cell.WorksheetColumn) (uint32 cell.WorksheetRow) (cell.Value)
Cell.fromValueWithDataType None (uint32 cell.WorksheetColumn) (uint32 cell.WorksheetRow) (cell.Value) (cell.DataType)
)
let row = Row.create (uint32 row.Index) (Row.Spans.fromBoundaries min max) cells
SheetData.appendRow row sd |> ignore
Expand Down

0 comments on commit 7250efd

Please sign in to comment.