Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debug excel enable conversion of data and free text columns of input / output #550

Merged
merged 7 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 67 additions & 42 deletions src/Client/OfficeInterop/OfficeInterop.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1587,9 +1587,7 @@ let addAnnotationBlockHandler (newBB: CompositeColumn) =
/// <param name="selectedIndex"></param>
let getSelectedBuildingBlock (table: Table) (context: RequestContext) =
promise {

let selectedRange = context.workbook.getSelectedRange().load(U2.Case2 (ResizeArray[|"columnIndex"|]))

let headerRange = table.getHeaderRowRange()
let _ = headerRange.load(U2.Case2 (ResizeArray [|"columnIndex"; "values"; "columnCount"|])) |> ignore

Expand Down Expand Up @@ -1633,6 +1631,68 @@ let getSelectedBuildingBlockCell (table: Table) (context: RequestContext) =
)
}

/// <summary>
/// Get the main column of the arc table of the selected building block of the active annotation table
/// </summary>
let getArcMainColumn (excelTable: Table) (arcTable: ArcTable) (context: RequestContext) =
promise {
let! selectedBlock = getSelectedBuildingBlock excelTable context

let protoHeaders = excelTable.getHeaderRowRange()
let _ = protoHeaders.load(U2.Case2 (ResizeArray(["values"])))

do! context.sync().``then``(fun _ -> ())

let headers = protoHeaders.values.Item 0 |> Array.ofSeq |> Array.map (fun c -> c.ToString())

let arcTableIndices = (groupToBuildingBlocks headers) |> Array.ofSeq |> Array.map (fun i -> i |> Array.ofSeq)

let arcTableIndex =
let potResult = arcTableIndices |> Array.mapi (fun i c -> i, c |> Array.tryFind (fun (_, s) -> s = snd selectedBlock.[0]))
let result = potResult |> Array.filter (fun (_, c) -> c.IsSome) |> Array.map (fun (i, c) -> i, c.Value)
Array.tryHead result

let arcTableIndex, columnName =
if arcTableIndex.IsSome then
fst arcTableIndex.Value, snd (snd arcTableIndex.Value)
else failwith "Could not find a fitting arc table index"

let targetColumn =
let potColumn = arcTable.GetColumn arcTableIndex
if columnName.Contains(potColumn.Header.ToString()) then potColumn
else failwith "Could not find a fitting arc table index with matchin name"

return (targetColumn, arcTableIndex)
}

/// <summary>
/// Get the valid cell type for the conversion based on input cell type
/// </summary>
/// <param name="cellType"></param>
let getValidConversionCellType (cellType: CompositeCellDiscriminate option) =
Excel.run(fun context ->
promise {
let! result = tryGetActiveAnnotationTable context
match result with
| Result.Ok excelTable ->
let! arcTable = ArcTable.tryGetFromExcelTable(excelTable, context)
let! (arcMainColumn, _) = getArcMainColumn excelTable arcTable.Value context

return
match cellType with
| Some CompositeCellDiscriminate.Unitized -> Some CompositeCellDiscriminate.Term
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function must be reworked.

Here is what you do as far as i understand it:

goal: Return valid celltypes for conversion based on cellType

  1. get active table
  2. try parse to arctable, then take .Value without error handling
  3. get selected main column
  4. check if valid conversion

BUT;

  • if cellType is term return unitized without any checking against the selected building block
  • Similiar for cellType = Data

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed (hopefully)

| Some CompositeCellDiscriminate.Term -> Some CompositeCellDiscriminate.Unitized
| Some CompositeCellDiscriminate.Data -> Some CompositeCellDiscriminate.Text
| Some CompositeCellDiscriminate.Text ->
if (arcMainColumn.Header.isInput || arcMainColumn.Header.isOutput) && arcMainColumn.Header.IsDataColumn then
Some CompositeCellDiscriminate.Data
else None
| _ -> None

| Result.Error _ -> return None
}
)

/// <summary>
/// Select a building block, shifted by adaptedIndex from the selected building block
/// </summary>
Expand Down Expand Up @@ -1688,40 +1748,6 @@ let removeSelectedAnnotationBlock () =
}
)

/// <summary>
/// Get the main column of the arc table of the selected building block of the active annotation table
/// </summary>
let getArcMainColumn (excelTable: Table) (arcTable: ArcTable) (context: RequestContext)=
promise {
let! selectedBlock = getSelectedBuildingBlock excelTable context

let protoHeaders = excelTable.getHeaderRowRange()
let _ = protoHeaders.load(U2.Case2 (ResizeArray(["values"])))

do! context.sync().``then``(fun _ -> ())

let headers = protoHeaders.values.Item 0 |> Array.ofSeq |> Array.map (fun c -> c.ToString())

let arcTableIndices = (groupToBuildingBlocks headers) |> Array.ofSeq |> Array.map (fun i -> i |> Array.ofSeq)

let arcTableIndex =
let potResult = arcTableIndices |> Array.mapi (fun i c -> i, c |> Array.tryFind (fun (_, s) -> s = snd selectedBlock.[0]))
let result = potResult |> Array.filter (fun (_, c) -> c.IsSome) |> Array.map (fun (i, c) -> i, c.Value)
Array.tryHead result

let arcTableIndex, columnName =
if arcTableIndex.IsSome then
fst arcTableIndex.Value, snd (snd arcTableIndex.Value)
else failwith "Could not find a fitting arc table index"

let targetColumn =
let potColumn = arcTable.GetColumn arcTableIndex
if columnName.Contains(potColumn.Header.ToString()) then potColumn
else failwith "Could not find a fitting arc table index with matchin name"

return (targetColumn, arcTableIndex)
}

