如果不需要单元格日期值范围,则删除Excel电子表格中的行

时间:2014-11-22 18:58:15

标签: excel powershell

我尝试根据单元格日期值是否超过x天来删除不需要的条目,从而改进生成excel报告的工作Powershell脚本。 powershell代码是否正确以删除超过14天的行?

感谢。

 $minus14days = (get-date).Adddays(-14).ToString('dd/MM/yyyy')
 $exceldate = (get-date).ToString('dd/MM/yyyy')
 $minus14days = [STRING] $minus14days
 $xlCellTypeLastCell = 11

 $usedup = $sheet1.usedRange
 $lastCell = $usedup.SpecialCells($xlCellTypeLastCell)
 $row = $lastCell.row

 $exRange = $sheet1.usedRange
    for ($i = 0; $i -le $row; $i++) {
       If ($exRange.Cells.Item($i, 1).value() -lt $minus14days) {
           $Range = $exRange.Cells.Item($i, 1).EntireRow
           $Range.Delete()
           $i = $i - 1
                }
            }

1 个答案:

答案 0 :(得分:0)

  $minus14days = (get-date).Adddays(-14).ToString('dd/MM/yyyy')
  $exceldate = (get-date).ToString('dd/MM/yyyy')
  $minus14days = [STRING] $minus14days
  $xlCellTypeLastCell = 11

  $usedup = $sheet1.usedRange
  $usedup.EntireColumn.HorizontalAlignment = -4131
  $usedup.EntireColumn.VerticalAlignment = -4160
  $lastCell = $usedup.SpecialCells($xlCellTypeLastCell)
  $row = $lastCell.row

  $exRange = $sheet1.usedRange
    for ($i = 0; $i -le $row; $i++) {
     If ($exRange.Cells.Item($i, 1).value() -lt $minus14days) {
       $Range = $exRange.Cells.Item($i, 1).EntireRow
       $Range.Delete()
       $i = $i - 1
            }
        }