Skip to content

Commit

Permalink
Add static methods, update existing methods
Browse files Browse the repository at this point in the history
  • Loading branch information
omaus committed Mar 21, 2023
1 parent 9b8de2e commit 31a9cbf
Showing 1 changed file with 71 additions and 40 deletions.
111 changes: 71 additions & 40 deletions src/FsSpreadsheet/Tables/FsTable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ type FsTable (name : string, rangeAddress, showTotalsRow, showHeaderRow) =

/// <summary>Returns all fieldnames as `fieldname*FsTableField` dictionary.</summary>
member self.FieldNames
with get(cells) =
with get(cellsCollection) =
if (_fieldNames <> null && _lastRangeAddress <> null && _lastRangeAddress.Equals(self.RangeAddress)) then
_fieldNames;
else
_lastRangeAddress <- self.RangeAddress

//self.RescanFieldNames(cells)
//self.RescanFieldNames(cellsCollection)

_fieldNames;

/// The FsTableFields of this FsTable.<summary>
/// <summary>The FsTableFields of this FsTable.<summary>
member self.Fields
with get(cells) =
with get(cellsCollection) =
let columnCount = base.ColumnCount()
Seq.init columnCount (fun i -> self.Field(i, cells))
Seq.init columnCount (fun i -> self.GetField(i, cellsCollection))

/// <summary>Gets or sets if the header row is shown.</summary>
member self.ShowHeaderRow
Expand All @@ -64,7 +64,7 @@ type FsTable (name : string, rangeAddress, showTotalsRow, showHeaderRow) =

/// Takes the respective FsCellsCollection for this FsTable and creates a new _fieldNames dictionary if the current one does not match.</summary>
// TO DO: maybe HLW can specify above description a bit...
member private self.RescanFieldNames(cells : FsCellsCollection) =
member private self.RescanFieldNames(cellsCollection : FsCellsCollection) =
printfn "Start RescanFieldNames"
_fieldNames
|> Seq.iter (fun kv -> printfn "Key: %s, index: %i, name: %s" kv.Key kv.Value.Index kv.Value.Name)
Expand All @@ -73,7 +73,7 @@ type FsTable (name : string, rangeAddress, showTotalsRow, showHeaderRow) =
_fieldNames <- new Dictionary<string, FsTableField>()
let headersRow = self.HeadersRow(false);
let mutable cellPos = 0
for cell in headersRow.Cells(cells) do
for cell in headersRow.Cells(cellsCollection) do
let mutable name = cell.Value //GetString();
match Dictionary.tryGet name oldFieldNames with
| Some tableField ->
Expand Down Expand Up @@ -109,14 +109,30 @@ type FsTable (name : string, rangeAddress, showTotalsRow, showHeaderRow) =
_fieldNames
|> Seq.iter (fun kv -> printfn "Key: %s, index: %i, name: %s" kv.Key kv.Value.Index kv.Value.Name)


member self.RescanRange () =
/// <summary>
/// Updates the FsRangeAddress of the FsTable according to the FsTableFields associated.
/// </summary>
member self.RescanRange() =
let rangeAddress =
_fieldNames.Values
|> Seq.map (fun v -> v.Column.RangeAddress)
|> Seq.reduce (fun r1 r2 -> r1.Union(r2))
base.RangeAddress <- rangeAddress

/// <summary>
/// Updates the FsRangeAddress of a given FsTable according to the FsTableFields associated.
/// </summary>
static member rescanRange (table : FsTable) =
table.RescanRange()

/// <summary>
/// Returns a unique name consisting of the original name and an initial offset that is raised
/// if the original name with that offset is already present.
/// </summary>
/// <param name="enforceOffset">
/// If true, the initial offset is always
/// </param>
// TO DO: HLW: make this description more precise. What is this method even about?
member this.GetUniqueName(originalName : string, initialOffset : int32, enforceOffset : bool) =
let mutable name = originalName + if enforceOffset then string initialOffset else ""
if _uniqueNames.Contains(name) then
Expand All @@ -128,8 +144,12 @@ type FsTable (name : string, rangeAddress, showTotalsRow, showHeaderRow) =
i <- i + 1
name <- originalName + string i

_uniqueNames.Add name |> ignore
name

static member getUniqueNames originalName initialOffset enforceOffset (table : FsTable) =
table.GetUniqueName(originalName, initialOffset, enforceOffset)

//member this.AddFields(fieldNames : IEnumerable<string>) =

// _fieldNames = new Dictionary<String, IXLTableField>();
Expand All @@ -140,55 +160,66 @@ type FsTable (name : string, rangeAddress, showTotalsRow, showHeaderRow) =
// _fieldNames.Add(name, new XLTableField(this, name) { Index = cellPos++ });
// }

