使用 Powershell 在 Excel 中同时选择多个范围

Selecting multiple ranges simultaneously in Excel using Powershell

我正在尝试使用 powershell 在 excel 中创建饼图。我可以使用 .Select() 或 .Activate() 突出显示范围,但我不能同时 select 两个单独的范围。我需要同时 select 它们,以便饼图具有正确的标签。无法在线找到任何指导,因此将不胜感激。

$headers = $ws.Cells.Item(1,1).EntireRow
$headers.Activate()
$ws.Range("A3:ZZ3").Activate()

This article 建议一种 select 多个范围的方法。使用上面的代码,试试这个...

[object[]]$args1 = ($headers, $ws.Range("A3:ZZ3")) 
$union=$xl.GetType().InvokeMember("Union", [System.Reflection.BindingFlags]::InvokeMethod, $null, $xl, $args1) 
$union.Select()

...其中 $xl 定义为 $xl = New-Object -ComObject Excel.Application