diff --git a/src/FsSpreadsheet.PythonIO/Cell.fs b/src/FsSpreadsheet.PythonIO/Cell.fs index a6f0c31..a208429 100644 --- a/src/FsSpreadsheet.PythonIO/Cell.fs +++ b/src/FsSpreadsheet.PythonIO/Cell.fs @@ -17,9 +17,9 @@ module PyCell = /// Here it will actually show the correct DateTime. But when writing, exceljs will apply local offset. //let dt = fsCell.ValueAsDateTime() |> System.DateTimeOffset ///// Therefore we add offset and it should work. - //let dt = dt + dt.Offset |> box |> Some + //let dt = dt.ToUniversalTime() + dt.Offset |> box |> Some //dt - fsCell.ValueAsDateTime() |> box |> Some + fsCell.ValueAsDateTime().ToUniversalTime() |> box |> Some | String -> fsCell.Value |> Some | anyElse -> @@ -36,48 +36,17 @@ module PyCell = /// /// let toFsCell worksheetName rowIndex columnIndex (pyCell: Cell) = - let t = CellType.fromCellType pyCell.cellType |> PyCellType.toDataType + //printfn "toFsCell worksheetName: %s, rowIndex: %i, columnIndex: %i, %A" worksheetName rowIndex columnIndex (pyCell.value, pyCell.cellType) let fsadress = FsAddress(rowIndex,columnIndex) - let createFscell = fun dt v -> FsCell(v,dt,address = fsadress) - let vTemp = string pyCell.value + let dt,v = + let dt,v = DataType.InferCellValue pyCell.value + if v = "=TRUE()" || v = "=True()" then + Boolean,box true + elif v = "=FALSE()" || v = "=False()" then + Boolean,box false + else + dt,v + FsCell(v,dt,address = fsadress) - let fscell = - match t with - | DataType.Boolean -> - let b = System.Boolean.Parse vTemp - createFscell DataType.Boolean b - | DataType.Number -> float vTemp |> createFscell DataType.Number - | DataType.Date -> - let dt = System.DateTime.Parse(vTemp)//.ToUniversalTime() - /// Without this step universal time get changed to local time? Exceljs tests will hit this. - /// - /// Expected item (from test object): C2 : Sat Oct 14 2023 00:00:00 GMT+0200 (Mitteleuropäische Sommerzeit) | Date - /// - /// Actual item (created here): C2 : Sat Oct 14 2023 02:00:00 GMT+0200 (Mitteleuropäische Sommerzeit) | Date - /// - /// But logging hour minute showed that the values were given correctly and needed to be reinitialized. - //let dt = System.DateTime(dt.Year,dt.Month,dt.Day,dt.Hour,dt.Minute, dt.Second) - dt |> createFscell DataType.Date - | DataType.String -> vTemp |> createFscell DataType.String - //| ValueType.Formula -> - // match jsCell.formula with - // | "TRUE()" -> - // let b = true - // createFscell DataType.Boolean b - // | "FALSE()" -> - // let b = false - // createFscell DataType.Boolean b - // | anyElse -> - // let msg = sprintf "ValueType 'Format' (%s) is not fully implemented in FsSpreadsheet and is handled as string input. In %s: (%i,%i)" anyElse worksheetName rowIndex columnIndex - // log msg - // anyElse |> createFscell DataType.String - //| ValueType.Hyperlink -> - // //log (c.value.Value?text) - // jsCell.value.Value?hyperlink |> createFscell DataType.String - | anyElse -> - let msg = sprintf "ValueType `%A` (%s) is not fully implemented in FsSpreadsheet and is handled as string input. In %s: (%i,%i)" anyElse vTemp worksheetName rowIndex columnIndex - printfn "%s" msg - createFscell DataType.String vTemp - fscell diff --git a/src/FsSpreadsheet.PythonIO/FsSpreadsheet.ExcelPy.fsproj b/src/FsSpreadsheet.PythonIO/FsSpreadsheet.ExcelPy.fsproj index 13c653c..51371c1 100644 --- a/src/FsSpreadsheet.PythonIO/FsSpreadsheet.ExcelPy.fsproj +++ b/src/FsSpreadsheet.PythonIO/FsSpreadsheet.ExcelPy.fsproj @@ -15,7 +15,7 @@ - + diff --git a/src/FsSpreadsheet.PythonIO/Table.fs b/src/FsSpreadsheet.PythonIO/Table.fs index 2c4f572..5b7e79f 100644 --- a/src/FsSpreadsheet.PythonIO/Table.fs +++ b/src/FsSpreadsheet.PythonIO/Table.fs @@ -12,5 +12,5 @@ module PyTable = let toFsTable(table:Table) = let name = if isNull table.displayName then table.name else table.displayName - let ref = table?ref + let ref = table.ref FsTable(name,FsRangeAddress(ref)) diff --git a/src/FsSpreadsheet.PythonIO/Workbook.fs b/src/FsSpreadsheet.PythonIO/Workbook.fs index 9ab58a4..5679b6c 100644 --- a/src/FsSpreadsheet.PythonIO/Workbook.fs +++ b/src/FsSpreadsheet.PythonIO/Workbook.fs @@ -8,16 +8,21 @@ module PyWorkbook = open Fable.Core.PyInterop let fromFsWorkbook (fsWB: FsWorkbook) : Workbook = + if fsWB.GetWorksheets().Count = 0 then + failwith "Workbook must contain at least one worksheet" let pyWB = Workbook.create() + pyWB.remove(pyWB.active) fsWB.GetWorksheets() |> Seq.iter (fun ws -> PyWorksheet.fromFsWorksheet pyWB ws |> ignore ) + //if fsWB.TryGetWorksheetByName("Sheet").IsNone then + // pyWB pyWB let toFsWorkbook(pyWB:Workbook) : FsWorkbook = let fsWB = new FsWorkbook() - pyWB?worksheets |> Array.iter (fun (ws : Worksheet) -> + pyWB.worksheets |> Array.iter (fun (ws : Worksheet) -> if ws.title <> "Sheet" && ws.values.Length <> 0 then let w = PyWorksheet.toFsWorksheet ws fsWB.AddWorksheet(w) |> ignore diff --git a/src/FsSpreadsheet.PythonIO/Worksheet.fs b/src/FsSpreadsheet.PythonIO/Worksheet.fs index 9568fe7..dc60225 100644 --- a/src/FsSpreadsheet.PythonIO/Worksheet.fs +++ b/src/FsSpreadsheet.PythonIO/Worksheet.fs @@ -7,15 +7,8 @@ module PyWorksheet = open Fable.Openpyxl open Fable.Core.PyInterop - type WorksheetStatic = - [] - abstract member create: parent:Workbook * title:string -> Worksheet - - [] - let Worksheet : WorksheetStatic = nativeOnly - - let fromFsWorksheet (parent : Workbook) (fsWS: FsWorksheet) : Worksheet = - let pyWS = Worksheet.create(parent,fsWS.Name) + let fromFsWorksheet (parent : Workbook) (fsWS: FsWorksheet) : Worksheet = + let pyWS = parent.create_sheet(fsWS.Name) fsWS.Tables |> Seq.iter (fun table -> let pyTable = PyTable.fromFsTable table @@ -37,8 +30,9 @@ module PyWorksheet = ) pyWS.rows |> Array.iteri (fun rowIndex row -> row |> Array.iteri (fun colIndex cell -> - let c = PyCell.toFsCell pyWS.title rowIndex colIndex cell - fsWS.AddCell(c) |> ignore + if cell.cellType <> "NoneType" then + let c = PyCell.toFsCell pyWS.title (rowIndex + 1) (colIndex + 1) cell + fsWS.AddCell(c) |> ignore ) ) fsWS diff --git a/tests/FsSpreadsheet.ExcelPy.Tests/Workbook.Tests.fs b/tests/FsSpreadsheet.ExcelPy.Tests/Workbook.Tests.fs index 75d8cd1..41d7fb9 100644 --- a/tests/FsSpreadsheet.ExcelPy.Tests/Workbook.Tests.fs +++ b/tests/FsSpreadsheet.ExcelPy.Tests/Workbook.Tests.fs @@ -122,10 +122,9 @@ let private tests_toFsWorkbook = testList "toFsWorkbook" [ let tests_toPyWorkbook = testList "toPyWorkbook" [ testCase "empty" <| fun _ -> + let fsWB = new FsWorkbook() - let pyWB = PyWorkbook.fromFsWorkbook fsWB - let jswslist = pyWB?worksheets |> Array.length - Expect.equal jswslist 0 "both no worksheet" + Expect.fails (fun () -> PyWorkbook.fromFsWorkbook fsWB |> ignore) "no worksheet given" testCase "worksheet" <| fun _ -> let fsWB = new FsWorkbook() let _ = fsWB.InitWorksheet("my awesome worksheet") diff --git a/tests/FsSpreadsheet.ExcelPy.Tests/Worksheet.Tests.fs b/tests/FsSpreadsheet.ExcelPy.Tests/Worksheet.Tests.fs index 1470df4..4c65b3b 100644 --- a/tests/FsSpreadsheet.ExcelPy.Tests/Worksheet.Tests.fs +++ b/tests/FsSpreadsheet.ExcelPy.Tests/Worksheet.Tests.fs @@ -34,7 +34,7 @@ let fromFsWorksheet = testList "fromFsWorksheet" [ let toFsWorksheet = testList "toFsWorksheet" [ testCase "Empty" <| fun _ -> let wb = Workbook.create() - let pyWS = PyWorksheet.Worksheet.create(wb,wsName) + let pyWS = wb.create_sheet(wsName) let fsWS = PyWorksheet.toFsWorksheet pyWS Expect.equal fsWS.Name wsName "Name did not match" Expect.equal (fsWS.CellCollection.GetCells() |> Seq.length) 0 "Cells did not match" diff --git a/tests/TestUtils/TestingUtils.fs b/tests/TestUtils/TestingUtils.fs index 101cba9..e5a2bb4 100644 --- a/tests/TestUtils/TestingUtils.fs +++ b/tests/TestUtils/TestingUtils.fs @@ -122,6 +122,13 @@ module Expect = let passWithMsg (message: string) = equal true true message + let fails (f : unit -> unit) message = + try + f() + failwith $"Function should have failed but did not: {message}" + with + | _ -> () + /// Fable compatible Expecto/Mocha unification [] module Test =