I want to copy the behavior of Bloomberg BDH.
BDH makes a web request and writes an array (but does not return an array style). During this web request, the function returns "# N / A Requesting". When the web request is complete, the BDH () function writes the result of the array to the worksheet.
For example, in ExcelDNA, I manage to write on a worksheet with a stream.
Result, if you use the code below in the DNA file, the result
= WriteArray (2; 2)
will be
Line 1> #N/A Requesting Data (0,1)
Row 2> (1,0) (1,1)
The latest release is to replace #N/A Requesting Data with a value and copy the formula. When you uncomment //xlActiveCellType.InvokeMember ("FormulaR1C1Local", you are close to the result, but you do not have the correct behavior
.Dna file
<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 {
@To Govert
BDH has become the standard in the financial industry. People do not know how to manipulate an array (even Ctrl + Shift + Enter).
BDH is the feature that made Bloomberg so popular (to the detriment of Reuters).
However, I will think about using your method or RTD.
Thank you for your work in Excel DNA.