我正在使用Excel-DNA在Excel中開發一些UDF。從Excel傳遞到我的UDF的一個參數是一個範圍。使用特定範圍時,UDF可正常工作,如「A1:C50」。下面是我的函數定義的一個示例:如何針對UsedRange剪輯ExcelReference?
[ExcelCommand()]
public static object CalcSMA(object[,] range, int num_points) {
...
}
不過,我得到時整列範圍傳遞,如「走出內存」錯誤「A:C」。我可以通過設置參數屬性AllowReference = TRUE避免錯誤和更改參數類型爲對象,如下面的例子:
[ExcelCommand()]
public static object CalcSMA([ExcelArgument("Range", AllowReference=true)]object range, int num_points) {
ExcelReference xref = (ExcelReference)range;
...
}
但現在我堅持想多行如何實際需要的UDF。我可以嘗試迭代工作表中的所有行,但這非常低效。有沒有辦法將ExcelReference(xref)與使用的範圍進行剪輯?我想避免使函數volatile(IsMacroType = true),但如果需要的話會這樣做。
也看到https://groups.google.com/d/topic/exceldna/GXs6xbnSTKY/discussion - 使用UsedRange在Excel-DNA中更容易一些,因爲您可以通過調用「ExcelDnaUtil.Application」來獲得COM對象模型應用程序對象。 – Govert
感謝您的快速響應。如果您按照您的建議使用了緩存後的AfterCalculate事件,那麼在打開工作簿之後,是否還需要在第一次計算期間迭代行? –
我陷阱工作簿打開事件最初填充緩存。 –