我想使用EPPlus根据属性值查找范围内的所有单元格。假设我需要在现有的电子表格中查找所有含粗体文本的单元格。我需要创建将接受一个可配置的性能参数的功能,但我使用存储在一个变量属性有问题:Powershell:存储在变量中的属性
$cellobject = $ws.cells[1,1,10,10]
$properties = 'Style.Font.Bold'
$cellobject.$properties
$cellobject.{$properties}
$cellobject.($properties)
$cellobject."$properties"
这些工作都没有,并导致调用深度溢出。
如果这种方式不行,我可以使用库中的东西吗?
编辑:为了证明我更新与HanShotFirst提供概念的功能,最终的解决方案......
function Get-CellObject($ExcelSheet,[string]$PropertyString,[regex]$Value){
#First you have to get the last row with text,
#solution for that is not provided here...
$Row = Get-LastUsedRow -ExcelSheet $ExcelSheet -Dimension $true
while($Row -gt 0){
$range = $ExcelSheet.Cells[$Row, 1, $Row, $ExcelSheet.Dimension.End.Column]
foreach($cellObject in $range){
if($PropertyString -like '*.*'){
$PropertyArr = $PropertyString.Split('.')
$thisObject = $cellObject
foreach($Property in $PropertyArr){
$thisObject = $thisObject.$Property
if($thisObject -match $Value){
$cellObject
}
}
}
else{
if($cellObject.$PropertyString -match $Value){
$cellObject
}
}
}
$Row--
}
}
#The ExcelSheet parameter takes a worksheet object
Get-CellObject -ExcelSheet $ws -Property 'Style.Font.Bold' -Value 'True'
你尝试'$ cellobject.properties'? – Moerwald
@Moerwald先生,我试过了。但重点是保持属性在函数中可配置,所以这就是为什么我需要将属性存储在变量中。那有意义吗? – Mack
问题似乎是字符串$ properties中的句点。 – Mack