Powershell:存储在变量中的属性

标签 powershell epplus

我想使用 EPPlus 根据属性值查找某个范围内的所有单元格。假设我需要在现有电子表格中查找所有带有粗体文本的单元格。我需要创建一个接受可配置属性参数的函数,但在使用存储在变量中的属性时遇到问题:

$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'

最佳答案

点走进属性并不真正适用于字符串。您需要分离属性层。下面是一个具有三层属性的对象的示例。

# create object
$props = @{
    first = @{
        second = @{
            third = 'test'
        }
    }
}
$obj = New-Object -TypeName psobject -Property $props

# outputs "test"
$obj.first.second.third

# does not work
$obj.'first.second.third'

# outputs "test"
$a = 'first'
$b = 'second'
$c = 'third'
$obj.$a.$b.$c

在您的示例中,这将是这样的:

$cellobject = $ws.cells[1,1,10,10]
$p1 = 'Style'
$p2 = 'Font'
$p3 = 'Bold'

$cellobject.$p1.$p2.$p3

或者你可以做得有点动态。这应该产生相同的结果:

$cellobject = $ws.cells[1,1,10,10]    
$props = 'Style.Font.Bold'.Split('.')
$result = $cellobject
foreach ($prop in $props) {
    $result = $result.$prop
}
$result

由于是星期五,这里有一个函数:)

function GetValue {
    param (
        [psobject]$InputObject,
        [string]$PropertyString
    )

    if ($PropertyString -like '*.*') {
        $props = $PropertyString.Split('.')
        $result = $InputObject
        foreach ($prop in $props) {
            $result = $result.$prop
        }
    } else {
        $result = $InputObject.$PropertyString
    }

    $result
}

# then call the function
GetValue -InputObject $cellobject -PropertyString 'Style.Font.Bold'

关于Powershell:存储在变量中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44205037/

相关文章:

azure - Powershell:Get-AzureADGroupMember 没有给我完整列表

c# - EPPlus 不支持 ExcelHorizo​​ntalAlignment Center 或 Right

c# - epplus获取列标题

c# - 尝试使用 EPPlus 在服务器上读取 Excel 文件,但不能通过浏览器

xml - 使用PowerShell脚本从Android list XML文件中删除权限

powershell - XmlSerializer在PowerShell中的功能?

Powershell - 某些结果的输出颜色

java - Windows Powershell 在运行 jboss 7.1.1 时经常挂起

c# - 将数据集导出到 excel 2007 EPPlus

c# - 需要将科学值(value)转化为文本