Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

powershell Call method with optional arguments

I have an excel vba code that finds a particular cell in a sheet. It uses the Find method form the excel libraries. Here is the code

objRange.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows)

I need to do the same thing in powershell. But this method has a total of 9 arguments. How to ignore the other optional arguments in powershell. Something like this?

$range.Find("*", "", "", "", $xlByRows, $xlPrevious, "", "", "")

Here is the documentation of Range.Find Method

like image 842
Ammar Avatar asked Jul 05 '13 07:07

Ammar


People also ask

How do I use optional parameters in PowerShell?

By default, PowerShell parameters are optional. When a user does not submit arguments to a parameter, PowerShell uses its default value. If no default value exists, the parameter value is $null. This is not always desired.

How do you indicate optional arguments?

To indicate optional arguments, Square brackets are commonly used, and can also be used to group parameters that must be specified together. To indicate required arguments, Angled brackets are commonly used, following the same grouping conventions as square brackets.

What are optional parameters added?

As the name suggests optional parameters are not compulsory parameters, they are optional. It helps to exclude arguments for some parameters. Or we can say in optional parameters, it is not necessary to pass all the parameters in the method. This concept is introduced in C# 4.0.


1 Answers

$null doesn't work, but according to this answer you can use [Type]::Missing:

$default = [Type]::Missing
$xl.Cells.Find("*", $default, $default, $default, $xlByRows, $xlPrevious,
               $default, $default, $default)
like image 169
Ansgar Wiechers Avatar answered Oct 06 '22 11:10

Ansgar Wiechers