Skip to content

Commit

Permalink
Add ((non-)static) methods to FsWorkbook.fs
Browse files Browse the repository at this point in the history
  • Loading branch information
omaus committed Mar 2, 2023
1 parent fd6fe47 commit 75d7ca4
Showing 1 changed file with 65 additions and 11 deletions.
76 changes: 65 additions & 11 deletions src/FsSpreadsheet/FsWorkbook.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,78 @@

type FsWorkbook() =

let mutable worksheets = []
let mutable _worksheets = []

interface System.IDisposable with
member self.Dispose() = ()

// maybe better to leave that to methods...
//member self.Worksheets
// with get() = _worksheets
// and set(value) = _worksheets <- value


// ------------------
// NON-STATIC METHODS
// ------------------

/// Adds an FsWorksheet with given name.
member self.AddWorksheet(name : string) =
let sheet = FsWorksheet (name)
worksheets <- List.append worksheets [sheet]
let sheet = FsWorksheet name
_worksheets <- List.append _worksheets [sheet]
sheet

/// Adds a given FsWorksheet.
member self.AddWorksheet(sheet : FsWorksheet) =
if worksheets |> List.exists (fun ws -> ws.Name = sheet.Name) then
if _worksheets |> List.exists (fun ws -> ws.Name = sheet.Name) then
failwithf "Could not add worksheet with name \"%s\" to workbook as it already contains a worksheet with the same name" sheet.Name
else
worksheets <- List.append worksheets [sheet]
_worksheets <- List.append _worksheets [sheet]
sheet

member self.GetWorksheets() = worksheets
/// Returns all FsWorksheets.
member self.GetWorksheets() =
_worksheets


interface System.IDisposable with

member self.Dispose() = ()

/// Removes an FsWorksheet with given name.
member self.RemoveWorksheet(name : string) =
let filteredWorksheets =
match _worksheets |> List.tryFind (fun ws -> ws.Name = name) with
| Some w -> _worksheets |> List.filter (fun ws -> ws.Name <> name)
| None -> failwith $"FsWorksheet with name {name} was not found in FsWorkbook."
_worksheets <- filteredWorksheets
self

/// Removes a given FsWorksheet.
member self.RemoveWorksheet(sheet : FsWorksheet) =
let filteredWorksheets =
match _worksheets |> List.tryFind ((=) sheet) with
| Some w -> _worksheets |> List.filter ((<>) sheet)
| None -> failwith $"FsWorksheet with name {sheet.Name} was not found in FsWorkbook."
_worksheets <- filteredWorksheets
self


// --------------
// STATIC METHODS
// --------------

/// Adds an FsWorksheet with given name to an FsWorkbook.
static member addWorksheetWithName (name : string) (workbook : FsWorkbook) =
workbook.AddWorksheet name

/// Adds an FsWorksheet to an FsWorkbook.
static member addWorksheet (sheet : FsWorksheet) (workbook : FsWorkbook) =
workbook.AddWorksheet sheet

/// Returns all FsWorksheets.
static member getWorksheets (workbook : FsWorkbook) =
workbook.GetWorksheets()

/// Removes an FsWorksheet with given name from an FsWorkbook.
static member removeWorksheetByName (name : string) (workbook : FsWorkbook) =
workbook.RemoveWorksheet name

/// Removes a given FsWorksheet from an FsWorkbook.
static member removeWorksheet (sheet : FsWorksheet) (workbook : FsWorkbook) =
workbook.RemoveWorksheet sheet

0 comments on commit 75d7ca4

Please sign in to comment.