I'm unable to write my REST API response in Excel using ExcelDNA in C#. It is working fine with reading values from specific column.
xlCell[i, 1] = "TEST"; // fails with System.Runtime.InteropServices.COMException: 'Exception from HRESULT: 0x800A03EC'
currentSheet.Cells[i, 2].Value = "TEST"; // same issue
Excel reading code --
Excel.Range xlCell;
Excel.Application xlapp = (Excel.Application)ExcelDnaUtil.Application;
xlapp.Visible = true;
Worksheet currentSheet;
xlCell = xlapp.ActiveCell;
Excel.Workbook wbook = xlapp.ActiveWorkbook;
currentSheet = wbook.ActiveSheet;
Please note that same code piece is working fine with VSTO addin project (through ribbon button event click) but not in .net class library (for UDF).
But I need UDF support explicitly that's the reason , went ahead with .net class library project with (.xll)
Appreciate if please can someone help.
Thanks,
Aniket Karhadkar
Hi Aniket,
Excel does not allow you to write to a cell using during a calculation (i.e. from a UDF being evaluated from a cell).
The idea is that your UDF returns a value, which Excel will then display and use as the cell value.
So you’d have:
public static object GetMyData(string info)
{
// Calculate or get the result
var result = info + " – Result”;
return result;
}
And not the macro version where your write to a cell:
public static void PutMyDataIntoActive(string info)
{
// Calculate or get the result
// Write to a cell using the COM object model
}
There is some support in Excel-DNA for making async functions, if your back-end calls are slow.
But for a start, I suggest just doing a synchronous call, and caching the result into a local Dictionary to speed things up when recalculating.
Once that works you can refine more.
-Govert
--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to exceldna+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/exceldna/7273cea9-fe0f-4bec-930d-517b9c943aebn%40googlegroups.com.
Hi Aniket,
Writing to a cell from a UDF is a bad idea – one problem is that the active cell might not be the caller of the UDF, another is that the dependency tree the Excel manages can’t track things correctly.
If you have a version of Excel which supports Dynamic Arrays – Excel 2022 or an Office 365 subscription – then you can return a large array of data from your UDF and it will expand and work correctly on the sheet.
To view this discussion on the web visit https://groups.google.com/d/msgid/exceldna/ab632fc6-c855-4c8a-905d-c444f45c97fen%40googlegroups.com.
Hi Aniket,
> Is there any way you can suggest to have UDF support which will eventually write large REST API data to Excel sheet ?
If you have a version of Excel which supports Dynamic Arrays – Excel 2022 or an Office 365 subscription – then you can return a large array of data from your UDF and it will expand and work correctly on the sheet.
To view this discussion on the web visit https://groups.google.com/d/msgid/exceldna/1a24acc1-4c40-42e0-bc62-8e70a34733f5n%40googlegroups.com.