【问题标题】:Automate Excel with VBScript to process Nagios Logs使用 VBScript 自动化 Excel 以处理 Nagios 日志
【发布时间】:2015-11-06 17:17:54
【问题描述】:

我正在使用 VBScript 从 Nagios XI 中提取一个 LOG 文件。我已经自动化了一个将使用 IE.Aplication navigate 的脚本,选择正确的参数(过滤)并导出为 CSV(Excel 可用)。已经完成了!

现在我想过滤更多(Nagios 不能做的事情)。我有多个 CPU 日志,我想删除所有包含这个词的行。我在 VBA 中发现了很多脚本,但在我的情况下,我每次使用都覆盖文件的问题是我使用的 vba 脚本(在另一个 Excel 中)。我需要将其更改为从应用程序的“外部”工作(即主脚本)

Sub CellColor(rng As Range)
  For Each row In rng.Rows
    For Each cell In row.Cells
      If cell.Value = ActiveCell.Value Then  '--- The Condition of the function
        cell.Style = "Bad"                   '--- Changing the "Style" atribbute
      Else
        cell.Style = "Good"                  '--- Changing the "Style" atribbute
      End If
    Next cell
  Next row
End Sub

任何机构都可以建议如何转换它吗?

【问题讨论】:

    标签: excel vbscript vba


    【解决方案1】:

    Excel 可以通过 VBScript 使用相应的 COM 对象实现自动化:

    Set xl = CreateObject("Excel.Application")
    xl.Visible = True
    
    Set wb = xl.Workbooks.Open("C:\path\to\your.xls")
    Set ws = wb.Sheets(1)
    
    Set rng = ws.Range("B3:E5")
    
    For Each row In rng
      For Each cell In row.Cells
        If cell.Value = xl.ActiveCell.Value
          cell.Style = "Bad"
        Else
          cell.Style = "Good"
        End If
      Next
    Next
    
    wb.Save
    wb.Close
    
    xl.Quit
    

    但是,由于 Range 对象也具有 Cells 属性,因此循环可以简化为:

    For Each row In rng.Cells
      If cell.Value = xl.ActiveCell.Value
        cell.Style = "Bad"
      Else
        cell.Style = "Good"
      End If
    Next
    

    请注意,无论哪种方式,您必须使用xl.ActiveCell.Value 而不仅仅是ActiveCell.Value,因为在VBScript 中没有隐式使用Application 对象(VBA 中的ActiveCell.Value 是缩写Application.ActiveCell.Value)。

    有关 VBScript 和 VBA 之间差异的更多信息,请参阅here

    上面代码中的范围只是一个例子。如果要处理工作表中的整个使用范围,请将其替换为

    Set rng = ws.UsedRange
    

    如果任何单元格包含“CPU”一词,则要删除行,您可以执行以下操作:

    For Each cell In rng.Cells
      If InStr(cell.Value, "CPU") > 0 Then cell.EntireRow.Delete
    Next
    

    您还可以在 Excel 中打开之前从 CSV 中删除这些行:

    Set fso = CreateObject("Scripting.FileSystemObject")
    
    filename = "C:\path\to\your.csv"
    
    Set inFile  = fso.OpenTextFile(filename)
    Set outFile = fso.OpenTextFile(filename & ".tmp", 2)
    
    Do Until inFile.AtEndOfTextStream
      line = inFile.ReadLine
      if InStr(line, "CPU") = 0 Then outFile.WriteLine line
    Loop
    
    inFile.Close
    outFile.Close
    
    fso.DeleteFile filename, True
    fso.MoveFile filename & ".tmp", filename
    

    不过,批处理文件可能是实现此目的的更简单方法:

    findstr /v "CPU" your.csv >your.csv.tmp
    del your.csv
    ren your.csv.tmp your.csv
    

    【讨论】:

    • 这太棒了!!!但是如果一个单元格包含 CPU 字,我该怎么删除整个 ROW 我知道它与 InSRT 有一些关系,但我真的不明白如何植入它我在这里找到了这个例子 'If InStr(1, ThisWorkbook. ActiveSheet.Cells(i, 1).Text, "CPU", vbTextCompare) > 0 Then' 和 'ThisWorkbook.ActiveSheet.Cells(i, 1).EntireRow.Delete' 而且我没有预定义的 RANGE 它可以是10 行,它可以是 110 ,所以我希望它通过所有填充的 ROWS 谢谢!!!
    • 查看更新的答案。如果它确实解决了您的问题,请仅接受答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-29
    相关资源
    最近更新 更多