Check Formula Syntax
October 15, 2005 – 3:53 pmBeim Erstellen von Aktionen, die in Masken oder Ansichten eingebunden werden sollen überprüft Lotus Notes automatisch beim Speichern, ob die Syntax der eingegebenen Formel richtig ist.
Was aber, wenn man eine Formel an ein db.search übergeben möchte? Man kann natürlich die zu verwendende Formel im Developer Client von Lotus Notes überprüfen lassen und dann in seinen Script Code einbinden. Das funktioniert solange, wie man mit starren Formeln arbeitet.
Wie prüft man aber die Validität einer Formel, wenn diese z.B. über ein Konfigurationsdokument eingegeben wird.
( z.B. in !!HELP!! ) ?
Seit Version 6 gibt es die Formel @CheckFormulaSyntax( Feldname ). Feldname enthält die zu prüfende Formel.
1 2 3 4 5 6 7 8 9 10 11 12 | CAPTION := "Formula Syntax Check"; SYNTAX_OK:= "Formula Syntax is OK"; Result := @CheckFormulaSyntax(nFormula); Error := Result[1]; Line:= Result[2]; Column:= Result[3]; @If(Error = "1"; @Prompt([Ok]; CAPTION; SYNTAX_OK); @Prompt([Ok]; CAPTION; "Line " + Line + " Column " + Column +": " + Error) ) |
Wie ich eingangs erwähnt habe, gibt es die Formel erst ab Version 6. Aber auch unter R5 lässt sich die syntaktische Richtigkeit einer Formel überprüfen. Dazu bedarf es aber der Notes API. Die hier beschriebene Version funktioniert auf Windows Clients.
Die Declaration Section enthält folgenden Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | Const NULLHANDLE = 0 Const NO_ERROR = 0 Const ERR_FORMULA_COMPILATION = &h500 + 1 ' =========================================================================== ' CheckSelectionFormulaValid - ' This function uses the Lotus C API to check the syntax of a Notes formula. ' Return Value: Variant - A 3 elements array containing: ' Index 0 - The compilation error code or NO_ERROR (0) if valid ' Index 1 - The compilation error offset in formula or NO_ERROR if valid ' Index 2 - The compilation error length or NO_ERROR if valid ' ' Note: The compilation error code at Index 0 can then be passed to GetAPIError() ' to get more info about the error. ' =========================================================================== ' =========================================================================== ' GetAPIError - This function uses the Lotus C API to return ' a Notes error's text message. ' ' Return Value: String - The text associated with the Notes API error code. ' =========================================================================== Declare Function NSFFormulaCompile Lib "nnotes.dll" ( _ Byval FormulaName As Long, _ Byval FormulaNameLength As Integer, _ Byval FormulaText As Lmbcs String, _ Byval FormulaTextLength As Integer, _ rethFormula As Long, _ retFormulaLength As Integer, _ retCompileError As Integer, _ retCompileErrorLine As Integer, _ retCompileErrorColumn As Integer, _ retCompileErrorOffset As Integer, _ retCompileErrorLength As Integer _ ) As Integer Declare Sub OSMemFree Lib "nnotes.dll" (Byval hHandle As Long) Declare Function OSLoadString Lib "nnotes.dll" ( _ Byval hmodule As Long, _ Byval status As Integer, _ Byval s As String, _ Byval slen As Integer _ ) As Integer |
Ausserdem werden noch zwei Funktionen benötigt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | Function CheckSelectionFormulaValid(sFormula) As Variant Dim iError As Integer Dim hFormula As Long Dim wFormulaLen As Integer Dim iCompileError As Integer Dim iCompileErrorLine As Integer Dim iCompileErrorColumn As Integer Dim iCompileErrorOffset As Integer Dim iCompileErrorLength As Integer Dim iArray(2) As Integer iError = NSFFormulaCompile(0, 0, _ sFormula, _ Len(sFormula), _ hFormula, _ wFormulaLen, _ iCompileError, _ iCompileErrorLine, _ iCompileErrorColumn, _ iCompileErrorOffset, _ iCompileErrorLength) If hFormula <> NULLHANDLE Then Call OSMemFree(hFormula) End If If iError = ERR_FORMULA_COMPILATION Then iArray(0) = iCompileError iArray(1) = iCompileErrorOffset iArray(2) = iCompileErrorLength CheckSelectionFormulaValid = iArray Else iArray(0) = NO_ERROR iArray(1) = NO_ERROR iArray(2) = NO_ERROR CheckSelectionFormulaValid = iArray End If End Function Function GetAPIError(iErrorCode As Integer) As String Dim iRetVal As Integer Dim sError As String * 1024 sError = String(1024, 0) iRetVal = OSLoadString(0&, iErrorCode, sError, 1023) If iRetval <> 0 Then GetApiError = Left$(sError, iRetVal) End If End Function |
Der vorstehende Code sollte in einer ScriptLibrary gespeichert werden. Der Aufruf des Formelcheckers erfolgt über eine Schaltfläche, die diesen Code enthält
1 2 3 4 5 6 7 8 9 10 11 | Sub Click(Source As Button) Dim workspace As New NotesUIWorkspace Dim uidoc As NotesUIDocument Set uidoc = workspace.CurrentDocument iRC = CheckSelectionFormulaValid( uidoc.FieldGetText( "nFormula" )) If iRC(0) = NO_ERROR Then Messagebox "Formula is valid!", 64 , "OK !" Else Messagebox GetAPIError(iRC(0)) & " - Pos: " & Cstr(iRC(1)), 16, "Error in Formula" End If End Sub |





One Response to “Check Formula Syntax”
ich hab das bisher immer ganz simpel über LS Fehlerbehandlung gelöst
sub testFormula(f as string)
on error goto errorHandler
Dim v as variant
v = evaluate(f)
messagebox “Formel ist syntaktisch korrekt.”
errorExit:
exit sub
errorHandler:
messagebox “Formel enthält Fehler.”
resume errorExit
end sub
By Julian Buss on Oct 15, 2005