我想使用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