如果不存在变量,则替换为0

时间:2018-12-01 13:45:59

标签: java screen-scraping

我正在使用屏幕抓取工具来提取数据并将其输出为CSV。除非会话变量不存在,否则它可以完美工作。我想将代码更改为:如果此变量不存在,请替换为0。

E.G。如果不存在Session.getVariable“ Figure2”,则使用值0。

session.log( "Writing data to a file." ); 

out = new FileWriter( "C:/Users/Documents/Live/data.CSV" );
out.write ("User, Figure 1,Figure 2, Figure 3, Figure 4 ");
out.write ("\n");
out.write ( "User" ) ;
out.write (",");
out.write ( session.getVariable( "Figure1" ) );
out.write (",");
out.write ( session.getVariable( "Figure2" ) );
out.write (",");
out.write ( session.getVariable( "Figure3" ) );
out.write (",");
out.write ( session.getVariable( "Figure4" ) );
out.close();

session.log( "Writing data to a file." ); 

3 个答案:

答案 0 :(得分:1)

您可以使用包含以下逻辑的简单方法来包装变量提取:

Option Explicit
 ' Based on code from Peter Thornton here:
 ' http://social.msdn.microsoft.com/Forums/en-US/7d584120-a929-4e7c-9ec2-9998ac639bea/mouse-scroll-in-userform-listbox-in-excel-2010?forum=isvvba
Private Type POINTAPI
    X                               As Long
    y                               As Long
End Type
Private Type MOUSEHOOKSTRUCT
    pt                              As POINTAPI
    Hwnd                            As Long
    wHitTestCode                    As Long
    dwExtraInfo                     As Long
End Type

Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Private Declare Function GetWindowLong Lib "user32.dll" _
Alias "GetWindowLongA" ( _
ByVal Hwnd As Long, _
ByVal nIndex As Long) As Long

Private Declare Function SetWindowsHookEx Lib "user32" _
Alias "SetWindowsHookExA" ( _
ByVal idHook As Long, _
ByVal lpfn As Long, _
ByVal hmod As Long, _
ByVal dwThreadId As Long) As Long

Private Declare Function CallNextHookEx Lib "user32" ( _
ByVal hHook As Long, _
ByVal nCode As Long, _
ByVal wParam As Long, _
lParam As Any) As Long

Private Declare Function UnhookWindowsHookEx Lib "user32" ( _
ByVal hHook As Long) As Long

