Вопрос

I have the following function:

Function Color-Cells {
    Param (
        [Parameter(
            Mandatory = $true,
            Valuefrompipeline = $true)]
        [String]$Range,
        [Parameter(
            Mandatory = $true)]
        [System.Drawing.Color]$Color
    )
    $Global:WorkSheet.range($Range).Interior.color = $Color
}#End Function

Which I can call like this:

Color-Cells -Range "A1" -Color LightBlue

Without any problems.

I would like to be able to call the function like this:

Color-Cells -Range "A1" -Color FromArgb(217,217,217)

to get more granular with the color selection of a cell, however when I do that, I get the following error:

Color-Cells : Cannot process argument transformation on parameter 'Color'. Cannot convert value 
"FromArgb(217,190,238)" to type "System.Drawing.Color". Error: "FromArgb(217 is not a valid value for Int32."
At line:70 char:32
+ Color-Cells -Range "A1" -Color "FromArgb(217,217,217)"
+                                ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Color-Cells], ParameterBindingArgumentTransformationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Color-Cells

I have tried calling the function like this:

  1. "FromArgb(217,217,217)"
  2. 'FromArgb(217,217,217)'
  3. `"FromArgb(217,217,217)`"

And they all produce errors. I'm not very familiar with .Net, so I'm not sure how to fix this. My end goal is to create a function that can color a range of cells based on either the [System.drawing.color] known name or the RGB value.

Edit for clarity:

The TypeName is specific as the parameter type in the function. When I call the function Color-cells the -Color parameter is the type [System.Drawing.Color]. When I call the function using the static property (Color-Cells -Color LightBlue for example) , it works just find. However, when I try to invoke a method (Color-Cells -Color FromArgb(217,217,217) for example), that's when I get the error message. Is there a way to pass just the method into the function?

Per Mike's answer I can success fully called the function like this:

Color-Cell -Range "A1" -Color $([System.Drawing.Color]::FromArgb(217,190,238))

However, I would like to call the function (using a method) without adding the $() around the command and without the [System.Drawing.Color]:: like I can with just the properties (LightBlue for example).

Это было полезно?

Решение

To call static methods, use [TYPENAME]::MethodName(args).

So do to what you're wanting, use this:

[System.Drawing.Color]::FromArgb(217,217,217)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top