Skip to content

Commit 62ff8ec

Browse files
authored
Merge pull request #76 from fslaborg/io_tests
Io tests
2 parents eedf32f + db263ed commit 62ff8ec

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1430
-366
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,4 @@ tmp/
362362
/tests/JS/FsSpreadsheet.Exceljs
363363
/tests/FsSpreadsheet.Exceljs.Tests/js
364364
/dist
365+
/tests/TestUtils/TestFiles/Scripts/fable

FsSpreadsheet.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "JS", "JS", "{ADCF7D08-F2EE-
4747
tests\JS\Exceljs.js = tests\JS\Exceljs.js
4848
EndProjectSection
4949
EndProject
50+
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "TestUtils", "tests\TestUtils\TestUtils.fsproj", "{60678E53-EDC4-4ADE-A9EE-B194BDC76B37}"
51+
EndProject
5052
Global
5153
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5254
Debug|Any CPU = Debug|Any CPU
@@ -177,6 +179,18 @@ Global
177179
{96E12F19-B25A-415E-B965-F9DE8D713C67}.Release|x64.Build.0 = Release|Any CPU
178180
{96E12F19-B25A-415E-B965-F9DE8D713C67}.Release|x86.ActiveCfg = Release|Any CPU
179181
{96E12F19-B25A-415E-B965-F9DE8D713C67}.Release|x86.Build.0 = Release|Any CPU
182+
{60678E53-EDC4-4ADE-A9EE-B194BDC76B37}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
183+
{60678E53-EDC4-4ADE-A9EE-B194BDC76B37}.Debug|Any CPU.Build.0 = Debug|Any CPU
184+
{60678E53-EDC4-4ADE-A9EE-B194BDC76B37}.Debug|x64.ActiveCfg = Debug|Any CPU
185+
{60678E53-EDC4-4ADE-A9EE-B194BDC76B37}.Debug|x64.Build.0 = Debug|Any CPU
186+
{60678E53-EDC4-4ADE-A9EE-B194BDC76B37}.Debug|x86.ActiveCfg = Debug|Any CPU
187+
{60678E53-EDC4-4ADE-A9EE-B194BDC76B37}.Debug|x86.Build.0 = Debug|Any CPU
188+
{60678E53-EDC4-4ADE-A9EE-B194BDC76B37}.Release|Any CPU.ActiveCfg = Release|Any CPU
189+
{60678E53-EDC4-4ADE-A9EE-B194BDC76B37}.Release|Any CPU.Build.0 = Release|Any CPU
190+
{60678E53-EDC4-4ADE-A9EE-B194BDC76B37}.Release|x64.ActiveCfg = Release|Any CPU
191+
{60678E53-EDC4-4ADE-A9EE-B194BDC76B37}.Release|x64.Build.0 = Release|Any CPU
192+
{60678E53-EDC4-4ADE-A9EE-B194BDC76B37}.Release|x86.ActiveCfg = Release|Any CPU
193+
{60678E53-EDC4-4ADE-A9EE-B194BDC76B37}.Release|x86.Build.0 = Release|Any CPU
180194
EndGlobalSection
181195
GlobalSection(SolutionProperties) = preSolution
182196
HideSolutionNode = FALSE
@@ -193,6 +207,7 @@ Global
193207
{E72A14FF-5026-463B-B0FA-2DA104D67B0C} = {1CF1274C-DE28-4446-9B4E-5884E797B87B}
194208
{96E12F19-B25A-415E-B965-F9DE8D713C67} = {F77AD108-C6B4-46BB-B7BC-13573F45F876}
195209
{ADCF7D08-F2EE-4DFD-A96A-7E0134A1546F} = {F77AD108-C6B4-46BB-B7BC-13573F45F876}
210+
{60678E53-EDC4-4ADE-A9EE-B194BDC76B37} = {F77AD108-C6B4-46BB-B7BC-13573F45F876}
196211
EndGlobalSection
197212
GlobalSection(ExtensibilityGlobals) = postSolution
198213
SolutionGuid = {0EDE6697-0F13-4DB1-AC56-12C15A72D395}

build/TestTasks.fs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,28 @@ let FableTestPath_input = "tests/FsSpreadsheet.Tests"
1111

1212
module RunTests =
1313

