LotusScript - 导出表单,部分字段除外
LotusScript - Export Form except some fields
我使用以下站点作为将 Lotus Notes 数据库表单导出到 csv 文件的指南。
http://searchdomino.techtarget.com/tip/How-to-export-data-from-a-Lotus-Notes-database-to-a-CSV-file
Sub Initialize
Dim session As New NotesSession
Dim db As NotesDatabase
Dim fileName As String
Dim fileNum As Integer
Dim headerstring As String
Dim values As String
Dim selection As String
Dim collection As NotesDocumentCollection
Dim doc As notesdocument
On Error Resume Next
Set db = session.CurrentDatabase
Forall form In db.Forms
If Isempty(form.Fields) Then
Messagebox form.Name & " has no fields"
Else
'Specify what form you want to export
If form.Name = "Server Information" Then
fieldCount = 0
msgString = ""
fileNum% = Freefile()
fileName$ = "c:\temp\LOTUS_EXPORT\" & form.Name & ".csv"
Open FileName$ For Output As fileNum%
Forall field In form.Fields
msgString = msgString & Chr(10) & _
"" & field
fieldCount = fieldCount + 1
headerstring=headerstring & |"| &field &|",|
End Forall
Write #fileNum%, |",| & headerstring & |"|
headerstring=""
Else
End If
End If
selection = |Form="| & form.Name & |"|
Set collection=db.Search(selection, Nothing, 0)
For x = 1 To collection.count
Set doc =collection.GetNthDocument(x)
values=""
Forall formfield In form.Fields
Forall formfield.value != 'AdditionalDocumentation'
newvalue=doc.GetItemValue(formfield)
values=values & |"| & newvalue(0) & |",|
End Forall
End Forall
Write #fileNum%, |",| & values &|"|
values=""
Next
'now check aliases
If Isempty(form.Aliases) Then
'Messagebox form.Name & " has no aliases"
Else
Forall aliaz In form.Aliases
If aliaz = form.Name Then
Goto NextAliaz 'alias is same as form name
End If
selection = |Form="| & aliaz & |"|
Set collection=db.Search(selection, Nothing, 0)
For x = 1 To collection.count
Set doc =collection.GetNthDocument(x)
values=""
Forall formfield In form.Fields
newvalue=doc.GetItemValue(formfield)
values=values & |"| & newvalue(0) & |",|
End Forall
Write #fileNum%, |",| & values &|"|
values=""
NextAliaz:
Next
End Forall
End If
Close fileNum%
End Forall
End Sub
哪个更简单,我想指定要导出的字段或导出除一组特定字段之外的整个表单。
form.Fields
returns 所有字段的 names。测试此字段名称以排除其中的一些:
...
ForAll formfield In form.Fields
If formfield <> "AdditionalDocumentation" And formfield <> "AnotherFieldName" Then
newvalue=doc.GetItemValue(formfield)
values=values & |"| & CStr(newvalue(0)) & |",|
End If
End ForAll
Knut Herrmann 在他的回答中建议的另一种方法是创建一个要导出的字段列表,然后在 ForAll 循环中测试 eaxch 字段是否是该列表的成员:
Dim exportField List As String
exportField("FieldOne") = "FieldOne"
exportField("FieldTwo") = "FieldTwo"
exportField("FieldFive") = "FieldFive"
循环:
ForAll formfield In form.Fields
If IsElement(exportField(formfield)) Then
newvalue=doc.GetItemValue(formfield)
values=values & |"| & CStr(newvalue(0)) & |",|
End If
End ForAll
我使用字符串列表是有原因的。您可以放置特殊的格式化命令或指示数据类型,而不是将字段名称放在那里。然后在循环中使用它相应地格式化 CSV 输出:
Dim exportField List As String
exportField("FieldOne") = "T" '*** Text
exportField("FieldTwo") = "DT" '*** Date and Time
exportField("FieldFive") = "N" '*** Numeric
exportField("FieldSix") = "D" '*** Date only
然后您只需检查值并正确格式化输出即可。
我使用以下站点作为将 Lotus Notes 数据库表单导出到 csv 文件的指南。
http://searchdomino.techtarget.com/tip/How-to-export-data-from-a-Lotus-Notes-database-to-a-CSV-file
Sub Initialize
Dim session As New NotesSession
Dim db As NotesDatabase
Dim fileName As String
Dim fileNum As Integer
Dim headerstring As String
Dim values As String
Dim selection As String
Dim collection As NotesDocumentCollection
Dim doc As notesdocument
On Error Resume Next
Set db = session.CurrentDatabase
Forall form In db.Forms
If Isempty(form.Fields) Then
Messagebox form.Name & " has no fields"
Else
'Specify what form you want to export
If form.Name = "Server Information" Then
fieldCount = 0
msgString = ""
fileNum% = Freefile()
fileName$ = "c:\temp\LOTUS_EXPORT\" & form.Name & ".csv"
Open FileName$ For Output As fileNum%
Forall field In form.Fields
msgString = msgString & Chr(10) & _
"" & field
fieldCount = fieldCount + 1
headerstring=headerstring & |"| &field &|",|
End Forall
Write #fileNum%, |",| & headerstring & |"|
headerstring=""
Else
End If
End If
selection = |Form="| & form.Name & |"|
Set collection=db.Search(selection, Nothing, 0)
For x = 1 To collection.count
Set doc =collection.GetNthDocument(x)
values=""
Forall formfield In form.Fields
Forall formfield.value != 'AdditionalDocumentation'
newvalue=doc.GetItemValue(formfield)
values=values & |"| & newvalue(0) & |",|
End Forall
End Forall
Write #fileNum%, |",| & values &|"|
values=""
Next
'now check aliases
If Isempty(form.Aliases) Then
'Messagebox form.Name & " has no aliases"
Else
Forall aliaz In form.Aliases
If aliaz = form.Name Then
Goto NextAliaz 'alias is same as form name
End If
selection = |Form="| & aliaz & |"|
Set collection=db.Search(selection, Nothing, 0)
For x = 1 To collection.count
Set doc =collection.GetNthDocument(x)
values=""
Forall formfield In form.Fields
newvalue=doc.GetItemValue(formfield)
values=values & |"| & newvalue(0) & |",|
End Forall
Write #fileNum%, |",| & values &|"|
values=""
NextAliaz:
Next
End Forall
End If
Close fileNum%
End Forall
End Sub
哪个更简单,我想指定要导出的字段或导出除一组特定字段之外的整个表单。
form.Fields
returns 所有字段的 names。测试此字段名称以排除其中的一些:
...
ForAll formfield In form.Fields
If formfield <> "AdditionalDocumentation" And formfield <> "AnotherFieldName" Then
newvalue=doc.GetItemValue(formfield)
values=values & |"| & CStr(newvalue(0)) & |",|
End If
End ForAll
Knut Herrmann 在他的回答中建议的另一种方法是创建一个要导出的字段列表,然后在 ForAll 循环中测试 eaxch 字段是否是该列表的成员:
Dim exportField List As String
exportField("FieldOne") = "FieldOne"
exportField("FieldTwo") = "FieldTwo"
exportField("FieldFive") = "FieldFive"
循环:
ForAll formfield In form.Fields
If IsElement(exportField(formfield)) Then
newvalue=doc.GetItemValue(formfield)
values=values & |"| & CStr(newvalue(0)) & |",|
End If
End ForAll
我使用字符串列表是有原因的。您可以放置特殊的格式化命令或指示数据类型,而不是将字段名称放在那里。然后在循环中使用它相应地格式化 CSV 输出:
Dim exportField List As String
exportField("FieldOne") = "T" '*** Text
exportField("FieldTwo") = "DT" '*** Date and Time
exportField("FieldFive") = "N" '*** Numeric
exportField("FieldSix") = "D" '*** Date only
然后您只需检查值并正确格式化输出即可。