diff --git a/src/FsSpreadsheet.ExcelIO/Cell.fs b/src/FsSpreadsheet.ExcelIO/Cell.fs index 858d0939..94fc3f4f 100644 --- a/src/FsSpreadsheet.ExcelIO/Cell.fs +++ b/src/FsSpreadsheet.ExcelIO/Cell.fs @@ -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() @@ -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 diff --git a/src/FsSpreadsheet.ExcelIO/FsExtensions.fs b/src/FsSpreadsheet.ExcelIO/FsExtensions.fs index aab47d13..0977bfee 100644 --- a/src/FsSpreadsheet.ExcelIO/FsExtensions.fs +++ b/src/FsSpreadsheet.ExcelIO/FsExtensions.fs @@ -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