Powershell ImportExcel 和 skip/exclude 个特定工作表
Powershell ImportExcel and skip/exclude specific sheets
我有下面的代码用于导入工作簿,排除工作簿中的特定工作表,在某些条件下工作,然后导出单个工作表。除了不排除“Get-ExcelSheetInfo”行中指定的一些工作表外,一切正常。
$param = @{
Path = $inputfileName
StartRow = 5
HeaderName = 'Setting', 'Current settings', 'Proposed settings', 'Notes'
}
# Loop and obtain worksheets in workbook
Get-ExcelSheetInfo -Path $inputfileName | Where-Object {$_.Name -notlike "Sheet1" -and $_.Name -notlike "Sheet3" -and $_.Name -notlike "Sheet5" -and $_.Name -notlike "Sheet8" -and $_.Name -notlike "Sheet11" -and $_.Name -notlike "Change Record" -and $_.Name -notlike "LIST"} | ForEach-Object {
# Set the worksheetname name in $param
$param['WorksheetName'] = $_.Name
# Import the worksheet and enumerate it
foreach($line in Import-Excel @param) {
$currSettings = $line.'Current settings'
$propSettings = $line.'Proposed settings'
# If the value of 'Current Settings' cell is equal to the adjacent value of
# 'Proposed Settings' cell OR is empty (white spaces), skip and go to next iteration
if($currSettings -eq $propSettings -or [string]::IsNullOrWhiteSpace($currSettings)) {
continue
}
# If we're here, condition before this was not true, hence we want to
# Output the line (row) and add a new column (A) with the name of the worksheet the setting was obtained from
$line | Select-Object @{N='Workstream';E={$param['WorksheetName']}}, *
}
} | Export-Excel -Path $outputfileName -WorksheetName 'Change List' -AutoSize -BoldTopRow -TableName table1 -TableStyle Medium6 -FreezeTopRow -CellStyleSB {
param($workSheet)
$WorkSheet.Cells["C:D"].Style.HorizontalAlignment="Center"}
我想知道这是否可能是因为其中一些工作表匹配来自“if($currSettings -eq $propSettings -or [string]::IsNullOrWhiteSpace($currSettings)) { continue”的条件
即使我指定不应该从一开始就导入它们。有什么想法吗?
问题似乎源于文件使用“invoke-item”cmdlet 启动的方式。我删除了该行并在导出结束时使用了“- Show”,现在它正在正确输出。不确定如何或为什么会导致问题。
我有下面的代码用于导入工作簿,排除工作簿中的特定工作表,在某些条件下工作,然后导出单个工作表。除了不排除“Get-ExcelSheetInfo”行中指定的一些工作表外,一切正常。
$param = @{
Path = $inputfileName
StartRow = 5
HeaderName = 'Setting', 'Current settings', 'Proposed settings', 'Notes'
}
# Loop and obtain worksheets in workbook
Get-ExcelSheetInfo -Path $inputfileName | Where-Object {$_.Name -notlike "Sheet1" -and $_.Name -notlike "Sheet3" -and $_.Name -notlike "Sheet5" -and $_.Name -notlike "Sheet8" -and $_.Name -notlike "Sheet11" -and $_.Name -notlike "Change Record" -and $_.Name -notlike "LIST"} | ForEach-Object {
# Set the worksheetname name in $param
$param['WorksheetName'] = $_.Name
# Import the worksheet and enumerate it
foreach($line in Import-Excel @param) {
$currSettings = $line.'Current settings'
$propSettings = $line.'Proposed settings'
# If the value of 'Current Settings' cell is equal to the adjacent value of
# 'Proposed Settings' cell OR is empty (white spaces), skip and go to next iteration
if($currSettings -eq $propSettings -or [string]::IsNullOrWhiteSpace($currSettings)) {
continue
}
# If we're here, condition before this was not true, hence we want to
# Output the line (row) and add a new column (A) with the name of the worksheet the setting was obtained from
$line | Select-Object @{N='Workstream';E={$param['WorksheetName']}}, *
}
} | Export-Excel -Path $outputfileName -WorksheetName 'Change List' -AutoSize -BoldTopRow -TableName table1 -TableStyle Medium6 -FreezeTopRow -CellStyleSB {
param($workSheet)
$WorkSheet.Cells["C:D"].Style.HorizontalAlignment="Center"}
我想知道这是否可能是因为其中一些工作表匹配来自“if($currSettings -eq $propSettings -or [string]::IsNullOrWhiteSpace($currSettings)) { continue”的条件 即使我指定不应该从一开始就导入它们。有什么想法吗?
问题似乎源于文件使用“invoke-item”cmdlet 启动的方式。我删除了该行并在导出结束时使用了“- Show”,现在它正在正确输出。不确定如何或为什么会导致问题。