Abstracting/Consolidating Duplicated Argument Descriptors (IntelliSense)

138 views
Skip to first unread message

Jeffrey Zenger

unread,
Feb 14, 2024, 3:10:31 PMFeb 14
to Excel-DNA
Hi, I am brand new to both Excel DNA and .NET languages, as well as relatively new to programming in general (1 year VBA experience). It has been quite a leap for me to try to take a stab at this and I keep getting lost in terminology when I check the Docs and StackOverflow, but I am trying to persevere!

I am now working in C# and have several functions (methods) that mostly all include the same 7 input arguments and definitions. I would ideally like to declare and decorate the arguments/descriptions in one central location by either using a separate Class or wrapper etc (or any other way really). For example, I use the following arguments, along with a handful of others in over a dozen formulas:

public static int MyFunction1(
 [ExcelArgument(Name = "Arg1", Description = "arg1 Description")] int arg1,
 [ExcelArgument(Name = "Arg2", Description = "arg2 Description")] int arg2,
 [ExcelArgument(Name = "Arg3", Description = "arg3 Description")] string arg3)

I have a specific pattern defined for the input arguments which remains the same, and if I include all 7 arguments with descriptors for every function (method), my code is very quickly going to become both bloated and unmaintainable from all the descriptions.

In my attempts so far, I have tried creating a separate class CommonArguments, but that failed because the ExcelArgument appears to only be allowed to be used when declaring the variables inside a new method. I have also tried at least creating variables for the common descriptions, so that I could shorten each line as well as consolidate the duplicate descriptions to one location by setting (...Description = arg1DescVariable...). However, ExcelArgument is requiring me to use a constant value for the description instead of a variable.

Is this even possible, or does anyone have any recommendations?? 

Thank you so much!

Terry Aney

unread,
Jul 11, 2024, 10:03:19 AM (11 days ago) Jul 11
to Excel-DNA
Have you looked at creating an '.intellisense.xml' file? I do so with a post build script, but you could generate this for each `ExcelFunction` and an <Argument/> for each `ExcelArgument` based on their name or something.

<?xml version="1.0" encoding="utf-8"?>
  <FunctionInfo>
    <Function Name="BTR415DBMax" Description="A replacement function for the Cfgena.xla!AnnualLimits() function call with '415DBLimit' name parameter in favor of explicit function name (reduces parameter typo errors).  Returns an integer value representing maximum annual defined benefit payable at Social Security normal retirement age under §415(b).">
      <Argument Name="year" Description="The effective year for the limit, if this parameter is 0 then function will return current year unrounded limit." />
      <Argument Name="rateProj" Description="The increase rate at which to project the limit." />
    </Function>
  </FunctionInfo>
</IntelliSense>

That would take care of IntelliSense.  For the Function Wizard, you'll have to do some C# as far as I know...something like this would get you there.  Obviously for both cases, you could have a single source of documentation that is accessible (embedded xml/json file that can be used in both situations)?

        ExcelRegistration
            .GetExcelFunctions()
            .ProcessParamsRegistrations().Select( UpdateParameterDocumentation )
            .RegisterFunctions();
    private static ExcelFunctionRegistration UpdateParameterDocumentation( ExcelFunctionRegistration funcReg )
    {
        foreach( var a in funcReg.ParameterRegistrations )
        {
            a.ArgumentAttribute.Description = a.ArgumentAttribute.Name switch
            {
                "year" => "The effective year for the limit, if this parameter is 0 then function will return current year unrounded limit."
                "rateProj" => "The increase rate at which to project the limit."
                _ => "Some Description based on argument name."
            };
        }
        return funcReg;
    }
Reply all
Reply to author
Forward
0 new messages