使用 Powershell Core 在 Excel Pivot Table 中枚举数据字段
Enumerate DataFields in Excel Pivot Table with Powershell Core
使用 powershell 7.1.3,我能够加载一个传播sheet 并枚举工作sheet,我可以在 sheet 上找到一个枢轴 table =] 并回显它的名字。这是一个连接到工作簿的本地数据模型的枢轴 table,它有许多 DataFields,我可以在 VBA.
中枚举它们
加载工作簿后,我成功地将工作sheet中的数据透视表table加载到变量中。然后我访问 DataFileds 成员并尝试枚举它的成员,但是,我只能看到这些成员...
$testPivot.PivotFields() | Get-Member -Force
在 PowerShell 7.1.3 中,数组未展开
当我在 Windows Powershell 5.1 中做同样的事情时,数组被展开......
7.1.3 中的行为
$testPivot.PivotFields() | Get-Member -Force
与
完全一样
write-output -NoEnumerate $testPivot.DataFields() | Get-Member -Force
5.1
如何在 7.1.3 中解包数组?
要让您的代码在两个版本中都能正常工作,您可以快速做的事情是通过以下方式检查结果是否为数组:
$pivotfields = $testPivot.PivotFields()
if ($pivotfields -is [System.Array]) {
# either pick the first one, or iterate through with foreach:
$pivotfields[0]
$pivotfields | Foreach { <# do stuff #> }
}
else {
# proceed normally
}
使用 powershell 7.1.3,我能够加载一个传播sheet 并枚举工作sheet,我可以在 sheet 上找到一个枢轴 table =] 并回显它的名字。这是一个连接到工作簿的本地数据模型的枢轴 table,它有许多 DataFields,我可以在 VBA.
中枚举它们加载工作簿后,我成功地将工作sheet中的数据透视表table加载到变量中。然后我访问 DataFileds 成员并尝试枚举它的成员,但是,我只能看到这些成员...
$testPivot.PivotFields() | Get-Member -Force
在 PowerShell 7.1.3 中,数组未展开
当我在 Windows Powershell 5.1 中做同样的事情时,数组被展开......
7.1.3 中的行为
$testPivot.PivotFields() | Get-Member -Force
与
完全一样write-output -NoEnumerate $testPivot.DataFields() | Get-Member -Force
5.1
如何在 7.1.3 中解包数组?
要让您的代码在两个版本中都能正常工作,您可以快速做的事情是通过以下方式检查结果是否为数组:
$pivotfields = $testPivot.PivotFields()
if ($pivotfields -is [System.Array]) {
# either pick the first one, or iterate through with foreach:
$pivotfields[0]
$pivotfields | Foreach { <# do stuff #> }
}
else {
# proceed normally
}