Skip to content

Commit

Permalink
Merge pull request #76 from fslaborg/io_tests
Browse files Browse the repository at this point in the history
Io tests
  • Loading branch information
Freymaurer authored Oct 26, 2023
2 parents eedf32f + db263ed commit 62ff8ec
Show file tree
Hide file tree
Showing 62 changed files with 1,430 additions and 366 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,4 @@ tmp/
/tests/JS/FsSpreadsheet.Exceljs
/tests/FsSpreadsheet.Exceljs.Tests/js
/dist
/tests/TestUtils/TestFiles/Scripts/fable
15 changes: 15 additions & 0 deletions FsSpreadsheet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "JS", "JS", "{ADCF7D08-F2EE-
tests\JS\Exceljs.js = tests\JS\Exceljs.js
EndProjectSection
EndProject
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "TestUtils", "tests\TestUtils\TestUtils.fsproj", "{60678E53-EDC4-4ADE-A9EE-B194BDC76B37}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -177,6 +179,18 @@ Global
{96E12F19-B25A-415E-B965-F9DE8D713C67}.Release|x64.Build.0 = Release|Any CPU
{96E12F19-B25A-415E-B965-F9DE8D713C67}.Release|x86.ActiveCfg = Release|Any CPU
{96E12F19-B25A-415E-B965-F9DE8D713C67}.Release|x86.Build.0 = Release|Any CPU
{60678E53-EDC4-4ADE-A9EE-B194BDC76B37}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{60678E53-EDC4-4ADE-A9EE-B194BDC76B37}.Debug|Any CPU.Build.0 = Debug|Any CPU
{60678E53-EDC4-4ADE-A9EE-B194BDC76B37}.Debug|x64.ActiveCfg = Debug|Any CPU
{60678E53-EDC4-4ADE-A9EE-B194BDC76B37}.Debug|x64.Build.0 = Debug|Any CPU
{60678E53-EDC4-4ADE-A9EE-B194BDC76B37}.Debug|x86.ActiveCfg = Debug|Any CPU
{60678E53-EDC4-4ADE-A9EE-B194BDC76B37}.Debug|x86.Build.0 = Debug|Any CPU
{60678E53-EDC4-4ADE-A9EE-B194BDC76B37}.Release|Any CPU.ActiveCfg = Release|Any CPU
{60678E53-EDC4-4ADE-A9EE-B194BDC76B37}.Release|Any CPU.Build.0 = Release|Any CPU
{60678E53-EDC4-4ADE-A9EE-B194BDC76B37}.Release|x64.ActiveCfg = Release|Any CPU
{60678E53-EDC4-4ADE-A9EE-B194BDC76B37}.Release|x64.Build.0 = Release|Any CPU
{60678E53-EDC4-4ADE-A9EE-B194BDC76B37}.Release|x86.ActiveCfg = Release|Any CPU
{60678E53-EDC4-4ADE-A9EE-B194BDC76B37}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -193,6 +207,7 @@ Global
{E72A14FF-5026-463B-B0FA-2DA104D67B0C} = {1CF1274C-DE28-4446-9B4E-5884E797B87B}
{96E12F19-B25A-415E-B965-F9DE8D713C67} = {F77AD108-C6B4-46BB-B7BC-13573F45F876}
{ADCF7D08-F2EE-4DFD-A96A-7E0134A1546F} = {F77AD108-C6B4-46BB-B7BC-13573F45F876}
{60678E53-EDC4-4ADE-A9EE-B194BDC76B37} = {F77AD108-C6B4-46BB-B7BC-13573F45F876}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0EDE6697-0F13-4DB1-AC56-12C15A72D395}
Expand Down
22 changes: 22 additions & 0 deletions build/TestTasks.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,28 @@ let FableTestPath_input = "tests/FsSpreadsheet.Tests"

module RunTests =

open Fake.Core

