为什么Range.BorderAround会发出"True"消息?到控制台? [英] Why does Range.BorderAround emit "True" to the console?
问题描述
使用BorderAround会向控制台发出"True"消息.
Using BorderAround emits "True" to the console.
$range = $sum_wksht.Range('B{0}:G{0}' -f ($crow))
$range.BorderAround(1, -4138)
这可以通过使用以下方法之一来解决.
This can be overcome by using one of the following.
$wasted = $range.BorderAround(1, -4138)
[void]$range.BorderAround(1, -4138)
为什么需要这个?我没有正确创建范围吗?有更好的解决方法吗?
Why is this needed? Am I not creating the range correctly? Is there a better workaround?
推荐答案
为什么需要这个?
Why is this needed?
这是必需的,因为 BorderAround
方法具有返回值,并且在PowerShell中, 输出(返回)数据 的任何命令或表达式...
都是 隐式输出到(成功)输出流 ,默认情况下流到 host ,该主机通常是控制台窗口 >(终端)在其中运行PowerShell会话.
It is needed, because the BorderAround
method has a return value and, in PowerShell, any command or expression ...
that outputs (returns) data is implicitly output to the (success) output stream, which by default goes to the host, which is typically the console window (terminal) in which a PowerShell session runs.
也就是说,数据显示在控制台/终端中,除非是,否则它是:
That is, the data shows in the console/terminal, unless it is:
- 已捕获(
$var = ...
) - 通过管道发送以进行进一步处理(
... | ...
;最后一个管道段的命令本身可能会或可能不会产生输出) - 已重定向(
... >
)
- captured (
$var = ...
) - sent through the pipeline for further processing (
... | ...
; the last pipeline segment's command may or may not produce output itself) - redirected (
... >
)
或其任何组合.
也就是说:
$range.BorderAround(1, -4138)
(更高效)是以下简称:
is (more efficient) shorthand for:
Write-Output $range.BorderAround(1, -4138)
(明确使用 Write-Output
很少需要.)
(Explicit use of Write-Output
is rarely needed.)
由于您不想要该输出,因此必须抑制 ,为此您可以选择以下几种方式:
Since you don't want that output, you must suppress it, for which you have several options:
-
$null = ...
[void] (...)
... > $null
... | Out-Null
$null = ...
可能是最佳的总体选择,因为:
$null = ...
may be the best overall choice, because:
- 传达了压制预先的意图
-
[void] = (...)
也会这样做,但是出于语法原因,通常需要您将其余的语句括在(...)
中.例如[void] 1 + 2
不能按预期运行,只有[void] (1 + 2)
- it conveys the intent to suppress up front
[void] = (...)
does that too, but often requires you to enclose the rest of the statement in(...)
for syntactic reasons; e.g.,[void] 1 + 2
doesn't work as intended, only[void] (1 + 2)
相反,避免使用
... | Out-Null
,因为它通常要慢得多(PowerShell Core 中的表达式输出除外).Conversely, avoid
... | Out-Null
, because it is generally much slower (except with expression output in PowerShell Core).However, if you need to silence all output streams - not just the success output, but also errors, verbose output, ... - you must use
*> $null
-
作为 shell , PowerShell的输出行为基于流 ,就像在传统的shell中(如
cmd.exe
或Bash)一样. (虽然传统的shell具有 2 输出流-stdout和stderr-PowerShell具有 6 ,以便提供更复杂的功能-请参见
As a shell, PowerShell's output behavior is based on streams, as in traditional shells such as
cmd.exe
or Bash. (While traditional shells have 2 output streams - stdout and stderr - PowerShell has 6, so as to provide more sophisticated functionality - see about_Redirection.)
-
cmdlet,脚本或函数可以根据需要多次写入输出流,并且此类输出通常即时可用以供显示,但对于潜在的使用者也尤其如此,它启用管道提供的流式传输,一对一处理.
A cmdlet, script, or function can write to the output streams as often as it wants, and such output is usually instantly available for display but notably also to potential consumers, which enables the streaming, one-by-one processing that the pipeline provides.
这与传统的编程语言相反,传统的编程语言的输出行为基于返回值(通常通过
return
关键字提供),用于对输出数据进行压缩(返回值)和流控制(退出范围并返回到调用方).This contrasts with traditional programming languages, whose output behavior is based on return values, typically provided via the
return
keyword, which conflates output data (the return value) with flow control (exit the scope and return to the caller).- 一个常见的陷阱是期望PowerShell的
return
语句执行相同的操作,但事实并非如此:return <val>
只是<val>; return
的语法糖,即,<val>
的隐式输出后跟无条件返回对呼叫者的控制权;值得注意的是,使用return
不会 阻止从同一范围内的早期语句生成输出.
- A frequent pitfall is to expect PowerShell's
return
statement to act the same, but it doesn't:return <val>
is just syntactic sugar for<val>; return
, i.e., implicit output of<val>
followed by an unconditional return of control to the caller; notably, the use ofreturn
does not preclude generation of output from earlier statements in the same scope.
与传统的shell不同, PowerShell不需要要求显式写入输出流命令即可产生输出:
Unlike traditional shells, PowerShell doesn't require an explicit write-to-the-output stream command in order to produce output:
While PowerShell does have a counterpart to
echo
, namelyWrite-Output
, its use is rarely needed.
- Among the rare cases where
Write-Output
is useful is preventing enumeration of a collection on output with-NoEnumerate
, or to use common parameter-OutVariable
to both output data and capture it in a variable (which is generally only needed for expressions, because cmdlets and advanced functions / scripts themselves support-OutVariable
).
隐式输出行为:
-
通常是祝福 :
is generally a blessing:
- for interactive experimentation - just type any statement - notably including expressions such as
[IO.Path]::GetExtension('foo.txt')
and[math]::Pow(2, 32)
- and see its output (akin to the behavior of a REPL). - for writing concise code that doesn't need to spell out implied behavior (see example below).
偶尔可能是一个陷阱:
-
面向习惯传统编程语言语义的用户.
for users accustomed to the semantics of traditional programming languages.
由于您可能不希望产生输出的陈述(例如您的情况)会导致输出流的意外污染;一个更典型的示例是
[System.Collections.ArrayList]
类的.Add()
方法意外产生输出.due to the potential for accidental pollution of the output stream from statements that one doesn't expect to produce output, such as in your case; a more typical example is the
.Add()
method of the[System.Collections.ArrayList]
class unexpectedly producing output.示例:
# Define a function that takes an array of integers and # outputs their hex representation (e.g., '0xa' for decimal 10) function Get-HexNumber { param([int[]] $numbers) foreach ($i in $numbers) { # Format the integer at hand # *and implicitly output it*. '0x{0}' -f $i.ToString('x') } } # Call the function with integers 0 to 16 and loop over the # results, sleeping 1 second between numbers. Get-HexNumber (0..16) | ForEach-Object { "[$_]"; Start-Sleep 1 }
上面的结果如下:
[0x0] # 1-second pause [0x1] # 1-second pause [0x2] ... [0x10]
这说明了行为的流方面:
Get-HexNumber
的输出可用于ForEach-Object
cmdlet调用,因为它正在生成,而不是之后Get-HexNumber
已退出.This demonstrates the streaming aspect of the behavior:
Get-HexNumber
's output is available to theForEach-Object
cmdlet call as it is being produced, not afterGet-HexNumber
has exited.这篇关于为什么Range.BorderAround会发出"True"消息?到控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-