如何在Excel中实现记录器?

时间:2014-05-06 07:33:58

标签: arrays excel vba logging

我试图为Excel实现logger,WONT logg用户更改为单元格值(谁关心...)。

但我的应用程序的内部动态。

所以:

  1. 必须是基于表格的。 (因为用户将无法记住 附上一些来自陌生地方的奇怪文件......)
  2. 一定很容易出类拔萃。 (Excel非常擅长简单分析,对吗? 我不想为日志编写任何自定义解析器。只需使用内置 Excel的功能)
  3. 它必须是有弹性的,有时我会 记录附加信息,有时候那些将是变量 数组,对象等。
  4. 问:如何处理3)? ParamArray会将传入的数组视为单个元素。使用Array / Collection时,需要在loggin时将标量值预处理到一个项目数组/集合中。

    到目前为止,这是我的代码:

    Public Sub Log(level As LoggerSeverityLevel, functionName As String, message As   String, Optional Arguments As Variant)
    Dim sh As Object
    Set sh = ActiveSheet
    
    LoggerDB.Activate
    ''' Find blank row for next entry
    Dim firstEmptyRow As LongLong
    firstEmptyRow = Range("A" & rows.count).End(xlUp).row + 1
    
    ''' Parse level into human redable format
    Dim lvlMessage As String
    lvlMessage = "Unknown"
    If level = lslInfo Then lvlMessage = "Info"
    If level = lslWarning Then lvlMessage = "Warning"
    If level = lslDebug Then lvlMessage = "Debug"
    If level = lslCritical Then lvlMessage = "Critical"
    
    ''' Insert data
    LoggerDB.Cells(firstEmptyRow, 1) = Now()
    LoggerDB.Cells(firstEmptyRow, 2) = lvlMessage
    LoggerDB.Cells(firstEmptyRow, 3) = functionName
    LoggerDB.Cells(firstEmptyRow, 4) = message
    
    ''' And optional arguments, one cell per argument
    Dim i As Long
    Dim arg As Variant
    i = 4
    For Each arg In Arguments
        LoggerDB.Cells(firstEmptyRow, i) = CStr(arg)
        i = i + 1
    Next arg
    
    sh.Activate
    End Sub
    

1 个答案:

答案 0 :(得分:0)

Public Sub Log(level As LoggerSeverityLevel, functionName As String, message As String,    Optional Arguments As Variant)
Dim sh As Object
Set sh = ActiveSheet

LoggerDB.Activate
''' Find blank row for next entry
Dim firstEmptyRow As LongLong
firstEmptyRow = Range("A" & rows.count).End(xlUp).row + 1

''' Parse level into human redable format
Dim lvlMessage As String
lvlMessage = "Unknown"
If level = lslInfo Then lvlMessage = "Info"
If level = lslWarning Then lvlMessage = "Warning"
If level = lslDebug Then lvlMessage = "Debug"
If level = lslCritical Then lvlMessage = "Critical"

''' Insert data
LoggerDB.Cells(firstEmptyRow, 1) = Now()
LoggerDB.Cells(firstEmptyRow, 2) = lvlMessage
LoggerDB.Cells(firstEmptyRow, 3) = functionName
LoggerDB.Cells(firstEmptyRow, 4) = message

''' And optional arguments, one cell per argument
Dim i As Long
Dim arg As Variant
i = 5
Dim tmp As Variant
Dim coll As Collection

此部分将标量(数字,字符串,bool,无论单个值)打包到集合中。 因此,我总是可以假设,如果传递任何其他参数,For Each将起作用。

它在TypeName()上传递,返回字符串为“()”的字符串。

  

如果Not IsMissing(Arguments)那么           '如果不能将每个(单个变量)包迭代到集合中。           如果不是(InStr(TypeName(Arguments),“()”)<> 0或TypeName(Arguments)=“Collection”或TypeName(Arguments)=   “字典”)然后               设置coll = New Collection               coll.add参数               设置tmp = coll           其他               tmp =参数           结束如果

    For Each arg In tmp
        LoggerDB.Cells(firstEmptyRow, i) = CStr(arg)
        i = i + 1
    Next arg
End If

sh.Activate
End Sub