Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
SkyChen91 opened this issue Feb 13, 2019 · 9 comments

Comments

@SkyChen91
Copy link

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

Give an example?
Thanks!

@govert
Copy link
Member

govert commented Feb 13, 2019

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?

@SkyChen91
Copy link
Author

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
Copy link
Author

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

@SkyChen91
Copy link
Author

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

@govert
Copy link
Member

govert commented Feb 15, 2019

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

@SkyChen91
Copy link
Author

Sorry,I have read this article, but still do not understand。
And I also read the source code. Find The Function [ public` static IEnumerable ProcessParameterConversions(this IEnumerable 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.

@lbnewre
Copy link

lbnewre commented Feb 10, 2021

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!

@govert
Copy link
Member

govert commented Feb 10, 2021

@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.

@andrewkittredge
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants