From 8084a32ef40e54a0f38c6bac67246d5f8deb9458 Mon Sep 17 00:00:00 2001 From: Heinrich Lukas Weil Date: Mon, 12 Dec 2022 10:11:57 +0100 Subject: [PATCH] allow for trailing spaces to be preserved when writing strings --- src/FsSpreadsheet.ExcelIO/Cell.fs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/FsSpreadsheet.ExcelIO/Cell.fs b/src/FsSpreadsheet.ExcelIO/Cell.fs index 94fc3f4f..9abbd4d2 100644 --- a/src/FsSpreadsheet.ExcelIO/Cell.fs +++ b/src/FsSpreadsheet.ExcelIO/Cell.fs @@ -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) = @@ -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) = @@ -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)