//let createFreshTestFiles = BuildTask.create "createFreshTestFiles" [] {
// let testFilesPath = "./tests/TestUtils/TestFiles"
// let source = System.IO.FileInfo(testFilesPath + @"/TestWorkbook_Excel.xlsx")
// let scriptsFolder = "/Scripts"
// let testFiles =
// [|
// @"/TestWorkbook_FsSpreadsheet.net.xlsx", @".\runFsSpreadsheet.fsx.cmd"
// @"/TestWorkbook_FsSpreadsheet.js.xlsx", @".\runFsSpreadsheet.js.cmd"
// @"/TestWorkbook_FableExceljs.xlsx", @".\runFableExceljs"
// @"/TestWorkbook_ClosedXML.xlsx", @".\runClosedXml"
// |]

// for testFile, script in testFiles do
// let target = System.IO.FileInfo(testFilesPath + testFile)
// if source.LastWriteTimeUtc > target.LastWriteTimeUtc then
// let scriptFolderPath = testFilesPath + scriptsFolder
// Trace.traceImportant $"Update `{testFile}` with `{script}`, as source file was updated since last transpilation."
// run (createProcess script) "" scriptFolderPath
//}

/// runs `npm test` in root.
/// npm test consists of `test` and `pretest`
/// check package.json in root for behavior
Expand Down
2 changes: 1 addition & 1 deletion src/FsSpreadsheet.CsvIO/FsExtension.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module FsExtensions =
cells
|> Seq.tryPick (fun cell ->
if cell.ColumnNumber = i then
Option.Some cell.Value
cell.ValueAsString() |> Some
else
None
)
Expand Down
84 changes: 47 additions & 37 deletions src/FsSpreadsheet.ExcelIO/Cell.fs
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,6 @@ module Cell =
/// </summary>
let setValue (value : string) (cellValue : CellValue) = cellValue.Text <- value

/// <summary>
/// Takes a DataType and returns the appropriate CellValue.
/// </summary>
/// <remarks>DataType is the FsSpreadsheet representation of the CellValue enum in OpenXml.</remarks>
let cellValuesFromDataType (dataType : DataType) =
match dataType with
| String -> CellValues.String
| Boolean -> CellValues.Boolean
| Number -> CellValues.Number
| Date -> CellValues.Date
| Empty -> CellValues.Error

/// <summary>
/// Takes a CellValue and returns the appropriate DataType.
/// </summary>
Expand Down Expand Up @@ -94,8 +82,20 @@ module Cell =
/// <summary>
/// Creates a Cell from a CellValues type case, a "A1" style reference, and a CellValue containing the value string.
/// </summary>
let create (dataType : CellValues) (reference : string) (value : CellValue) =
Cell(CellReference = StringValue.FromString reference, DataType = EnumValue(dataType), CellValue = value)
let create (dataType : CellValues option) (reference : string) (value : CellValue) =
match dataType with
| Some dataType -> Cell(CellReference = StringValue.FromString reference, DataType = EnumValue(dataType), CellValue = value)
| None -> Cell(CellReference = StringValue.FromString reference, CellValue = value)

/// <summary>
/// Creates a Cell from a CellValues type case, a "A1" style reference, and a CellValue containing the value string.
/// </summary>
let createWithFormat doc (dataType : CellValues option) (reference : string) (cellFormat : CellFormat) (value : CellValue) =
let styleSheet = Stylesheet.getOrInit doc
let i = Stylesheet.CellFormat.appendOrGetIndex cellFormat styleSheet
match dataType with
| Some dataType -> Cell(StyleIndex = UInt32Value(uint32 i),CellReference = StringValue.FromString reference, DataType = EnumValue(dataType), CellValue = value)
| None -> Cell(StyleIndex = UInt32Value(uint32 i),CellReference = StringValue.FromString reference, CellValue = value)

