Skip to content

Commit

Permalink
allow for trailing spaces to be preserved when writing strings
Browse files Browse the repository at this point in the history
  • Loading branch information
HLWeil committed Dec 12, 2022
1 parent 4a2318e commit 8084a32
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/FsSpreadsheet.ExcelIO/Cell.fs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ module Cell =
/// Creates a cell from a CellValues type case, a "A1" style reference, and a CellValue containing the value string.
let create (dataType : CellValues) (reference : string) (value : CellValue) =
Cell(CellReference = StringValue.FromString reference, DataType = EnumValue(dataType), CellValue = value)


let setSpacePreserveAttribute (c : Cell) =
c.SetAttribute(OpenXmlAttribute("xml:space","","preserve"))
c

/// Create a cell using a shared string table, also returns the updated shared string table.
let fromValue (sharedStringTable : SharedStringTable Option) columnIndex rowIndex (value : 'T) =
Expand All @@ -84,10 +87,19 @@ module Cell =
|> string
|> CellValue.create
|> create CellValues.SharedString reference
|> fun c ->
if s.EndsWith " " then
setSpacePreserveAttribute c
else c

| _ ->
let valType,value = inferCellValue value
let reference = CellReference.ofIndices columnIndex (rowIndex)
create valType reference (CellValue.create value)
let valType,value = inferCellValue value
let reference = CellReference.ofIndices columnIndex (rowIndex)
create valType reference (CellValue.create value)
|> fun c ->
if value.EndsWith " " then
setSpacePreserveAttribute c
else c

/// 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) =
Expand All @@ -111,6 +123,11 @@ module Cell =
|> string
|> CellValue.create
|> create CellValues.SharedString reference
|> fun c ->
if value.EndsWith " " then
setSpacePreserveAttribute c
else c

| _ ->
let valType = cellValuesFromDataType dataType
let reference = CellReference.ofIndices columnIndex (rowIndex)
Expand Down

0 comments on commit 8084a32

Please sign in to comment.