Skip to content

Commit

Permalink
Add method to get the FsWorksheet of an FsTable
Browse files Browse the repository at this point in the history
  • Loading branch information
omaus committed Mar 16, 2023
1 parent 0857107 commit 0062c62
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/FsSpreadsheet.ExcelIO/FsExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ module FsExtensions =
let ra = FsRangeAddress(FsAddress(topLeftBoundary), FsAddress(bottomRightBoundary))
FsTable(table.Name, ra, table.TotalsRowShown, true)

/// <summary>Returns the FsWorksheet associated with the FsTable in a given FsWorkbook.</summary>
member self.GetWorksheetOfTable(workbook : FsWorkbook) =
workbook.GetWorksheets()
|> List.find (
fun s ->
s.Tables
|> List.exists (fun t -> t.Name = self.Name)
)

/// <summary>Returns the FsWorksheet associated with a given FsTable in an FsWorkbook.</summary>
static member getWorksheetOfTable workbook (table : FsTable) =
table.GetWorksheetOfTable workbook


type FsWorksheet with

Expand Down
4 changes: 1 addition & 3 deletions src/FsSpreadsheet/Tables/FsTable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,4 @@ type FsTable (name : string, rangeAddress, showTotalsRow, showHeaderRow) =
_fieldNames.Remove(oldName) |> ignore
_fieldNames.Add(newName, field)
| None ->
raise (System.ArgumentException("The field does not exist in this table", "oldName"))


raise (System.ArgumentException("The field does not exist in this table", "oldName"))
11 changes: 11 additions & 0 deletions tests/FsSpreadsheet.ExcelIO.Tests/FsExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ let dummyFsWorksheet1 = FsWorksheet("StringSheet", [], [], dumm
let dummyFsWorksheet2 = FsWorksheet("NumericSheet", [], [], dummyFsCellsCollection2)
let dummyFsWorksheet3 = FsWorksheet("TableSheet", [], [dummyFsTable], dummyFsCellsCollection3)
let dummyFsWorksheet4 = FsWorksheet("DataTypeSheet", [], [], dummyFsCellsCollection4)
dummyFsWorkbook.AddWorksheet(dummyFsWorksheet1) |> ignore
dummyFsWorkbook.AddWorksheet(dummyFsWorksheet2) |> ignore
dummyFsWorkbook.AddWorksheet(dummyFsWorksheet3) |> ignore
dummyFsWorkbook.AddWorksheet(dummyFsWorksheet4) |> ignore



Expand Down Expand Up @@ -85,6 +89,13 @@ let fsExtensionTests =
Expect.equal testCell.DataType dtOfCvs "addresses/references are not equal"
]
]
testList "FsTable" [
testList "GetWorksheetOfTable" [
testCase "gets correct worksheet" <| fun _ ->
let gottenWorksheet = dummyFsTable.GetWorksheetOfTable dummyFsWorkbook
Expect.equal gottenWorksheet.Name dummyFsWorksheet3.Name "Worksheets differ"
]
]
testList "FsWorkbook" [
testList "fromXlsxStream" [
let fsWorkbookFromStream = FsWorkbook.fromXlsxStream sr.BaseStream
Expand Down

0 comments on commit 0062c62

Please sign in to comment.