Private Declare Function PostMessage Lib "user32.dll" _
Alias "PostMessageA" ( _
ByVal Hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long

Private Declare Function WindowFromPoint Lib "user32" ( _
ByVal xPoint As Long, _
ByVal yPoint As Long) As Long

Private Declare Function GetCursorPos Lib "user32.dll" ( _
ByRef lpPoint As POINTAPI) As Long

Declare Function GetActiveWindow Lib "user32" () As Long

Private Const WH_MOUSE_LL           As Long = 14
Private Const WM_MOUSEWHEEL         As Long = &H20A
Private Const HC_ACTION             As Long = 0
Private Const GWL_HINSTANCE         As Long = (-6)

Private Const WM_KEYDOWN            As Long = &H100
Private Const WM_KEYUP              As Long = &H101
Private Const VK_UP                 As Long = &H26
Private Const VK_DOWN               As Long = &H28
Private Const WM_LBUTTONDOWN        As Long = &H201

Private Const cFRAME_SCROLLCHANGE   As Long = 20
Private Const cTBOX_SCROLLCHANGE    As Long = 1

Private mLngMouseHook               As Long
Private mControlHwnd                As Long
Private mbHook                      As Boolean
Private lngOldLinePos               As Long
Dim mControl                        As Object


Sub HookFormScroll(oControl As Object, strFormCapt As String)
    Dim lngAppInst                  As Long
    Dim hwndUnderCursor             As Long

    Set mControl = oControl
    hwndUnderCursor = FindWindow("ThunderDFrame", strFormCapt)
    Debug.Print "Form window: " & hwndUnderCursor
    If mControlHwnd <> hwndUnderCursor Then
        UnhookFormScroll
        Debug.Print "Unhook old proc"
        mControlHwnd = hwndUnderCursor
        lngAppInst = GetWindowLong(mControlHwnd, GWL_HINSTANCE)
        If Not mbHook Then
            mLngMouseHook = SetWindowsHookEx(WH_MOUSE_LL, AddressOf mouseProc, lngAppInst, 0)
            mbHook = mLngMouseHook <> 0
            If mbHook Then Debug.Print "Form hooked"
        End If
    End If
End Sub

Sub UnhookFormScroll()
    If mbHook Then
        UnhookWindowsHookEx mLngMouseHook
        mLngMouseHook = 0
        mControlHwnd = 0
        mbHook = False
    End If
End Sub

Private Function mouseProc( _
    ByVal nCode As Long, ByVal wParam As Long, _
    ByRef lParam As MOUSEHOOKSTRUCT) As Long
    Dim lngSelStart As Long, lngNewLineLen As Long
    On Error GoTo errH 'Resume Next
    If (nCode = HC_ACTION) Then
        If GetActiveWindow = mControlHwnd Then

            If wParam = WM_MOUSEWHEEL Then
                mouseProc = True
                With mControl
                    If TypeName(mControl) = "Frame" Then
                        If lParam.Hwnd > 0 Then
                            .ScrollTop = Application.Max(0, .ScrollTop - cFRAME_SCROLLCHANGE)
                        Else
                            .ScrollTop = Application.Min(.ScrollHeight - .InsideHeight, .ScrollTop + cFRAME_SCROLLCHANGE)
                        End If
                    Else
                        If TypeName(mControl) = "TextBox" Then
                            If reasonCustKeyPressed Then
                                lngSelStart = .SelStart
                                .CurLine = .CurLine
                                lngOldLinePos = lngSelStart - .SelStart
                                reasonCustKeyPressed = False
                            End If
                            If lParam.Hwnd > 0 Then
                                .CurLine = Application.Max(0, .CurLine - cTBOX_SCROLLCHANGE)
                            Else
                                .CurLine = Application.Min(.LineCount - 1, .CurLine + cTBOX_SCROLLCHANGE)
                            End If
                            lngSelStart = .SelStart
                            If .CurLine < .LineCount - 1 Then
                                .CurLine = .CurLine + 1
                                .SelStart = .SelStart - 1
                            Else
                                .SelStart = Len(.Text)
                            End If
                            lngNewLineLen = .SelStart - lngSelStart
                            .SelStart = Application.Min(lngSelStart + lngOldLinePos, lngSelStart + lngNewLineLen)
                        End If
                    End If
                End With
                Exit Function
            End If
        End If

    End If
    mouseProc = CallNextHookEx( _
    mLngMouseHook, nCode, wParam, ByVal lParam)
    Exit Function
errH:
    UnhookFormScroll
End Function

然后在您的代码中使用它:

private static String variableOrZero(String variable) {
    String retVal = session.getVaraible(variable);
    if (retVal == null) {
        retVal = "0";
    }
    return retVal;
}

答案 1 :(得分:0)

我的解决方案是使用三元运算符Another stackoverflow link for ternary operator discussion

session.log( "Writing data to a file." ); 

out = new FileWriter( "C:/Users/Documents/Live/data.CSV" );
out.write ("User, Figure 1,Figure 2, Figure 3, Figure 4 ");
out.write ("\n");
out.write ( "User" ) ;
out.write (",");
out.write ( session.getVariable( "Figure1" )==null ? 0 :session.getVariable( "Figure1" ));
out.write (",");
out.write ( session.getVariable( "Figure2" )==null?0:session.getVariable("Figure2") );
out.write (",");
out.write ( session.getVariable( "Figure3" ) );
out.write (",");
out.write ( session.getVariable( "Figure4" ) );
out.close();

session.log( "Writing data to a file." ); 

答案 2 :(得分:0)

我不知道您的会话类型。所以我猜session.getVariable()可能返回null。 如果运行此代码:

[pylint] Module 'cv2' has no 'waitKey' member [E1101]

程序将引发NullPointerException。

Java8 Optional类是可以保存空值的容器。我们可以使用它来检查Integer对象是否为空。 另一个错误是write方法是向Stream中写入一个字节。您需要PrintWriter和print方法直接将int写入文件。

import java.io.FileWriter;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        Integer integer = null;
        FileWriter out = new FileWriter( "data.CSV" );
        out.write(integer);
    }
}