Powershell:根据列值自动过滤

时间:2018-11-20 20:00:39

标签: excel powershell filter

我已经看到了几篇文章,但是它们似乎都使用相同的示例,因此我不确定不同情况下的代码会有何不同:

这是我所拥有的:

$rng = $sheet.Cells.Item(1,7).EntireColumn #choose all columns from 1 to 7
    $rng.select | Out-Null # select this range
    $filterValue = @("TRUE") # create filter value, that is only choose when value matches "TRUE"
    $xlFilterValues = 7 # "found in MS documentation", copied not sure what this does
$objExcel.Selection.AutoFilter(7,$filterValue, $xlFilterValues) # here the first parameter value of 7 is the column I am trying to filter on


$rowMax = ($sheet.FilteredRange.Rows).count
 $colMax = ($sheet.FilteredRange.Columns).count

尽管我的脚本没有崩溃或任何故障,但看起来过滤器无法正常工作。我大约有一千行,其中过滤列的值设置为“ TRUE”,“ FALSE”或空白

我的期望是:

对于$ colMax将输出7,对于$ rowMax将输出40(第7列的值为“ TRUE”的行数)。

但是,我得到0的{​​{1}}。

1 个答案:

答案 0 :(得分:0)

我想打开制表符分隔的文件并为给定值过滤M列

$column = 13 # column M
$filename = "C:\temp\terminations.txt"
$criteria "Voluntary"
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $true
$workbook = $Excel.Workbooks.Open($filename)
$worksheet = $workbook.Worksheets.Item(1)
$usedrange = $worksheet.UsedRange
$usedrange.EntireColumn.AutoFilter()
$usedrange.AutoFilter($column, $criteria)