f# - Excel DNA UDF obtain unprocessed values as inputs
问题描述
I have written several helper functions in F# that enable me to deal with the dynamic nature of Excel over the COM/PIA interface. However when I go to use these functions in an Excel-DNA UDF they do not work as expected as Excel-DNA is pre-processing the values in the array from excel.
e.g. null
is turned into ExcelDna.Integration.ExcelEmpty
This interferes with my own validation code that was anticipating a null
. I am able to work around this by adding an additional case to my pattern matching:
let (|XlEmpty|_|) (x: obj) =
match x with
| null -> Some XlEmpty
| :? ExcelDna.Integration.ExcelEmpty -> Some XlEmpty
| _ -> None
However it feels like a waste to convert and then convert again. Is there a way to tell Excel-DNA not to do additional processing of the range values in a UDF and supply them equivalent to the COM/PIA interface? i.e. Range.Value XlRangeValueDataType.xlRangeValueDefault
EDIT:
I declare my arguments as obj
like this:
[<ExcelFunction(Description = "Validates a Test Table Row")>]
let isTestRow (headings: obj) (row: obj) =
let validator = TestTable.validator
let headingsList = TestTable.testHeadings
validateRow validator headingsList headings row
解决方案
I have done some more digging and @Jim Foye's suggested question also confirms this. For UDF's, Excel-DNA works over the C API rather than COM and therefore has to do its own marshaling. The possible values are shown in this file:
The reason to use ExcelEmpty
(the user supplied an empty cell) is that for a UDF, the argument can also be ExcelMissing
(the user supplied no argument) which might both be reasonably null
and there is a need to disambiguate.
I will adjust my pattern matching to be compatible with both the COM marshaling and the ExcelDNA marshaling.
推荐阅读
- python - Python并发第一个结果结束等待尚未完成的结果
- amazon-cloudwatch - CloudWatch Logs Insight - 解析 2 个元素并在一个字段下分组
- unity3d - 为什么 NetworkIdentity 组件使我的游戏对象不可见?(统一)
- socket.io - 对 AWS 弹性缓存的套接字 IO 访问失败
- java - 从 localhost:4200 访问 localhost:8080 的 XMLHttpRequest 已被 CORS 策略阻止
- python - numpy中向量的自相关
- git - Git 和 GitHub Desktop 是完全独立的应用程序吗?
- python - 在 JSON 字段中使用第一个键进行注释
- go - 为什么这个 go slice 的长度是 4,为什么输出在 slice 中有空间?
- python - 如何用字符和捕获组替换数据框列中的多个单词