/// <summary>
/// Get the main column of the arc table of the selected building block of the active annotation table
/// </summary>
Expand Down Expand Up @@ -1826,6 +1852,9 @@ let addBuildingBlockAt (excelIndex: int) (newBB: CompositeColumn) (table: Table)
|> List.iteri (fun ci header -> ExcelHelper.addColumnAndRows (float (excelIndex + ci)) table header bodyValues.[ci] |> ignore)
}

/// <summary>
/// Get the cell type of the selected cell
/// </summary>
let getSelectedCellType () =
Excel.run(fun context ->
promise {
Expand All @@ -1838,17 +1867,13 @@ let getSelectedCellType () =
let! (arcMainColumn, _) = getArcMainColumn excelTable arcTable.Value context

if rowIndex > 0 then

let result =
return
match arcMainColumn with
| amc when amc.Cells.[(int rowIndex) - 1].isUnitized -> Some CompositeCellDiscriminate.Unitized
| amc when amc.Cells.[(int rowIndex) - 1].isTerm -> Some CompositeCellDiscriminate.Term
| amc when amc.Cells.[(int rowIndex) - 1].isData -> Some CompositeCellDiscriminate.Data
| amc when amc.Header.isInput && amc.Header.IsDataColumn && amc.Cells.[(int rowIndex) - 1].isFreeText -> Some CompositeCellDiscriminate.Text
| amc when amc.Header.isOutput && amc.Header.IsDataColumn && amc.Cells.[(int rowIndex) - 1].isFreeText -> Some CompositeCellDiscriminate.Text
| amc when amc.Cells.[(int rowIndex) - 1].isFreeText -> Some CompositeCellDiscriminate.Text
| _ -> None

return result
else return None

| Result.Error _ -> return None
Expand Down
55 changes: 31 additions & 24 deletions src/Client/Pages/BuildingBlock/CellConvertComponent.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,42 @@ open Feliz.Bulma
open OfficeInterop.Core
open Shared

type CellDiscriminateState = {
SelectedCellState: CompositeCellDiscriminate option
TargetCellState: CompositeCellDiscriminate option
} with
static member init = {
SelectedCellState = None
TargetCellState = None
}

module private CellConvertComponentHelpers =

let getSelectedCellType (setState: CompositeCellDiscriminate option -> unit) =
let setCellTypes (state: CellDiscriminateState) (setState: CellDiscriminateState -> unit) =
promise {
//Write function to access current state of selected excel cell excel
let! cellType = getSelectedCellType ()
let! selectedCellType = getSelectedCellType ()

setState cellType
}
if selectedCellType.IsSome then
let! targetCellType = getValidConversionCellType (selectedCellType)

let getTargetConversionType (cellType: CompositeCellDiscriminate option) =
if cellType.IsSome then
match cellType.Value with
| CompositeCellDiscriminate.Unitized -> Some CompositeCellDiscriminate.Term
| CompositeCellDiscriminate.Term -> Some CompositeCellDiscriminate.Unitized
| CompositeCellDiscriminate.Text -> Some CompositeCellDiscriminate.Data
| CompositeCellDiscriminate.Data -> Some CompositeCellDiscriminate.Text
else None
setState {
state with
SelectedCellState = selectedCellType
TargetCellState = targetCellType
}
}

type CellConvertComponent =

[<ReactComponent>]
static member Main () =

let (state: CompositeCellDiscriminate option), setState = React.useState(None)

//let (state: CompositeCellDiscriminate option), setState = React.useState(None)
//let (targetState: CompositeCellDiscriminate option), setTargetState = React.useState(None)
let (cellDiscriminateState, setCellDiscriminateState) = React.useState(CellDiscriminateState.init)
React.useEffectOnce(fun () ->
CellConvertComponentHelpers.getSelectedCellType setState
CellConvertComponentHelpers.setCellTypes cellDiscriminateState setCellDiscriminateState
|> Promise.start
)

Expand All @@ -43,27 +51,26 @@ type CellConvertComponent =
Bulma.button.button [
Bulma.color.isSuccess
prop.text "Refresh"
prop.onClick (fun _ -> CellConvertComponentHelpers.getSelectedCellType setState |> Promise.start)
prop.onClick (fun _ ->
CellConvertComponentHelpers.setCellTypes cellDiscriminateState setCellDiscriminateState |> Promise.start
)
]
Html.div (string state)
Html.div (string cellDiscriminateState.SelectedCellState)
]
Bulma.buttons [
Bulma.button.button [
if state.IsSome then
if cellDiscriminateState.TargetCellState.IsSome then
Bulma.color.isSuccess
prop.disabled false
prop.text $"Convert {state.Value} to"



prop.text $"Convert {cellDiscriminateState.SelectedCellState.Value} to"
else
Bulma.color.isDanger
prop.disabled true
prop.text $"Unconvertible"
prop.onClick (fun _ ->
CellConvertComponentHelpers.getSelectedCellType setState |> ignore
CellConvertComponentHelpers.setCellTypes cellDiscriminateState setCellDiscriminateState |> Promise.start
convertBuildingBlock () |> Promise.start)
]
Html.div (string (CellConvertComponentHelpers.getTargetConversionType state))
Html.div (string cellDiscriminateState.TargetCellState)
]
]