【问题标题】:Using null in a string在字符串中使用 null
【发布时间】:2018-07-23 07:36:43
【问题描述】:

我发现在 PowerShell 中对字符串使用 $null 是不可能的。虽然我使用了以下解决方法,但我想知道是否有更好的方法?

基本上我需要字符串$null''(空)、'some string string' 的 3 种状态。

  • 第一个表示 - 不执行。
  • 第二个 - 明文
  • 用新值替换文本

以下是我选择开关-ClearText 的解决方法代码。请记住,虽然如果字段为 $null,我不能添加 Set-WordTextText,但由于我在嵌套场景中执行此操作,因此我更愿意将其添加为 $null

function Remove-WordText {
    Param(
        [parameter(ValueFromPipelineByPropertyName, ValueFromPipeline)][Xceed.Words.NET.InsertBeforeOrAfter] $Paragraph,
        [int] $Index = 0,
        [int] $Count = $($Paragraph.Text.Length),
        [bool] $TrackChanges,
        [bool] $RemoveEmptyParagraph,
        [bool] $Supress = $false
    )
    if ($Paragraph -ne $null) {
        Write-Verbose "Remove-WordText - Current text $($Paragraph.Text) "
        Write-Verbose "Remove-WordText - Removing from $Index to $Count - Paragraph Text Count: $($Paragraph.Text.Length)"
        $Paragraph.RemoveText($Index, $Count, $TrackChanges, $RemoveEmptyParagraph)
    }
    if ($Supress) { return } else { return $Paragraph }
}

function Set-WordTextText {
    param(
        [parameter(ValueFromPipelineByPropertyName, ValueFromPipeline)][Xceed.Words.NET.InsertBeforeOrAfter] $Paragraph,
        [alias ("S")] [AllowNull()][string] $Text,
        [switch] $ClearText,
        [bool] $Supress = $false
    )
    if ($Paragraph -ne $null) {
        if (-not [string]::IsNullOrEmpty($Text)) {
            if ($ClearText -eq $true) {
                Write-Verbose 'Set-WordTextText - Clearing Text $ClearText is True'
                $Paragraph = Remove-WordText -Paragraph $Paragraph
            }
            Write-Verbose "Set-WordTextText - Appending Value $Text"
            $Paragraph = $Paragraph.Append($Text)
        }
    }
    if ($Supress) { return } else { return $Paragraph }
}

我基本上是在函数中执行它:

$Paragraph[$i] = $Paragraph[$i] | Set-WordTextText -Text $Text[$i] -ClearText:$ClearText -Supress $false
$Paragraph[$i] = $Paragraph[$i] | Set-WordTextColor -Color $Color[$i] -Supress $false
$Paragraph[$i] = $Paragraph[$i] | Set-WordTextFontSize -FontSize $FontSize[$i] -Supress $false
$Paragraph[$i] = $Paragraph[$i] | Set-WordTextFontFamily -FontFamily $FontFamily[$i] -Supress $false
$Paragraph[$i] = $Paragraph[$i] | Set-WordTextBold -Bold $Bold[$i] -Supress $false
$Paragraph[$i] = $Paragraph[$i] | Set-WordTextItalic -Italic $Italic[$i] -Supress $false

只有值不为空时才会执行动作。所以“包装器”函数可以有 1 到 20 个参数,并且只有在给定参数时才执行。

【问题讨论】:

    标签: powershell null


    【解决方案1】:

    如果您为参数指定类型[String],则该参数将成为类型变量并且不能再为$null。即使您将$null 传递给参数,该值也会自动转换为空字符串。要允许参数是 $null 或字符串(空或非空),您需要这样的内容:

    function Set-WordTextText {
        Param(
            ...
            [Parameter(Mandatory=$false)]
            [AllowNull()]
            $Text = $null,
            ...
        )
    
        if ($_ -ne $null -and $_ -isnot [String]) {
            throw 'Invalid argument for parameter -Text.'
        }
    
        ...
    }
    

    【讨论】:

    • 有道理。出于某种原因,我非常专注于将其键入为 [string],以至于我不接受任何类型。谢谢
    【解决方案2】:

    如果 $Paragraph 处于三种状态之一,则决定要做什么取决于您正在执行的功能。这样的事情可能会有所帮助

    if ($null -eq $Paragraph) { 
        Write-Host "Paragraph is Null, do not execute"
        return
    }
    elseif ([string]::IsNullOrEmpty($Paragraph.Text)) { 
        # this depends on the function you are in.
        # if inside Remove-WordText() you don't have to do anything because it is already empty
        # you can check the function currently executed with $MyInvocation.MyCommand.Name
        if ($MyInvocation.MyCommand.Name -eq 'Remove-WordText') {
            Write-Host "Paragraph.Text is Null or Empty, clear text"
            $Paragraph.RemoveText($Index, $Count, $TrackChanges, $RemoveEmptyParagraph)
        else {
            # however, if you're inside Set-WordTextText() you need to decide if $Text
            # is not empty aswell and if not, you append the Text tp $Paragraph
            if (-not [string]::IsNullOrEmpty($Text)) {
                Write-Verbose "Set-WordTextText - Appending Value $Text"
                $Paragraph = $Paragraph.Append($Text)
            }
        }
    }
    else {
        Write-Host "Paragraph.Text has value, appending text"
        $Paragraph = $Paragraph.Append($Text)
    }
    

    希望有帮助

    【讨论】:

    • 我不认为我遵循你的逻辑。 $Paragraph 为空是一个选项,并且工作得很好。它是单独的值 $Text ,它是一个字符串,不能为空,我需要它为空才能停止执行。否则我需要提供额外的参数。如果我能够使用 $null 我可以简单地跳过向函数提供参数并且它不会执行。就像现在一样,如果我没有定义参数并且我会跳过使用 -Clear 它实际上会擦除值而不是跳过它。
    猜你喜欢
    • 2021-11-17
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    • 2013-07-22
    相关资源
    最近更新 更多