在function上编程Excel代码

我正在用F#和Excel-DNA重写我的Excel VBA加载项。 下面的Formula1工作。 它遍历当前选定的单元格,并将trim应用于每个单元格值。

Formula2是我在应用Functional和F#概念方面失败的尝试。 我不确定如何将范围返回到Excel。 公式必须返回一个单位。

有人可以帮我吗?

Formula1(作品):

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() 

公式2(不起作用):

 let Trim2 (values:obj[,]) = values |> Seq.cast<obj> |> Seq.map( fun x -> x.ToString().Trim() ) 

有人询问了返回单元或调用函数的原因。 那是下面。

 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 | _ -> () 

不知道你在那里试图完成什么。 为什么一个公式需要修剪,而不是像一个值?

另一个障碍是,我一直在晚做绑定,以避免Interop库依赖。 以下是后期绑定的dynamic运算符的实现 。

 let usedRange = workSheet?UsedRange usedRange?Formula |> box |> function | :? (obj[,]) as a -> a | o -> Array2D.createBased 1 1 1 1 o // Single cell sheet |> Array2D.iteri (fun ij formula -> let cell = usedRange?Cells(i, j) cell?Formula <- (string formula).Trim() ) 

尝试

 let Trim2 (values:obj[,]) = values |> Array2D.map(fun x-> box(x.ToString().Trim())) 

你在哪里获得“F1”中的Rangetypes?