Skip to content

Commit

Permalink
xlsx reader now parses headerless objects
Browse files Browse the repository at this point in the history
  • Loading branch information
HLWeil committed Jul 25, 2023
1 parent 65174d9 commit b2c3435
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/FsSpreadsheet.ExcelIO/FsExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ module FsExtensions =
let topLeftBoundary, bottomRightBoundary = Table.getArea table |> Table.Area.toBoundaries
let ra = FsRangeAddress(FsAddress(topLeftBoundary), FsAddress(bottomRightBoundary))
let totalsRowShown = if table.TotalsRowShown = null then false else table.TotalsRowShown.Value
FsTable(table.Name, ra, totalsRowShown, true)
let showHeaderRow = if table.HeaderRowCount = null then false else table.HeaderRowCount.Value = 1u
FsTable(table.Name, ra, totalsRowShown, showHeaderRow)

/// <summary>
/// Returns the FsWorksheet associated with the FsTable in a given FsWorkbook.
Expand Down
7 changes: 6 additions & 1 deletion src/FsSpreadsheet.ExcelIO/Table.fs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ module Table =

/// Given a "A1:A1"-style area, returns A1-based cell start and end cellReferences.
let toBoundaries (area : StringValue) =

area.Value.Split ':'
|> fun a -> a.[0], a.[1]
|> fun a ->
if a.Length = 1 then
area.Value, area.Value
else
a.[0], a.[1]

/// Gets the right boundary of the area.
let rightBoundary (area : StringValue) =
Expand Down
9 changes: 8 additions & 1 deletion tests/FsSpreadsheet.ExcelIO.Tests/Table.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ let transformTable =
Expect.isTrue (table.TotalsRowShown = null) "Check that field of interest is None"
FsTable.fromXlsxTable table |> ignore
)

testCase "handleNoHeaders" (fun () ->
let doc = Spreadsheet.fromFile TestObjects.headerLessTablePath false
let wsp = doc.WorkbookPart.WorksheetParts |> Seq.head
let t = wsp |> Worksheet.WorksheetPart.getTables |> Seq.head
let parsed = FsTable.fromXlsxTable t
Expect.equal (parsed.RangeAddress.ToString()) "B8:B8" "Range address should be B8:B8"
Expect.isFalse parsed.ShowHeaderRow "ShowHeaderRow should be false"
)
]


Expand Down
5 changes: 4 additions & 1 deletion tests/FsSpreadsheet.ExcelIO.Tests/TestObjects.fs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,7 @@ let sheet2() =
FsCell.createWithDataType DataType.Number 2 4 8
]
|> List.iter (fun c -> ws.Row(c.RowNumber).[c.ColumnNumber].SetValueAs c.Value)
ws
ws

let headerLessTablePath =
System.IO.Path.Combine(__SOURCE_DIRECTORY__, "data/headerLessTable.xlsx")
Binary file not shown.

0 comments on commit b2c3435

Please sign in to comment.