我希望复制Bloomberg BDH的行为。
BDH发起了一个网络请求并写入一个数组(但未返回数组样式)。在这个网络请求期间,函数返回“#N/A Requesting”。 当网络请求完成后,BDH()函数将把数组结果写入工作表。
例如,在ExcelDNA中,我成功地使用线程写入了工作表。
如果您在DNA文件中使用下面的代码,则
=WriteArray(2;2)
的结果将是
第1行 > #N/A Requesting Data (0,1)
第2行 > (1,0) (1,1)
最后一个问题是用值替换#N/A Requesting Data
并复制公式。
当您取消注释//xlActiveCellType.InvokeMember("FormulaR1C1Local"时,您接近结果,但没有正确的行为
文件.dna
<DnaLibrary Language="CS" RuntimeVersion="v4.0">
<![CDATA[
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using ExcelDna.Integration;
public static class WriteForXL
{
public static object[,] MakeArray(int rows, int columns)
{
if (rows == 0 && columns == 0)
{
rows = 1;
columns = 1;
}
object[,] result = new string[rows, columns];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
result[i, j] = string.Format("({0},{1})", i, j);
}
}
return result;
}
public static object WriteArray(int rows, int columns)
{
if (ExcelDnaUtil.IsInFunctionWizard())
return "Waiting for click on wizard ok button to calculate.";
object[,] result = MakeArray(rows, columns);
var xlApp = ExcelDnaUtil.Application;
Type xlAppType = xlApp.GetType();
object caller = xlAppType.InvokeMember("ActiveCell", BindingFlags.GetProperty, null, xlApp, null);
object formula = xlAppType.InvokeMember("FormulaR1C1Local", BindingFlags.GetProperty, null, caller, null);
ObjectForThread q = new ObjectForThread() { xlRef = caller, value = result, FormulaR1C1Local = formula };
Thread t = new Thread(WriteFromThread);
t.Start(q);
return "#N/A Requesting Data";
}
private static void WriteFromThread(Object o)
{
ObjectForThread q = (ObjectForThread) o;
Type xlActiveCellType = q.xlRef.GetType();
try
{
for (int i = 0; i < q.value.GetLength(0); i++)
{
for (int j = 0; j < q.value.GetLength(1); j++)
{
if (i == 0 && j == 0)
continue;
Object cellBelow = xlActiveCellType.InvokeMember("Offset", BindingFlags.GetProperty, null, q.xlRef, new object[] { i, j });
xlActiveCellType.InvokeMember("Value", BindingFlags.SetProperty, null, cellBelow, new[] { Type.Missing, q.value[i, j] });
}
}
}
catch(Exception e)
{
}
finally
{
//xlActiveCellType.InvokeMember("Value", BindingFlags.SetProperty, null, q.xlRef, new[] { Type.Missing, q.value[0, 0] });
//xlActiveCellType.InvokeMember("FormulaR1C1Local", BindingFlags.SetProperty, null, q.xlRef, new [] { q.FormulaR1C1Local });
}
}
public class ObjectForThread
{
public object xlRef { get; set; }
public object[,] value { get; set; }
public object FormulaR1C1Local { get; set; }
}
}
]]>
</DnaLibrary>
@Govert
BDH已成为金融行业的标准。即使是Ctrl+Shift+Enter,人们也不知道如何操作数组。
BDH函数使彭博社如此受欢迎(对路透社不利)。
但我会考虑使用您的方法或RTD。
感谢您在Excel DNA中的所有工作。
=BDH("CAC Index";"PX_LAST";"20/05/2010";"20/05/2012")
,则在单元格(0,0)中会有一个值,在公式=BDH("CAC Index";"PX_LAST";"20/05/2010";"20/05/2012";"cols=2;rows=478")
中会有多个值。BDH遵守Excel的所有标准,除非数据覆盖现有工作表中的数据。 - Farandole