ExcelDna icon indicating copy to clipboard operation
ExcelDna copied to clipboard

Support for Namespace Prefix

Open jointy44 opened this issue 4 years ago • 3 comments

I'm trying to migrate an old COM add-in to Excel DNA but there are several thousands of worksheets (from dozens of users) who have the old COM add-in. After migrating ALL the functions to Excel DNA (by retaining the function name and signatures) the sheets built with old COM add-in shows the functions as .customFunction(), while Excel DNA add-in shows the functions as customFunction().

Is there any way in Excel DNA to mimic the function (like the COM add-in function) with custom namespace prefix to every single function?

*Asking every user to update all their worksheets is one hell of route that I'm trying to avoid :smile:

jointy44 avatar Jun 02 '20 23:06 jointy44

Hi @jointy44 You can already change the name of the function using the ExcelFunctionAttribute on each function:

public static class MyExcelDnaFunctions
{
    [ExcelFunction(Name = "MyCompany.MyFunction")]
    public static double SumValues(double x, double y)
    {
        return x + y;
    }
}

image


If you'd like to do that dynamically for several functions, instead of manually adding the attribute on each one, you can use ExcelDna.Registration.

Usage examples:

  • https://github.com/Excel-DNA/Registration/tree/master/Source/Samples
  • https://github.com/augustoproiete/serilog-sinks-exceldnalogdisplay/tree/master/sample/SampleAddIn
  • https://stackoverflow.com/a/60079589

Does that work for you?

augustoproiete avatar Jun 03 '20 03:06 augustoproiete

Awesome - Thank you @augustoproiete for your quick response and examples. These should most likely solve my problem. I will spend next few days making changes and test some set of sheets to ensure there is no clash, but I'm more optimistic than doubtful about your suggestion.

jointy44 avatar Jun 03 '20 03:06 jointy44

@jointy44 and @augustoproiete

This topic is a bit more tricky than just setting the names of the Excel-DNA functions.

There is another issue here. "Automation add-ins" are COM add-ins that provide UDF functions to Excel. When a sheet uses such a UDF, the ProgId information from the Automation add-in is saved with the sheet into the .xlsx file. So the stored representation of =MyFunction() might be MyLibrary.MyFunction(). Excel will only show you the ProgId part if the library could not be loaded.

On the other hand, any .xll add-in (including those built with Excel-DNA) which defines a function called =MyFunction() will have it stored as _xll.MyFunction(). Excel will never display the _xll. part, and will show #NAME? if the add-in is not loaded.

The end result is that you can't replace an automation add-in with an .xll add-in in such a way that the formulas don't need to be re-entered. Although you can set the name of your Excel-DNA function to MyLibrary.MyFunction, Excel will still not think of this is the same functions as the one from the automation add-in.

The only way around is to force Excel to re-interpret the formulas, by re-entering them (e.g. pressing F2 + Enter on every cell) or to run a macro that has the same effect. Doing a workbook-wide search-and-replace is normally the easiest.

govert avatar Jun 03 '20 08:06 govert