diff --git a/src/FsSpreadsheet/Json/Cell.fs b/src/FsSpreadsheet/Json/Cell.fs index 5978d00..babbfe9 100644 --- a/src/FsSpreadsheet/Json/Cell.fs +++ b/src/FsSpreadsheet/Json/Cell.fs @@ -20,9 +20,9 @@ let encodeRows (cell:FsCell) = let decodeRows rowNumber : Decoder = Decode.object (fun builder -> - let v,dt = builder.Required.Field value (Value.decode) - let c = builder.Required.Field column Decode.int - new FsCell(v,dt,FsAddress(rowNumber,c)) + let v,dt = builder.Optional.Field value (Value.decode) |> Option.defaultValue ("", DataType.Empty) + let c = builder.Optional.Field column Decode.int |> Option.defaultValue 0 + new FsCell(v,dt,FsAddress(Option.defaultValue 0 rowNumber,c)) ) diff --git a/src/FsSpreadsheet/Json/Row.fs b/src/FsSpreadsheet/Json/Row.fs index 66a659e..5fcdd50 100644 --- a/src/FsSpreadsheet/Json/Row.fs +++ b/src/FsSpreadsheet/Json/Row.fs @@ -15,9 +15,9 @@ let encode (row:FsRow) = cells, Encode.seq (row.Cells |> Seq.map Cell.encodeRows) ] -let decode : Decoder = +let decode : Decoder = Decode.object (fun builder -> - let n = builder.Required.Field number Decode.int - let cs = builder.Required.Field cells (Decode.seq (Cell.decodeRows n)) + let n = builder.Optional.Field number Decode.int + let cs = builder.Optional.Field cells (Decode.seq (Cell.decodeRows n)) |> Option.defaultValue Seq.empty n,cs ) \ No newline at end of file diff --git a/src/FsSpreadsheet/Json/Worksheet.fs b/src/FsSpreadsheet/Json/Worksheet.fs index 10e7333..c285c72 100644 --- a/src/FsSpreadsheet/Json/Worksheet.fs +++ b/src/FsSpreadsheet/Json/Worksheet.fs @@ -27,16 +27,29 @@ let encodeRows (sheet:FsWorksheet) = let decodeRows : Decoder = Decode.object (fun builder -> + let mutable rowIndex = 0 + let n = builder.Required.Field name Decode.string let ts = builder.Optional.Field tables (Decode.seq Table.decode) - let rs = builder.Required.Field rows (Decode.seq Row.decode) + let rs = builder.Optional.Field rows (Decode.seq Row.decode) |> Option.defaultValue Seq.empty let sheet = new FsWorksheet(n) rs |> Seq.iter (fun (rowI,cells) -> + let mutable colIndex = 0 + let rowI = + match rowI with + | Some i -> i + | None -> rowIndex + 1 + rowIndex <- rowI let r = sheet.Row(rowI) cells |> Seq.iter (fun cell -> - let c = r[cell.ColumnNumber] + let colI = + match cell.ColumnNumber with + | 0 -> colIndex + 1 + | i -> i + colIndex <- colI + let c = r[colI] c.Value <- cell.Value c.DataType <- cell.DataType ) diff --git a/tests/FsSpreadsheet.Js.Tests/Json.Tests.fs b/tests/FsSpreadsheet.Js.Tests/Json.Tests.fs index 79b4591..5e487f3 100644 --- a/tests/FsSpreadsheet.Js.Tests/Json.Tests.fs +++ b/tests/FsSpreadsheet.Js.Tests/Json.Tests.fs @@ -8,6 +8,52 @@ open Fable.Pyxpecto let rows = testList "Rows" [ + testCase "Read Standard" <| fun _ -> + // Read object taken from https://spreadsheet.dsl.builders/#_sheets_and_rows + let s = """{ + "sheets": [ + { + "name": "Sample", + "rows": [ + { + "number": 5, + "cells": [ + { + "value": "Line Number Five" + } + ] + }, + { + }, + { + "cells": [ + { + "value": "Line Number Seven" + } + ] + } + ] + } + ] +}""" + let wb = FsWorkbook.fromRowsJsonString(s) + let sheet = Expect.wantSome (wb.TryGetWorksheetByName "Sample") "Sheet Sample" + Expect.equal sheet.Rows.Count 3 "Row count" + + Expect.isTrue (sheet.ContainsRowAt 5) "Row 5" + let row1 = sheet.Row(5) + Expect.isTrue (row1.HasCellAt 1) "Row 5 cell" + Expect.equal (row1.[1].Value) "Line Number Five" "Row 5 cell value" + + Expect.isTrue (sheet.ContainsRowAt 6) "Row 6" + let row1 = sheet.Row(6) + Expect.hasLength row1.Cells 0 "Row 6 cell count" + + Expect.isTrue (sheet.ContainsRowAt 7) "Row 7" + let row2 = sheet.Row(7) + Expect.isTrue (row2.HasCellAt 1) "Row 7 cell" + Expect.equal (row2.[1].Value) "Line Number Seven" "Row 7 cell value" + testCase "Read-Write DefaultTestObject" <| fun _ -> let dto = DefaultTestObject.defaultTestObject() let s = dto.ToRowsJsonString() diff --git a/tests/FsSpreadsheet.Net.Tests/Json.Tests.fs b/tests/FsSpreadsheet.Net.Tests/Json.Tests.fs index 25028fb..c797d9d 100644 --- a/tests/FsSpreadsheet.Net.Tests/Json.Tests.fs +++ b/tests/FsSpreadsheet.Net.Tests/Json.Tests.fs @@ -7,6 +7,51 @@ open Fable.Pyxpecto let rows = testList "Rows" [ + testCase "Read Standard" <| fun _ -> + // Read object taken from https://spreadsheet.dsl.builders/#_sheets_and_rows + let s = """{ + "sheets": [ + { + "name": "Sample", + "rows": [ + { + "number": 5, + "cells": [ + { + "value": "Line 5" + } + ] + }, + { + }, + { + "cells": [ + { + "value": "Line 7" + } + ] + } + ] + } + ] +}""" + let wb = FsWorkbook.fromRowsJsonString(s) + let sheet = Expect.wantSome (wb.TryGetWorksheetByName "Sample") "Sheet Sample" + Expect.equal sheet.Rows.Count 3 "Row count" + + Expect.isTrue (sheet.ContainsRowAt 5) "Row 5" + let row1 = sheet.Row(5) + Expect.isTrue (row1.HasCellAt 1) "Row 5 cell" + Expect.equal (row1.[1].Value) "Line 5" "Row 5 cell value" + + Expect.isTrue (sheet.ContainsRowAt 6) "Row 6" + let row1 = sheet.Row(6) + Expect.hasLength row1.Cells 0 "Row 6 cell count" + + Expect.isTrue (sheet.ContainsRowAt 7) "Row 7" + let row2 = sheet.Row(7) + Expect.isTrue (row2.HasCellAt 1) "Row 7 cell" + Expect.equal (row2.[1].Value) "Line 7" "Row 7 cell value" testCase "Read-Write DefaultTestObject" <| fun _ -> let dto = DefaultTestObject.defaultTestObject() diff --git a/tests/FsSpreadsheet.Py.Tests/Json.Tests.fs b/tests/FsSpreadsheet.Py.Tests/Json.Tests.fs index 67686d0..53b6fbf 100644 --- a/tests/FsSpreadsheet.Py.Tests/Json.Tests.fs +++ b/tests/FsSpreadsheet.Py.Tests/Json.Tests.fs @@ -7,7 +7,52 @@ open Fable.Pyxpecto let rows = testList "Rows" [ + testCase "Read Standard" <| fun _ -> + // Read object taken from https://spreadsheet.dsl.builders/#_sheets_and_rows + let s = """{ + "sheets": [ + { + "name": "Sample", + "rows": [ + { + "number": 5, + "cells": [ + { + "value": "Line 5" + } + ] + }, + { + }, + { + "cells": [ + { + "value": "Line 7" + } + ] + } + ] + } + ] +}""" + let wb = FsWorkbook.fromRowsJsonString(s) + let sheet = Expect.wantSome (wb.TryGetWorksheetByName "Sample") "Sheet Sample" + Expect.equal sheet.Rows.Count 3 "Row count" + Expect.isTrue (sheet.ContainsRowAt 5) "Row 5" + let row1 = sheet.Row(5) + Expect.isTrue (row1.HasCellAt 1) "Row 5 cell" + Expect.equal (row1.[1].Value) "Line 5" "Row 5 cell value" + + Expect.isTrue (sheet.ContainsRowAt 6) "Row 6" + let row1 = sheet.Row(6) + Expect.hasLength row1.Cells 0 "Row 6 cell count" + + Expect.isTrue (sheet.ContainsRowAt 7) "Row 7" + let row2 = sheet.Row(7) + Expect.isTrue (row2.HasCellAt 1) "Row 7 cell" + Expect.equal (row2.[1].Value) "Line 7" "Row 7 cell value" + testCase "Read-Write DefaultTestObject" <| fun _ -> let dto = DefaultTestObject.defaultTestObject() let s = dto.ToRowsJsonString()