Skip to content

Latest commit

History

192 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Excel-DNA Registration Helper

This library implements helper functions to assist and modify the Excel-DNA function registration, by applying various transformations before the functions are registered.

The following transformations have been implemented:

Generation of wrapper functions for:

  • Functions returning Task or IObservable as asynchronous or RTD-based functions (including F# Async functions)
  • Optional parameters (with default values), 'params' parameters and Nullable parameters
  • Range parameters in Visual Basic functions

Examples of general function transformations:

  • Logging / Caching / Timing handlers
  • Suppress in Function Arguments dialog

If you've previously used the CustomRegistration library, note that I've renamed and rearranged the project source, and renamed the output assembly from ExcelDna.CustomRegistration to ExcelDna.Registration. The last state of the project before the large-scale rearrangement is marked by the git tag CustomRegistration_Before_Rename, and can be retrieved from the release tab on GitHub.

Getting Started

To make a simple add-in that uses the Excel-DNA Registration extension to dynamically update the HelpTopic information for function registrations:

  1. Create a new C# Class Library (.NET Framework) project, e.g. called RegistrationHelpUpdate.
  2. Open the Package Manager Console.
  3. PM> Install-Package ExcelDna.AddIn
  4. PM> Install-Package ExcelDna.Registration
  5. Edit the RegistrationHelpUpdate-AddIn.dna file to add the ExplicitRegistration flag to the function library, and add the reference to the ExcelDna.Registration for packing:
<DnaLibraryName="RegistrationHelpUpdate Add-In"RuntimeVersion="v4.0">
<ExternalLibraryPath="RegistrationHelpUpdate.dll"ExplicitExports="false"ExplicitRegistration="true"LoadFromBytes="true"Pack="true" />
<ReferencePath="ExcelDna.Registration.dll"Pack="true" />
</DnaLibrary>
  1. Insert the following code:
usingSystem.Linq;usingExcelDna.Integration;usingExcelDna.Registration;namespaceRegistrationHelpUpdate{publicclassAddIn:IExcelAddIn{publicvoidAutoOpen(){RegisterFunctions();}publicvoidAutoClose(){}publicvoidRegisterFunctions(){// There are various options for wrapping and transforming your functions// See the Source\Samples\Registration.Sample project for a comprehensive example// Here we just change the attribute before registering the functionsExcelRegistration.GetExcelFunctions().Select(UpdateHelpTopic).RegisterFunctions();}publicExcelFunctionRegistrationUpdateHelpTopic(ExcelFunctionRegistrationfuncReg){funcReg.FunctionAttribute.HelpTopic="http://www.bing.com";returnfuncReg;}}publicclassFunctions{[ExcelFunction(HelpTopic="http://www.google.com")]publicstaticobjectSayHello(){return"Hello!!!";}}}
  1. Press F5 to compile and start in Excel.
  2. Start typing =SayHello( in a cell and press the Fx button to open the function wizard. Check that the HelpTopic has been updated during registration to open Bing instead of Google.

See the add-ins in the Samples directory to see various registration update extensions.

Step-by-step for Visual Basic

Once you have a basic Visual Basic add-in working.

  1. From the NuGet Package Manager Console, (or the Manage NuGet Packages dialog): PM> Install-Package ExcelDna.Registration.VisualBasic

  2. Fix up your .dna file by changing to ExplicitRegistration for your library registration, and packing the extra ExcelDna.Registration libraries:

 <DnaLibraryName="MyVisualBasic Add-In"RuntimeVersion="v4.0" > <ExternalLibraryPath="MyVisualBasic.dll"ExplicitRegistration="true"LoadFromBytes="true"Pack="true" /> <ReferencePath="ExcelDna.Registration.dll"Pack="true" /> <ReferencePath="ExcelDna.Registration.VisualBasic.dll"Pack="true" /> </DnaLibrary> 

3.Perform the explicit registration in your AutoOpen by calling ExcelDna.Registration.VisualBasic.PerformDefaultRegistration():

ImportsExcelDna.IntegrationImportsExcelDna.Registration.VisualBasicPublicClassMyAddInImplementsIExcelAddInPublicSubAutoOpen()ImplementsIExcelAddIn.AutoOpen' Code here will run eery time the add-in is loaded PerformDefaultRegistration()EndSubPublicSubAutoClose()ImplementsIExcelAddIn.AutoClose' Code in here will run when the add-in is removed in the Add-Ins dialog, ' but not when Excel closes normally EndSubEndClassPublicFunctiondnaTestParams(date1AsDate,ParamArrays()AsString)AsStringReturns.LengthEndFunctionPublicFunctiondnaTestOptional(date1AsDate,OptionalheadAsBoolean=True)AsStringReturnhead.ToString()EndFunction

Sample Projects

Sample projects for this library can be found in the Excel-DNA Samples repository.

Registration [Error] Repeated function name...

If you receive this error when opening your Excel addin, you need to add ExplicitRegistration="true" to the <ExternalLibrary Path="MyAddin.dll"... command in your .dna file.

About

Registration helpers for Excel-DNA

Resources

Stars

37 stars

Watchers

13 watching

Forks

Releases

Packages

Used by

Contributors

Languages