Registration icon indicating copy to clipboard operation
Registration copied to clipboard

How to implement Optional parameters (with default values) and Nullable parameters

Open SkyChen91 opened this issue 6 years ago • 9 comments

how to use this function? Give an example? •Optional parameters (with default values), 'params' parameters and Nullable parameters

Give an example? Thanks!

SkyChen91 avatar Feb 13 '19 08:02 SkyChen91

There are lots of parameter-related examples here: https://github.com/Excel-DNA/Registration/blob/master/Source/Samples/Registration.Sample/ParameterConversionExamples.cs

Where are you stuck?

govert avatar Feb 13 '19 10:02 govert

I use this formula in Excel and can't return the default value ""Hello World!". [ExcelFunction] public static string dnaOptionalString(string optOptTest = "Hello World!") { return optOptTest; } dnaoptionstring

Where is the mistake. What do I need to configure?

SkyChen91 avatar Feb 14 '19 02:02 SkyChen91

Do I need to register these functions separately in a special way?

SkyChen91 avatar Feb 14 '19 02:02 SkyChen91

I have a another question, how to get the Worksheet where Range is located via ExcelReference?

SkyChen91 avatar Feb 15 '19 07:02 SkyChen91

To use the Excel-DNA Registration extensions (which support optional parameters) you need to change your project to use 'explicit registration'. See the walkthrough here: https://github.com/Excel-DNA/Registration/blob/master/README.md

govert avatar Feb 15 '19 09:02 govert

Sorry,I have read this article, but still do not understand。 And I also read the source code. Find The Function [ public` static IEnumerable<ExcelFunctionRegistration> ProcessParameterConversions(this IEnumerable<ExcelFunctionRegistration> registrations, ParameterConversionConfiguration conversionConfig); ] Should I use this extension convert the function parameter before registration. Like This: config I do not know How to set the ParameterConversionConfiguration.

SkyChen91 avatar Feb 16 '19 04:02 SkyChen91

Govert, thanks a lot for your work, but this part is not clear enough. Are optional arguments still a work in progress? (you're mentioning here some casting issues: https://github.com/Excel-DNA/Registration/blob/master/Source/Samples/Registration.Sample/ParameterConversionExamples.cs

Otherwise, could you amend the documentation and give some examples? I.e. in the walkthrough you're showing how to update a help page by adjusting that specific argument generally for functions. I think what SkyChen91 is looking for (and so am I) is a neat way to set arguments/parameters for individual functions as a default to the value specified in that function.

E.g. When setting defaults as in the example above [ExcelFunction] public static string dnaOptionalString(string optOptTest = "Hello World!") { return optOptTest; } Value of optOptTest stays empty "" and can't be updated to "Hello World!"?. We could check for emptyness as a workaround and update it that way below inside the function, but am I right to assume that the information "Hello World!" in this case is only overhead yet and can't be retrieved inside the function? Or how would you do that? If it's possible, a simple example would be great!

lbnewre avatar Feb 10 '21 15:02 lbnewre

@lbnewre When I run the Registration sample, the optional string function works fine, and I get see the default value returned to Excel when calling it as =dnaOptionalString() from a sheet.

Have you tested the Registration sample? Are you using the Registration extension in your add-in, according to the walkthrough here: https://github.com/Excel-DNA/Registration/blob/master/README.md (You need a few extra steps in your add-in like ExplicitRegistraiton=true, and dealing with the RegisterFunctions explicitly etc.)

I think there are some quirks with the conversions in some cases, but optional strings with defaults work in the Registration sample on my computer.

govert avatar Feb 10 '21 15:02 govert

the samples have been moved to https://github.com/Excel-DNA/Samples/blob/master/Registration.Sample/ParameterConversionExamples.cs

andrewkittredge avatar May 02 '23 13:05 andrewkittredge