/// <summary>Returns the FsTableField with given name. If an FsTableField does not exist under this name in the FsTable, adds it.</summary>
member self.Field(name : string, cellsCollection : FsCellsCollection) =
match Dictionary.tryGet name _fieldNames with
| Some field ->
field
| None ->
let maxIndex =
_fieldNames.Values
|> Seq.map (fun v -> v.Index)
|> fun s ->
if Seq.length s = 0 then 0 else Seq.max s
let range =
let offset = _fieldNames.Count
let firstAddress = FsAddress(self.RangeAddress.FirstAddress.RowNumber,self.RangeAddress.FirstAddress.ColumnNumber + offset)
let lastAddress = FsAddress(self.RangeAddress.LastAddress.RowNumber,self.RangeAddress.FirstAddress.ColumnNumber + offset)
FsRangeAddress(firstAddress,lastAddress)
let column = FsRangeColumn(range)
let newField = FsTableField(name,maxIndex + 1,column,null,null)
if self.ShowHeaderRow then
newField.HeaderCell(cellsCollection,true).SetValueAs name |> ignore
_fieldNames.Add(name,newField)
self.RescanRange()
newField

/// <summary>Takes a name of an FsTableField and an FsCellsCollection (belonging to the FsWorksheet of this FsTable) and returns the respective FsTableField.</summary>
/// <exception cref="System.ArgumentException">if the header row has no field with the given name.</exception>
member self.Field(name : string, cells : FsCellsCollection) =
member self.GetField(name : string, cellsCollection : FsCellsCollection) =
let name = name.Replace("\r\n", "\n")
try self.FieldNames(cells).Item name
try self.FieldNames(cellsCollection).Item name
with _ -> failwith <| "The header row doesn't contain field name '" + name + "'."

// TO DO: ask HLW: why this way?
///// <summary>Returns the FsTableField with given name. If an FsTableField does not exist under this name in the FsTable, adds it.</summary>
//member self.Field(name, cells : FsCellsCollection) =
//match Dictionary.tryGet name _fieldNames with
//| Some field ->
// field
//| None ->
// let maxIndex =
// _fieldNames.Values
// |> Seq.map (fun v -> v.Index)
// |> fun s ->
// if Seq.length s = 0 then 0 else Seq.max s
// let range =
// let offset = _fieldNames.Count
// let firstAddress = FsAddress(self.RangeAddress.FirstAddress.RowNumber,self.RangeAddress.FirstAddress.ColumnNumber + offset)
// let lastAddress = FsAddress(self.RangeAddress.LastAddress.RowNumber,self.RangeAddress.FirstAddress.ColumnNumber + offset)
// FsRangeAddress(firstAddress,lastAddress)
// let column = FsRangeColumn(range)
// let newField = FsTableField(name,maxIndex + 1,column,null,null)
// if self.ShowHeaderRow then
// newField.HeaderCell(cells,true).SetValueAs name |> ignore
// _fieldNames.Add(name,newField)
// self.RescanRange()
// newField
/// <summary>Takes a name of an FsTableField and an FsCellsCollection (belonging to the FsWorksheet of this FsTable) and returns the respective FsTableField.</summary>
/// <exception cref="System.ArgumentException">if the header row has no field with the given name.</exception>
static member getFieldByName (name : string) (cellsCollection : FsCellsCollection) (table : FsTable) =
table.GetField(name, cellsCollection)

/// <summary>Takes the index of an FsTableField and an FsCellsCollection (belonging to the FsWorksheet of this FsTable) and returns the respective FsTableField.</summary>
/// <exception cref="System.ArgumentException">if the FsTable has no FsTableField with the given index.</exception>
member self.Field(index, cells) =
member self.GetField(index, cellsCollection) =
try
self.FieldNames(cells).Values
self.FieldNames(cellsCollection).Values
|> Seq.find (fun ftf -> ftf.Index = index)
with _ -> failwith $"FsTableField with index {index} does not exist in the FsTable."

/// <summary>Takes a name of an FsTableField and an FsCellsCollection (belonging to the FsWorksheet of this FsTable) and returns the index of the respective FsTableField.</summary>
/// <exception cref="System.ArgumentException">if the header row has no field with the given name.</exception>
member self.GetFieldIndex(name : string, cells) =
self.Field(name, cells).Index
member self.GetFieldIndex(name : string, cellsCollection) =
self.GetField(name, cellsCollection).Index

/// <summary>Renames a fieldname of the FsTable if it exists. Else fails.</summary>
/// <exception cref="System.ArgumentException">if the FsTableField does not exist in the FsTable.</exception>
member this.RenameField(oldName : string, newName : string) =
match Dictionary.tryGet oldName _fieldNames with
| Some field ->
_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 FsTabelField does not exist in this FsTable", "oldName"))

/// <summary>Renames a fieldname of the FsTable if it exists. Else fails.</summary>
/// <exception cref="System.ArgumentException">if the FsTableField does not exist in the FsTable.</exception>
static member renameField oldName newName (table : FsTable) =
table.RenameField(oldName, newName)

0 comments on commit 31a9cbf

Please sign in to comment.