I am rewriting my Excel VBA add-in with F# and Excel-DNA. Formula1 below works. It iterates through the currently selected cells and applies trim to each cells value.
Formula2 is my failed attempt at applying Functional and F# concepts. I'm unsure how to return the range to Excel. The formula must return a unit.
Could someone help me?
Formula1 (Works):
let Trim (rng:Range) =
for cell in rng.Cells do
let cel = cell :?> Range
if not (cel :? ExcelEmpty) then
cel.Formula <- cel.Formula.ToString().Trim()
Formula2 (Does not work):
let Trim2 (values:obj[,]) =
values
|> Seq.cast<obj>
|> Seq.map( fun x -> x.ToString().Trim() )
Some have asked about the reason for returning a unit or the calling function. That is below.
type public MyRibbon() =
inherit ExcelRibbon()
override this.GetCustomUI(ribbonId) =
"bunch of xml here"
member this.OnButtonPressed (control:IRibbonControl) =
let app = ExcelDnaUtil.Application :?> Application
let rng = app.Selection :?> Range
let rng2 = app.Selection :?> obj
match control.Id with
| "AddIfError" -> RibFuncs.RangeFuncs.AddIfError app rng
| "Trim" -> RibFuncs.RangeFuncs.Trim rng
|> ignore
| _ -> ()
how to return the range to Excelandthe formula must return a unitformulavalue you create withlet formula = ...isn't actually used. If you add--warnon:1182to theOther Flagssetting in theBuildtab of your project properties, the F# compiler will warn you about unused values (which is extremely useful for avoiding small mistakes).