Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Powershell Core枚举Excel数据透视表中的数据字段_Powershell_Excel 2016 - Fatal编程技术网

使用Powershell Core枚举Excel数据透视表中的数据字段

使用Powershell Core枚举Excel数据透视表中的数据字段,powershell,excel-2016,Powershell,Excel 2016,使用powershell 7.1.3,我可以加载电子表格和枚举工作表,例如,我可以在工作表上找到数据透视表并回显其名称。这是一个数据透视表,它与工作簿的本地数据模型相连,并有许多数据字段,我可以在VBA中枚举这些数据字段 加载工作簿后,我成功地将数据透视表从工作表加载到变量中。然后我访问DataFileds成员并尝试枚举其成员,但我只能看到这些成员 $testPivot.PivotFields()|获取成员-Force 在PowerShell 7.1.3中,阵列未展开 当我在Windows P

使用powershell 7.1.3,我可以加载电子表格和枚举工作表,例如,我可以在工作表上找到数据透视表并回显其名称。这是一个数据透视表,它与工作簿的本地数据模型相连,并有许多数据字段,我可以在VBA中枚举这些数据字段

加载工作簿后,我成功地将数据透视表从工作表加载到变量中。然后我访问DataFileds成员并尝试枚举其成员,但我只能看到这些成员

$testPivot.PivotFields()|获取成员-Force

在PowerShell 7.1.3中,阵列未展开

当我在Windows Powershell 5.1中执行相同操作时,阵列将展开。。。

7.1.3中的行为

$testPivot.PivotFields()|获取成员-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
}
$pivotfields=$testPivot.pivotfields()
if($pivotfields-is[System.Array]){
#选择第一个,或使用foreach迭代:
$pivotfields[0]
$pivotfields | Foreach{}
} 
否则{
#正常进行
}

看起来您得到的对象类型完全不同。v7图像显示了一个与数组类似的
Count
属性,因此您可以加载多个透视表吗?请尝试
$testPivot[0].PivotFields()
$testPivot.PivotFields()[0]
进行双重检查。@user19702是的,您是对的!在5.1中,
$testPivot.DataFields()
返回一个对象数组,但在7.1.3中,它返回数组对象,这就是difference.Yep,这将起作用。谢谢这看起来像是excel对象模型中的一个bug(或者至少是不一致)。有些集合是以正常方式展开的,如
工作簿.Sheets
集合,但有些不是。