Lotus script 技巧

 

 

LotusScript 是完全面向对象的编程语言。它通过预定义的类与 Domino 接口。Domino 监控用户代码的编译和加载,并且自动包含 Domino 的类定义。

  访问现有的对象最好使用 LotusScript,例如:根据其他文档的值来更改一个文档中的值。LotusScript 提供了一些公式没有的功能,例如:操作数据库存取控制列表 (ACL) 的能力。

  写script关键是取对象,查看对象的属性,所以你要学会看notes提供的Script帮助。下面是我收集的一些script例子。一般是比较技巧的程序。

怎样判断视图中没有文档?

  

set doc = vw.getfirstdocument()

 if doc is nothing then

.....

 

 end if

 

  

如何将查询结果放到一个文件夹里?

  

下面是将搜索结果放到名叫newfolder的文件夹中,并跳转到该文件夹上

Sub Click(Source As Button) 

Dim docs As notesdocumentcollection         

Dim doc As notesdocument

...........

q=doc.query(0)

Set docs = db.ftsearch(q, 0)

Call docs.PutAllInFolder( \"newfolder\" )

Call w.OpenDatabase( \"\",\"\",\"newfolder\")

End Sub

  

如何删掉数据库中所有私有视图?

 

Dim session As New notessession 

     Dim db As notesdatabase 

     Dim doc As notesdocument 

     Set db=session.currentdatabase 

     Forall i In db.views 

     Set doc=db.getDocumentByUNID(v.universalID) 

     \' 这个地方视图当作文档来处理,以便取到视图的一些属性。 

     viewflag=doc.getItemvalue(\"$flags\") 

     If viewflag(0)=\"pYV\" Then 

     \' 视图属性中$flags为\"pYV\"的是私有视图。 

          Call i.remove 

     End If 

     End Forall

 

如何在Notes中调用ODBC数据源中的进程?

   

下面是一个利用ODBC调用access数据库(资料库)的script代码

   

Dim session As New NotesSession

Dim con As New ODBCConnection

Dim qry As New ODBCQuery

Dim result As New ODBCResultSet

Set qry.Connection = con

Set result.Query = qry

con.ConnectTo(\"资料库\")

qry.SQL = \"SELECT * FROM 资料库\"

result.Execute

If result.IsResultSetAvailable Then

Do

result.NextRow

id=result.GetValue(\"ID\",id)

Loop Until result.IsEndOfData

result.Close(DB_CLOSE)

Else

Messagebox \"Cannot get result set for AssetData\"

Exit Sub

End If

con.Disconnect

End Sub

  

获得当前视图中选择了的文档?

  

可以用 Notesdatabase 的 Unprocesseddocuments 属性。

 

   Dim session As New notessession

   Dim db As notesdatabase

   Dim collection As notesdocumentcollection

    

   Set db = session.currentdatabase

   Set collection = db.UnprocessedDocuments

 

Unprocesseddocuments 其实很有用的

  

notes和Excel交换数据

   

Dim session As New NotesSession

   Dim db As NotesDatabase

   Dim view As NotesView

   Dim doc As NotesDocument

   Dim excelApplication As Variant

   Dim excelWorkbook As Variant

   Dim excelSheet As Variant

   Dim i As Integer

    

   Set excelApplication = CreateObject(\"Excel.Application\")

   excelApplication.Visible = True

   Set excelWorkbook = excelApplication.Workbooks.Add

   Set excelSheet = excelWorkbook.Worksheets(\"Sheet1\")

   excelSheet.Cells(1,1).Value = \"姓名\"

   excelSheet.Cells(1,2).Value = \"年龄\"

    

   i = 1

   Set db = session.CurrentDatabase

   Set view = db.GetView(\"abc\")

   Set doc = view.GetFirstDocument

   While Not(doc Is Nothing)

     i = i + 1

     excelSheet.Cells(i,1).Value = doc.ClassCategories(0)

     excelSheet.Cells(i,2).Value = doc.Subject(0)

     Set doc = view.GetNextDocument(doc)

   Wend

   excelSheet.Columns(\"A:B\").Select

   excelSheet.Columns(\"A:B\").EntireColumn.AutoFit

    

   excelWorkbook.SaveAs(\"Script 内容\")

   excelApplication.Quit

   Set excelApplication = Nothing 

从后台刷新当前文档?

   

将当前文档先关闭后再打开

set doc=uidoc.document

......

call uidoc.save()

call uidoc.close()

set uidoc=ws.editdocument(doc)

 

在视图中怎样历遍所有的文档?

  

Dim db As New NotesDatabase( \"Ankara\", \"current\\projects.nsf\" )

Dim view As NotesView

Dim doc As NotesDocument

Set view = db.GetView( \"Open\\By Due Date\" )

Set doc = view.GetFirstDocument

While Not ( doc Is Nothing )

....................

 Set doc = view.GetNextDocument( doc )

Wend

  

在scipt中如何调用公式

 

例如我们想要取服务器名的普通名,在script中用@name() ,假设server变量以取到服务器名称

  

在script中用Evaluate可以运行公式,如:servername=Evaluate(\"@name([CN];server)\")

  

 

怎样用script代理取到CGI变量

 

Dim session As New NotesSession

Dim doc As NotesDocument

Set doc = session.DocumentContext

Messagebox \"User = \" + doc.Remote_User(0)

  

如何使用Win32API隐藏菜单呢?

  

1. Declarations :

 

Declare Function GetActiveWindow Lib \"user32.dll\" () As Long

Declare Function SetMenu Lib \"user32.dll\" ( Byval hmenu As Long, Byval newmenu As Long ) As Integer

 

2.

Sub HiddenMenu()

 

Dim hwnd As Long

hwnd = GetActiveWindow()

Call SetMenu(hwnd,0)

 

End Sub 

怎样判断一个RTF为空值

   

Function IsRTFNull(rtfield As String) As Integer  

 On Error Goto Errhandle   

 Dim workspace As New NotesUIWorkspace

 Dim uidoc As NotesUIDocument 

 Set uidoc = workspace.CurrentDocument  

 currentfield = uidoc.CurrentField  

 Call uidoc.GotoField(rtfield) 

 Call uidoc.SelectAll

 Call uidoc.DeselectAll  

 If currentfield <> \"\" Then  

 Call uidoc.GotoField(currentfield) 

 End If  

 IsRTFNull = False   

 Exit Function     

 Errhandle: 

  Select Case Err

  Case 4407

  \'the DeselectAll line generated an error message, indicating that the rich text field does   not contain anything

  If currentfield <> \"\" Then   

  Call uidoc.GotoField(currentfield)  

  End If

  IsRTFNull = True 

  Exit Function 

  Case Else

  \'For any other error, force the same error to cause LotusScript to do the error handling

  Error Err 

  End Select

  End Function

  

怎样返回一个数据的类型

  

Declarations

Class ReturnObj

 Private m_stName As String

 Private m_stType As String

   

 Property Get NameVal As String

  NameVal = m_stName$

 End Property

   

 Property Get TypeVal As String

  TypeVal = m_stType$

 End Property

   

 Sub new( arg_stName$, arg_stType$ )

  m_stName = arg_stName$

  m_stType = arg_stType

 End Sub 

End Class

 

Function Test() As ReturnObj

 Set Test = New ReturnObj( \"Name\", \"Type\" )

End Function

 

Initialize

 Dim var

 Set var = Test()

 Msgbox( var.NameVal )

怎样判断一个文件目录是否存在

 

If Dir$(dirName, ATTR_DIRECTORY) = \"\"

Then   \'Directory does not exist

Else

   \'Directory does exist

End If

  

怎样在lotusScript中运行代理

 

Set s = CreateObject(\"Notes.NotesSession\") 

Set db = s.GETDATABASE(\"\", \"db.nsf\")

 Set a = db.GETAGENT(\"SomeAgent\")

 Call s.SETENVIRONMENTVAR(\"AgentDocID\", \"ABCD\") 

 Call a.RUN

  

怎样才能得到当前数据库的文件路径

  

Public Function

 GetDatabasePath( db As Notesdatabase ) As String

 Dim position As Integer

 position = Instr( db.FilePath, db.FileName )

 GetDatabasePath = Left( db.FilePath , position - 1 )

End Function

  

怎样比较两个日期型的域

 

mdate1V = document.DateField1(0)

mdate2V = document.DateField2(0)

If mdate1V < mdate2V Then 

  MsgBox \"DATE 1 LESS THEN DATE 2\"

  Else

  MsgBox \"DATE 2 LESS THEN OR EQUAL TO DATE 1\"

End If

  

在Script中做到@mailsend

  

Function SendMailMemo(sendTo As String, _

           cc As String, _

           bcc As String, _

           subject As String, _

           body As String, _

           linkTo As NotesDocument) As Integer

 On Error Goto ErrorHandler

 Dim mailDb As New NotesDatabase(\"\", \"\")

 Dim mailDoc As NotesDocument

 Dim rtItem As NotesRichTextItem

 

 Call mailDb.OpenMail

 If (mailDb.IsOpen = False) Then Call mailDb.Open(\"\", \"\")

 Set mailDoc = mailDb.CreateDocument

 mailDoc.Form = \"Memo\"

 mailDoc.SendTo = sendTo

 mailDoc.CC = cc

 mailDoc.BCC = bcc

 mailDoc.Subject = subject

 Set rtItem = mailDoc.CreateRichTextItem(\"Body\")

 Call rtItem.AppendText(body)

 If Not(linkTo Is Nothing) Then

  Call rtItem.AddNewLine(2)

  Call rtItem.AppendDocLink(linkTo, \"Double-click to open document\")

 End If

 Call mailDoc.Send(False)

 SendMailMemo = True

 Exit Function

 

ErrorHandler:

 Print \"Error \" & Str$(Err) & \": \" & Error$

 Resume TheEnd

 

TheEnd:

 SendMailMemo = False

End Function

怎样用lotusScript启动附件

  

首先使用EmbeddedObjects类将附件拆离到一个临时文件夹里,然后用shell命令语句运行它

 

怎样在lotusScript中创建一个姓名域、读者域、作者域

 

创建一个\"specialType\"姓名域

 Dim variableName As New NotesItem( notesDocument, name$, value [,specialType%])

创建一个\"Author\"作者域

 Dim TAuthor As New NotesItem(doc, \"Author\", Auths, AUTHORS)

TAuthor.IsSummary = True

posted @ 2009-12-03 22:48  hannover  阅读(3382)  评论(2编辑  收藏  举报