为什么 Range.BorderAround 发出“True"?到控制台? [英] Why does Range.BorderAround emit "True" to the console?

查看:26
本文介绍了为什么 Range.BorderAround 发出“True"?到控制台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 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 中,任何命令或表达式...输出(返回)数据隐式输出(成功)输出流,默认情况下会转到主机,这通常是运行 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)

是(更有效的)速记:

Write-Output $range.BorderAround(1, -4138)

(显式使用 很少需要写输出.)

(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);类似地,命令必须总是包含在(...)中;[void] New-Item test.txt 不起作用,只有 [void] (New-Item test.txt) 起作用.
  • While [void] = (...) does that too, it often requires you to enclose an expression in (...) for syntactic reasons; e.g., [void] 1 + 2 doesn't work as intended, only [void] (1 + 2); similarly, a command must always be enclosed in (...); [void] New-Item test.txt doesn't work, only [void] (New-Item test.txt) does.

它在 command 输出(例如,$null = Get-AdUser ...)和 expression 输出方面都表现良好(例如,$null = $range.BorderAround(1, -4138)).

It performs well with both command output (e.g., $null = Get-AdUser ...) and expression output (e.g., $null = $range.BorderAround(1, -4138)).

相反,避免... |Out-Null,因为它通常要慢得多(PowerShell (Core) 6+ 中无副作用的表达式输出的边缘情况除外)[1].

但是,如果您需要保持沉默所有 输出流 - 不仅仅是成功输出,还有错误,详细输出,...... - 你必须使用<代码>*>$null

However, if you need to silence all output streams - not just the success output, but also errors, verbose output, ... - you must use *> $null

  • 作为shellPowerShell 的输出行为基于,就像在cmd 等传统shell 中一样.exe 或 Bash.(虽然传统 shell 有 2 个输出流 - stdout 和 stderr - PowerShell 有 6,以便提供更复杂的功能 - 请参阅about_Redirection.)

  • 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>;return,即 的隐式输出,然后无条件地将控制权返回给调用者;值得注意的是,使用 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 of return 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:

  • 虽然 PowerShell 确实有一个对应于 echo 的东西,即 Write-Output,很少用到它.

  • While PowerShell does have a counterpart to echo, namely Write-Output, its use is rarely needed.

  • Write-Output 有用的极少数情况是使用 -NoEnumerate 防止在输出上枚举集合,或使用 通用参数 -OutVariable 到两个输出data 在一个变量中捕获它(通常只有表达式需要,因为cmdlet和高级函数/脚本本身支持-OutVariable).
  • 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).

隐式输出行为:

  • 通常是一种祝福:

  • 对于交互式实验 - 只需输入任何语句 - 特别是包括诸如 [IO.Path]::GetExtension('foo.txt')[math]::Pow(2, 32) - 并查看其输出(类似于 REPL).
  • 用于编写无需说明隐含行为的简洁代码(参见下面的示例).
  • 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 the ForEach-Object cmdlet call as it is being produced, not after Get-HexNumber has exited.

[1] 在 PowerShell (Core) 6+ 中,Out-Null 有一个优化,如果前面的管道段是一个无副作用的表达式而不是方法或命令调用;例如,1..1e6 |Out-Null 几乎立即执行,因为该表达式似乎甚至没有执行.然而,这样的场景是非典型的,功能上等效的 Write-Output (1..1e6) |Out-Null 需要很长时间才能运行,远比 $null = Write-Output (1..1e6) 长.

[1] In PowerShell (Core) 6+, Out-Null has an optimization if the only preceding pipeline segment is a side effect-free expression rather than a method or command call; e.g., 1..1e6 | Out-Null executes in almost no time, because the expression is seemingly not even executed. However, such a scenario is atypical, and the functionally equivalent Write-Output (1..1e6) | Out-Null takes a long time to run, much longer than $null = Write-Output (1..1e6).

这篇关于为什么 Range.BorderAround 发出“True"?到控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