Powershell 在字符串中使用null
我发现,对于PowerShell中的字符串,使用Powershell 在字符串中使用null,powershell,null,Powershell,Null,我发现,对于PowerShell中的字符串,使用$null是不可能的。虽然我已经使用了以下解决方法,但我想知道是否有更好的方法 基本上我需要3种状态的字符串$null,'(空),'some string' 第一个意思是——不要执行 第二个一-明文 用新值替换文本 下面是我选择开关-ClearText的变通代码。请记住,如果字段是$null,我就不能添加Set WordTextText,但因为我是在嵌套场景中这样做的,所以我更喜欢按$null的方式来做 function Remove-Word
$null
是不可能的。虽然我已经使用了以下解决方法,但我想知道是否有更好的方法
基本上我需要3种状态的字符串$null
,'
(空),'some string'
- 第一个意思是——不要执行李>
- 第二个一-明文
- 用新值替换文本
-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
它仅在值不为null时执行操作。因此,“包装器”函数可以有1到20个参数,并且仅在给定参数时执行 如果$paragration处于三种状态之一,则根据您正在执行的功能决定要执行的操作。像这样的东西可能会有帮助
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)
}
如果为参数指定类型
[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.'
}
...
}
我不明白你的逻辑$段落为空是一种选择,效果很好。它是一个单独的值$Text,它是一个字符串,不能为null,我需要它为null以停止执行。否则我需要提供额外的参数。若我能够使用$null,我可以简单地跳过为函数提供参数,它就不会执行了。就像现在一样,若我不定义参数,我会跳过使用-Clear,它实际上会擦除值,而不是跳过它。这很有意义。出于某种原因,我太专注于将其键入为[string],以至于我不接受任何类型。谢谢