PowerShell 脚本检查并跳过具有黄色单元格、灰色单元格或空白单元格的行

问题描述 投票:0回答:1

我需要准备 Powershell 脚本来检查我的 NSG 规则设计表并跳过包含黄色单元格、灰色单元格或空白单元格的行。我已经准备了下面的脚本,但它没有按预期工作。有人可以帮我修复这个脚本吗?

Powershell 脚本:

# Load the EPPlus library
Add-Type -Path "/home/lokesh/EPPlus.dll"  # Replace with the path to your EPPlus.dll file

# Create an Excel package object
$excelPackage = New-Object OfficeOpenXml.ExcelPackage

# Load the Excel file
$filePath = "/home/lokesh/NSG_Rules.xlsx"
$excelPackage = [OfficeOpenXml.ExcelPackage]::Load($filePath)

# Get the first worksheet from the Excel package
$worksheet = $excelPackage.Workbook.Worksheets[0]  # Change the index if needed

# Define a function to check cell color
function IsYellow($cell) {
    return ($cell.Style.Fill.BackgroundColor.Rgb -eq "FFFFFF00")  # Yellow color code
}

# Skip rows with yellow, gray, or blank cells
$rowsToKeep = @()
foreach ($row in $worksheet.Cells.Rows) {
    $skipRow = $false
    foreach ($cell in $row.Cells) {
        if (IsYellow($cell) -or $cell.Style.Fill.BackgroundColor.Rgb -eq "FFA9A9A9" -or -not $cell.Value) {
            $skipRow = $true
            break
        }
    }
    if (-not $skipRow) {
        $rowsToKeep += $row
    }
}
powershell
1个回答
0
投票
  • 您问题中的代码在几个基本方面被破坏了;是AI生成的吗?

  • 解决方案如下:

    • 按要求解决了您的问题,但请注意,EPPlus .NET 库对 PowerShell 不是特别友好,而

      ImportExcel
      PowerShell 模块通常是更好的选择。
      此外,对于商业用途,EPPlus 需要付费许可证。

    • 它:

      • 收集感兴趣的(数据)行作为输出数组中的

        [OfficeOpenXml.ExcelRangeRow]
        实例以进行进一步处理;每个此类行的单元格都可以通过
        .Range
        属性进行枚举,并且每个枚举单元格都有一个
        .Value
        属性。

      • 不会创建 PowerShell 友好的 [pscustomobject]

         实例,并具有为标题单元格命名的属性,就像 Import-Excel
         中的 
        ImportExcel
         所做的那样。

      • 不会

        尝试更新电子表格本身,即它不会删除您想要跳过(省略)的行。

  • # Specify the license type (required in v5+ of EPPlus) $env:EPPlusLicenseContext = 'NonCommercial' # Load the EPPlus assembly. Add-Type -LiteralPath "/home/lokesh/EPPlus.dll" # Replace with the path to your EPPlus.dll file # The file path of the workbook to open. $filePath = "/home/lokesh/NSG_Rules.xlsx" # Create an Excel package object and load the workbook file. # Convert-Path ensures that a *full path* is passed, which is required in # .NET calls, given that .NET's working dir. usually differs from PowerShell's. $excelPackage = [OfficeOpenXml.ExcelPackage]::new((Convert-Path -LiteralPath $filePath)) # Get the first worksheet from the Excel package $worksheet = $excelPackage.Workbook.Worksheets[0] # Iterate over all rows that have cells with content and/or formatting # and collect the data rows of interest. $headerProcessed = $false [array] $rowsOfInterest = foreach ($row in $worksheet.Rows) { # process each row $rowCellCount = 0; $skipRow = $false foreach ($cell in $row.Range) { # process each cell in a row ++$rowCellCount if (-not $headerProcessed) { continue } # header row: just count the cells. # data row: check for a background color of interest. $skipRow = $cell.Style.Fill.BackgroundColor.Rgb -in 'FFFFFF00', 'FFA9A9A9' if ($skipRow) { break } } if (-not $headerProcessed) { # header row: Save the count of cells in the header row (assumed to be the first one), # i.e. the count of columns $headerRowCellCount = $rowCellCount $headerProcessed = $true continue } # data row: Also check if not all cells in the rows had values. $skipRow = $skipRow -or $rowCellCount -lt $headerRowCellCount if (-not $skipRow) { $row # output } } # $rowsOfInterest is now an array of [OfficeOpenXml.ExcelRangeRow] instances # containing only those data rows that neither have empty cells nor # cells with the specified background colors. # Sample output: Print the address and value of the cells in these rows. $rowsOfInterest.Range | Select-Object Address, Value
© www.soinside.com 2019 - 2024. All rights reserved.