14+
open Fake.Core
15+
16+
//let createFreshTestFiles = BuildTask.create "createFreshTestFiles" [] {
17+
// let testFilesPath = "./tests/TestUtils/TestFiles"
18+
// let source = System.IO.FileInfo(testFilesPath + @"/TestWorkbook_Excel.xlsx")
19+
// let scriptsFolder = "/Scripts"
20+
// let testFiles =
21+
// [|
22+
// @"/TestWorkbook_FsSpreadsheet.net.xlsx", @".\runFsSpreadsheet.fsx.cmd"
23+
// @"/TestWorkbook_FsSpreadsheet.js.xlsx", @".\runFsSpreadsheet.js.cmd"
24+
// @"/TestWorkbook_FableExceljs.xlsx", @".\runFableExceljs"
25+
// @"/TestWorkbook_ClosedXML.xlsx", @".\runClosedXml"
26+
// |]
27+
28+
// for testFile, script in testFiles do
29+
// let target = System.IO.FileInfo(testFilesPath + testFile)
30+
// if source.LastWriteTimeUtc > target.LastWriteTimeUtc then
31+
// let scriptFolderPath = testFilesPath + scriptsFolder
32+
// Trace.traceImportant $"Update `{testFile}` with `{script}`, as source file was updated since last transpilation."
33+
// run (createProcess script) "" scriptFolderPath
34+
//}
35+
1436
/// runs `npm test` in root.
1537
/// npm test consists of `test` and `pretest`
1638
/// check package.json in root for behavior

src/FsSpreadsheet.CsvIO/FsExtension.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module FsExtensions =
1616
cells
1717
|> Seq.tryPick (fun cell ->
1818
if cell.ColumnNumber = i then
19-
Option.Some cell.Value
19+
cell.ValueAsString() |> Some
2020
else
2121
None
2222
)

src/FsSpreadsheet.ExcelIO/Cell.fs

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,6 @@ module Cell =
3636
/// </summary>
3737
let setValue (value : string) (cellValue : CellValue) = cellValue.Text <- value
3838

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-
5139
/// <summary>
5240
/// Takes a CellValue and returns the appropriate DataType.
5341
/// </summary>
@@ -94,8 +82,20 @@ module Cell =
9482
/// <summary>
9583
/// Creates a Cell from a CellValues type case, a "A1" style reference, and a CellValue containing the value string.
9684
/// </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)
9999

100100
/// <summary>
101101
/// Sets the preserve attribute of a Cell.
@@ -118,7 +118,7 @@ module Cell =
118118
i
119119
|> string
120120
|> CellValue.create
121-
|> create CellValues.SharedString reference
121+
|> create (Some CellValues.SharedString) reference
122122
| None ->
123123
let updatedSharedStringTable =
124124
sharedStringTable
@@ -128,7 +128,7 @@ module Cell =
128128
|> SharedStringTable.count
129129
|> string
130130
|> CellValue.create
131-
|> create CellValues.SharedString reference
131+
|> create (Some CellValues.SharedString) reference
132132
|> fun c ->
133133
if s.EndsWith " " then
134134
setSpacePreserveAttribute c
@@ -137,26 +137,21 @@ module Cell =
137137
| _ ->
138138
let valType,value = inferCellValue value
139139
let reference = CellReference.ofIndices columnIndex (rowIndex)
140-
create valType reference (CellValue.create value)
140+
create (Some valType) reference (CellValue.create value)
141141
|> fun c ->
142142
if value.EndsWith " " then
143143
setSpacePreserveAttribute c
144144
else c
145145

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
150148
match dataType with
151149
| DataType.String when sharedStringTable.IsSome->
152150
let sharedStringTable = sharedStringTable.Value
153-
let reference = CellReference.ofIndices columnIndex (rowIndex)
154151
match SharedStringTable.tryGetIndexByString value sharedStringTable with
155152
| Some i ->
156153
i
157154
|> string
158-
|> CellValue.create
159-
|> create CellValues.SharedString reference
160155
| None ->
161156
let updatedSharedStringTable =
162157
sharedStringTable
@@ -165,22 +160,38 @@ module Cell =
165160
updatedSharedStringTable
166161
|> SharedStringTable.count
167162
|> 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|}
174180

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 ->
180192
if value.EndsWith " " then
181193
setSpacePreserveAttribute c
182194
else c
183-
184195
/// <summary>
185196
/// Gets "A1"-style Cell reference.
186197
/// </summary>
@@ -256,7 +267,6 @@ module Cell =
256267
match cell |> tryGetType with
257268
| Some (CellValues.SharedString) when sharedStringTable.IsSome->
258269
let sharedStringTable = sharedStringTable.Value
259-
260270
let sharedStringTableIndex =
261271
cell
262272
|> getCellValue

0 commit comments

Comments
 (0)