@@ -36,18 +36,6 @@ module Cell =
36
36
/// </summary>
37
37
let setValue ( value : string ) ( cellValue : CellValue ) = cellValue.Text <- value
38
38
39
- /// <summary>
40
- /// Takes a DataType and returns the appropriate CellValue.
41
- /// </summary>
42
- /// <remarks>DataType is the FsSpreadsheet representation of the CellValue enum in OpenXml.</remarks>
43
- let cellValuesFromDataType ( dataType : DataType ) =
44
- match dataType with
45
- | String -> CellValues.String
46
- | Boolean -> CellValues.Boolean
47
- | Number -> CellValues.Number
48
- | Date -> CellValues.Date
49
- | Empty -> CellValues.Error
50
-
51
39
/// <summary>
52
40
/// Takes a CellValue and returns the appropriate DataType.
53
41
/// </summary>
@@ -94,8 +82,20 @@ module Cell =
94
82
/// <summary>
95
83
/// Creates a Cell from a CellValues type case, a "A1" style reference, and a CellValue containing the value string.
96
84
/// </summary>
97
- let create ( dataType : CellValues ) ( reference : string ) ( value : CellValue ) =
98
- Cell( CellReference = StringValue.FromString reference, DataType = EnumValue( dataType), CellValue = value)
85
+ let create ( dataType : CellValues option ) ( reference : string ) ( value : CellValue ) =
86
+ match dataType with
87
+ | Some dataType -> Cell( CellReference = StringValue.FromString reference, DataType = EnumValue( dataType), CellValue = value)
88
+ | None -> Cell( CellReference = StringValue.FromString reference, CellValue = value)
89
+
90
+ /// <summary>
91
+ /// Creates a Cell from a CellValues type case, a "A1" style reference, and a CellValue containing the value string.
92
+ /// </summary>
93
+ let createWithFormat doc ( dataType : CellValues option ) ( reference : string ) ( cellFormat : CellFormat ) ( value : CellValue ) =
94
+ let styleSheet = Stylesheet.getOrInit doc
95
+ let i = Stylesheet.CellFormat.appendOrGetIndex cellFormat styleSheet
96
+ match dataType with
97
+ | Some dataType -> Cell( StyleIndex = UInt32Value( uint32 i), CellReference = StringValue.FromString reference, DataType = EnumValue( dataType), CellValue = value)
98
+ | None -> Cell( StyleIndex = UInt32Value( uint32 i), CellReference = StringValue.FromString reference, CellValue = value)
99
99
100
100
/// <summary>
101
101
/// Sets the preserve attribute of a Cell.
@@ -118,7 +118,7 @@ module Cell =
118
118
i
119
119
|> string
120
120
|> CellValue.create
121
- |> create CellValues.SharedString reference
121
+ |> create ( Some CellValues.SharedString) reference
122
122
| None ->
123
123
let updatedSharedStringTable =
124
124
sharedStringTable
@@ -128,7 +128,7 @@ module Cell =
128
128
|> SharedStringTable.count
129
129
|> string
130
130
|> CellValue.create
131
- |> create CellValues.SharedString reference
131
+ |> create ( Some CellValues.SharedString) reference
132
132
|> fun c ->
133
133
if s.EndsWith " " then
134
134
setSpacePreserveAttribute c
@@ -137,26 +137,21 @@ module Cell =
137
137
| _ ->
138
138
let valType , value = inferCellValue value
139
139
let reference = CellReference.ofIndices columnIndex ( rowIndex)
140
- create valType reference ( CellValue.create value)
140
+ create ( Some valType) reference ( CellValue.create value)
141
141
|> fun c ->
142
142
if value.EndsWith " " then
143
143
setSpacePreserveAttribute c
144
144
else c
145
145
146
- /// <summary>
147
- /// Create a cell using a shared string table, also returns the updated shared string table.
148
- /// </summary>
149
- let fromValueWithDataType ( sharedStringTable : SharedStringTable Option ) columnIndex rowIndex ( value : string ) ( dataType : DataType ) =
146
+ let getCellContent ( doc : Packaging.SpreadsheetDocument ) ( value : string ) ( dataType : DataType ) =
147
+ let sharedStringTable = SharedStringTable.tryGet doc
150
148
match dataType with
151
149
| DataType.String when sharedStringTable.IsSome->
152
150
let sharedStringTable = sharedStringTable.Value
153
- let reference = CellReference.ofIndices columnIndex ( rowIndex)
154
151
match SharedStringTable.tryGetIndexByString value sharedStringTable with
155
152
| Some i ->
156
153
i
157
154
|> string
158
- |> CellValue.create
159
- |> create CellValues.SharedString reference
160
155
| None ->
161
156
let updatedSharedStringTable =
162
157
sharedStringTable
@@ -165,22 +160,38 @@ module Cell =
165
160
updatedSharedStringTable
166
161
|> SharedStringTable.count
167
162
|> string
168
- |> CellValue.create
169
- |> create CellValues.SharedString reference
170
- |> fun c ->
171
- if value.EndsWith " " then
172
- setSpacePreserveAttribute c
173
- else c
163
+ |> fun v -> {| DataType = Some CellValues.SharedString; Value = v; Format = None|}
164
+ | DataType.String ->
165
+ {| DataType = Some CellValues.String; Value = value; Format = None|}
166
+ | DataType.Boolean ->
167
+ {| DataType = Some CellValues.Boolean; Value = System.Boolean.Parse value |> FsCellAux.boolConverter; Format = None|}
168
+ | DataType.Number ->
169
+ {| DataType = Some CellValues.Number; Value = value; Format = None|}
170
+ | DataType.Date ->
171
+ //let cellFormat = CellFormat(NumberFormatId = UInt32Value 19u, ApplyNumberFormat = BooleanValue true)
172
+ let value = System.DateTime.Parse( value) .ToOADate() |> string
173
+ let cellFormat =
174
+ if value.Contains( " ." ) then
175
+ Stylesheet.CellFormat.getDefaultDateTime()
176
+ else
177
+ Stylesheet.CellFormat.getDefaultDate()
178
+ {| DataType = None; Value = value; Format = Some cellFormat|}
179
+ | DataType.Empty -> {| DataType = None; Value = value; Format = None|}
174
180
175
- | _ ->
176
- let valType = cellValuesFromDataType dataType
177
- let reference = CellReference.ofIndices columnIndex ( rowIndex)
178
- create valType reference ( CellValue.create value)
179
- |> fun c ->
181
+ /// <summary>
182
+ /// Create a cell using a shared string table, also returns the updated shared string table.
183
+ /// </summary>
184
+ let fromValueWithDataType ( doc : Packaging.SpreadsheetDocument ) columnIndex rowIndex ( value : string ) ( dataType : DataType ) =
185
+ let reference = CellReference.ofIndices columnIndex ( rowIndex)
186
+ let cellContent = getCellContent doc value dataType
187
+ if cellContent.Format.IsSome then
188
+ createWithFormat doc cellContent.DataType reference cellContent.Format.Value ( CellValue.create cellContent.Value)
189
+ else
190
+ create cellContent.DataType reference ( CellValue.create cellContent.Value)
191
+ |> fun c ->
180
192
if value.EndsWith " " then
181
193
setSpacePreserveAttribute c
182
194
else c
183
-
184
195
/// <summary>
185
196
/// Gets "A1"-style Cell reference.
186
197
/// </summary>
@@ -256,7 +267,6 @@ module Cell =
256
267
match cell |> tryGetType with
257
268
| Some ( CellValues.SharedString) when sharedStringTable.IsSome->
258
269
let sharedStringTable = sharedStringTable.Value
259
-
260
270
let sharedStringTableIndex =
261
271
cell
262
272
|> getCellValue
0 commit comments