3 回答

TA貢獻1836條經驗 獲得超5個贊
您應該直接從輸入參數中獲取值,而不是獲取 Range COM 對象。這樣做也更有效率。
您的簡單函數可能如下所示:
public static object Concat2(object[,] values)
{
string result = "";
int rows = values.GetLength(0);
int cols = values.GetLength(1);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
object value = values[i, j];
result += value.ToString();
}
}
return result;
}
通常,您希望檢查value對象的類型,并基于此做一些不同的事情。object[,]從 Excel-DNA 傳遞的數組可以包含以下類型的項目:
double
string
bool
ExcelDna.Integration.ExcelError
ExcelDna.Integration.ExcelEmpty
ExcelDna.Integration.ExcelMissing
(如果不帶參數調用該函數,則為=Concat2()
)。
如果您將簽名更改為具有單個類型的參數object
(而不是object[,]
),如下所示:
public static object Concat2(object value)
然后,根據函數的調用方式,您可能會獲得上述類型之一作為 the ,value
或者您可能獲得一個object[,]
數組作為 the value
,因此在進行迭代之前,您的類型檢查看起來會有所不同。

TA貢獻1840條經驗 獲得超5個贊
在我的 F# 插件中,我有一個執行此操作的函數(我使用此函數主要是為了提取日期的顯示值):
[<ExcelFunction(Description="Returns what is currently displayed as text.", IsMacroType=true)>]
let DISPLAYEDTEXT ([<ExcelArgument(Description="Cell", AllowReference=true)>] rng : obj) =
app().get_Range(XlCall.Excel(XlCall.xlfReftext, rng, true)).Text
應用程序在哪里:
let app()= ExcelDnaUtil.Application :?> Excel.Application

TA貢獻1946條經驗 獲得超3個贊
這個怎么樣?
[ExcelFunction(IsMacroType = true)]
public static double GetBackColor([ExcelArgument(AllowReference=true)] object cell)
{
ExcelReference rng = (ExcelReference)cell;
Excel.Range refrng = ReferenceToRange(rng);
return refrng.Interior.Color;
}
這是輔助函數
private static Excel.Range ReferenceToRange(ExcelReference xlRef)
{
Excel.Application app = (Excel.Application)ExcelDnaUtil.Application;
string strAddress = XlCall.Excel(XlCall.xlfReftext, xlRef, true).ToString();
return app.Range[strAddress];
}
- 3 回答
- 0 關注
- 393 瀏覽
添加回答
舉報