[英]Trying to count the number of rows in excel having a specific text in two different column using powershell
I just started learning powershell and I'm trying to automate an excel report to extract some important info.我刚开始学习 powershell 并且我正在尝试自动化 excel 报告以提取一些重要信息。 to make my life easier.让我的生活更轻松。
There are 3 possible states (n,l,m) and 3 possible desc (i,j,k) in this report此报告中有 3 种可能的状态 (n,l,m) 和 3 种可能的 desc (i,j,k)
I have a table like this:我有一张这样的桌子:
|name | number | state | desc|
| ---- | ------ |-------|-----|
|a | 1 | n | i |
|b | 2 | n | j |
|c | 3 | l | j |
|d | 4 | m | k |
I want to be able to count the number of rows having these combinations in the column 3 and 4: (n,i), (l,i), (nj), (l,j)我希望能够计算在第 3 列和第 4 列中具有这些组合的行数:(n,i),(l,i),(nj),(l,j)
This is what I have done so far but I have tried to many things that didn't work.这是我到目前为止所做的,但我尝试了很多没有用的东西。
$excel = New-Object -ComObject excel.application
$excel.visible = $false
$filepath = "C:\file path"
$WB = $excel.workbooks.open($filepath)
$MN = $WB.worksheets.item("sheet3")
$MN.activate()
$x= @("n","l","m")
$y=@("i","j")
$z=$MN.UsedRange
$count=0
foreach ($z in ($x -and $y)) { ++$count
$rowcount=$count}
write-host "Row count:" $rowcount
It is giving an output of 1 and Its more than 1 in the sheet.它给出的 output 为 1,并且在表格中超过 1。 Please help请帮忙
================================================= Going from Owlsleeping solution, I'm trying to filter on dates as well like this: ================================================== 去从 Owlsleeping 解决方案中,我正在尝试过滤日期,如下所示:
$Content = Import-Csv -Path "C:\file.csv"
$StateFilter = @("n", "l")
$DescFilter = @("i", "j")
$startdate = Get-date -Year 2020 -Month 10 -Day 1 -Hour 00 -Minute 00 -Second 00
$enddate = Get-date -Year 2020 -Month 10 -Day 31 -Hour 23 -Minute 59 -Second 59
($Content | Where-Object {$_.State -in $StateFilter -and $_.Desc -in $DescFilter -and $_.Created -le $enddate -and $_.Created -ge $startdate }).Count
This is not giving back the accurate count of records created between Oct 1 and 31. Please help这并没有返回 10 月 1 日至 31 日之间创建的记录的准确计数。请帮助
Here's the solution for csv input.这是 csv 输入的解决方案。
$Content = Import-Csv -Path "C:\file.csv"
$StateFilter = @("n", "l")
$DescFilter = @("i", "j")
($Content | Where-Object {$_.State -in $StateFilter -and $_.Desc -in $DescFilter}).Count
I figured out the second part of my question:我想出了问题的第二部分:
$Content = Import-Csv -Path "C:\file.csv"
$StateFilter = @("n", "l")
$DescFilter = @("i", "j")
$startdate = Get-date -Year 2020 -Month 10 -Day 1 -Hour 00 -Minute 00 -Second 00
$enddate = Get-date -Year 2020 -Month 10 -Day 31 -Hour 23 -Minute 59 -Second 59
($Content | Where-Object {$_.State -in $StateFilter -and $_.Desc -in $DescFilter -and $_.Created -as [datetime] -le $enddate -and $_.Created -as [datetime] -ge $startdate }).Count
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.