/// <summary>
/// Sets the preserve attribute of a Cell.
Expand All @@ -118,7 +118,7 @@ module Cell =
i
|> string
|> CellValue.create
|> create CellValues.SharedString reference
|> create (Some CellValues.SharedString) reference
| None ->
let updatedSharedStringTable =
sharedStringTable
Expand All @@ -128,7 +128,7 @@ module Cell =
|> SharedStringTable.count
|> string
|> CellValue.create
|> create CellValues.SharedString reference
|> create (Some CellValues.SharedString) reference
|> fun c ->
if s.EndsWith " " then
setSpacePreserveAttribute c
Expand All @@ -137,26 +137,21 @@ module Cell =
| _ ->
let valType,value = inferCellValue value
let reference = CellReference.ofIndices columnIndex (rowIndex)
create valType reference (CellValue.create value)
create (Some valType) reference (CellValue.create value)
|> fun c ->
if value.EndsWith " " then
setSpacePreserveAttribute c
else c

/// <summary>
/// Create a cell using a shared string table, also returns the updated shared string table.
/// </summary>
let fromValueWithDataType (sharedStringTable : SharedStringTable Option) columnIndex rowIndex (value : string) (dataType : DataType) =
let getCellContent (doc : Packaging.SpreadsheetDocument) (value : string) (dataType : DataType) =
let sharedStringTable = SharedStringTable.tryGet doc
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
Expand All @@ -165,22 +160,38 @@ module Cell =
updatedSharedStringTable
|> SharedStringTable.count
|> string
|> CellValue.create
|> create CellValues.SharedString reference
|> fun c ->
if value.EndsWith " " then
setSpacePreserveAttribute c
else c
|> fun v -> {|DataType = Some CellValues.SharedString; Value = v; Format = None|}
| DataType.String ->
{|DataType = Some CellValues.String; Value = value; Format = None|}
| DataType.Boolean ->
{|DataType = Some CellValues.Boolean; Value = System.Boolean.Parse value |> FsCellAux.boolConverter; Format = None|}
| DataType.Number ->
{|DataType = Some CellValues.Number; Value = value; Format = None|}
| DataType.Date ->
//let cellFormat = CellFormat(NumberFormatId = UInt32Value 19u, ApplyNumberFormat = BooleanValue true)
let value = System.DateTime.Parse(value).ToOADate() |> string
let cellFormat =
if value.Contains(".") then
Stylesheet.CellFormat.getDefaultDateTime()
else
Stylesheet.CellFormat.getDefaultDate()
{|DataType = None; Value = value; Format = Some cellFormat|}
| DataType.Empty -> {|DataType = None; Value = value; Format = None|}

| _ ->
let valType = cellValuesFromDataType dataType
let reference = CellReference.ofIndices columnIndex (rowIndex)
create valType reference (CellValue.create value)
|> fun c ->
/// <summary>
/// Create a cell using a shared string table, also returns the updated shared string table.
/// </summary>
let fromValueWithDataType (doc : Packaging.SpreadsheetDocument) columnIndex rowIndex (value : string) (dataType : DataType) =
let reference = CellReference.ofIndices columnIndex (rowIndex)
let cellContent = getCellContent doc value dataType
if cellContent.Format.IsSome then
createWithFormat doc cellContent.DataType reference cellContent.Format.Value (CellValue.create cellContent.Value)
else
create cellContent.DataType reference (CellValue.create cellContent.Value)
|> fun c ->
if value.EndsWith " " then
setSpacePreserveAttribute c
else c

/// <summary>
/// Gets "A1"-style Cell reference.
/// </summary>
Expand Down Expand Up @@ -256,7 +267,6 @@ module Cell =
match cell |> tryGetType with
| Some (CellValues.SharedString) when sharedStringTable.IsSome->
let sharedStringTable = sharedStringTable.Value

let sharedStringTableIndex =
cell
|> getCellValue
Expand Down
Loading

0 comments on commit 62ff8ec

Please sign in to comment.