如何创建文档,然后替换特定文档中字段的值
How to create a document and then replace the values of the fields in the specific document
我有一个代理可以将记录从 Excel 导入到 Notes。现在每次,它 运行 都会创建一个新文档。我想要:
- 第一次 运行 创建文档。
- 下次它将 运行 替换特定文档的字段值,而不是创建新文档。
我该如何修复我的代理:
子初始化
Dim session As New NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim xlApp As Variant, xlsheet As Variant, xlwb As Variant, xlrange As Variant
Dim filename As String, currentvalue As String
Dim batchRows As Integer, batchColumns As Integer, totalColumns As Integer
Dim x As Integer, y As Integer, startrow As Integer
Dim curRow As Long, timer1 As Long, timer2 As Long
Dim DataArray, fieldNames, hasData
Dim view As NotesView
Set db = session.CurrentDatabase
Set view = db.GetView("test-forecast")
Set doc = view.GetFirstDocument
timer1=Timer
filename="C:\DM\Forecast\forecast-a.xlsx"
batchRows=2 'process 2 rows at a time
Set db=session.CurrentDatabase
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True 'set Excel program to run in foreground to see what is happening
Set xlwb=xlApp.Workbooks.Open(filename)
Set xlsheet =xlwb.Worksheets(1)
Redim fieldNames(1 To 5) As String
DataArray=xlsheet.Range("A1").Resize(batchRows, 5).Value 'get worksheet area of specified size
For y=1 To 5 'we assume max 5 columns in the sheet
currentvalue=Cstr(DataArray(1,y))
If currentvalue<>"" Then 'abort counting on empty column
fieldNames(y)=currentvalue 'collect field names from the first row
totalColumns=y
Else
y=2
End If
Next
Redim Preserve fieldNames(1 To totalColumns) As String
curRow=2
hasData=True
While hasData=True 'loop until we get to the end of Excel rows
If curRow=2 Then startrow=2 Else startrow=1
For x=startrow To batchRows
curRow=curRow+1
If Cstr(DataArray(x,1))+Cstr(DataArray(x,2))<>"" Then 'when 2 first columns are empty, we assume that it's the end of data
Print Cstr(curRow-2)
Set doc=New NotesDocument(db)
doc.Form="test-forecast"
doc.Type="test-forecast"
For y=1 To totalColumns
currentvalue=Cstr(DataArray(x,y))
Call doc.ReplaceItemValue(fieldNames(y), currentvalue)
Next
Call doc.save(True, False)
Else
hasData=False
x=batchRows
End If
Next
If hasData=True Then DataArray=xlsheet.Range("A"+Cstr(curRow)).Resize(batchRows, totalColumns).Value 'get worksheet area
Wend
timer2=Timer
Call xlApp.Quit() 'close Excel program
结束子
提前致谢。
根据您的评论,您的 excel- 文件中只有 2 行:第一行包含字段名,第二行包含值。
每次导入仅包含一个文档。并且您想在每个 运行.
上更新此单个文档
有多种方法可以获取该文档,最快的方法是使用包含该文档的视图。使用选择公式创建视图:SELECT Form = "test-forecast"
。给它起一个会说话的名字,比如 (ViwLkpDocument)。如果你真的只有一个文档的导入,那么你可以保留默认列,否则你可以通过某种键对第一列进行排序以识别匹配的文档。
我会为此添加一个功能,以便您以后可以在需求发生变化时更改方法:
Function GetDocument(db as NotesDatabase) As NotesDocument
Dim viwLkp as NotesView
Dim docTmp as NotesDocument
Set viwLkp = db.GetView( "(ViwLkpDocument)" )
Set docTmp = viwLkp.GetFirstDocument
If docTmp is Nothing then
Set docTmp = New NotesDocument( db )
docTmp.Form="test-forecast"
docTmp.Type="test-forecast"
End If
Set GetDocument = docTmp
End Function
然后像这样更改您的代码:
...
Print Cstr(curRow-2)
Set doc=GetDocument(db)
For y=1 To totalColumns
...
我有一个代理可以将记录从 Excel 导入到 Notes。现在每次,它 运行 都会创建一个新文档。我想要:
- 第一次 运行 创建文档。
- 下次它将 运行 替换特定文档的字段值,而不是创建新文档。 我该如何修复我的代理:
子初始化
Dim session As New NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim xlApp As Variant, xlsheet As Variant, xlwb As Variant, xlrange As Variant
Dim filename As String, currentvalue As String
Dim batchRows As Integer, batchColumns As Integer, totalColumns As Integer
Dim x As Integer, y As Integer, startrow As Integer
Dim curRow As Long, timer1 As Long, timer2 As Long
Dim DataArray, fieldNames, hasData
Dim view As NotesView
Set db = session.CurrentDatabase
Set view = db.GetView("test-forecast")
Set doc = view.GetFirstDocument
timer1=Timer
filename="C:\DM\Forecast\forecast-a.xlsx"
batchRows=2 'process 2 rows at a time
Set db=session.CurrentDatabase
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True 'set Excel program to run in foreground to see what is happening
Set xlwb=xlApp.Workbooks.Open(filename)
Set xlsheet =xlwb.Worksheets(1)
Redim fieldNames(1 To 5) As String
DataArray=xlsheet.Range("A1").Resize(batchRows, 5).Value 'get worksheet area of specified size
For y=1 To 5 'we assume max 5 columns in the sheet
currentvalue=Cstr(DataArray(1,y))
If currentvalue<>"" Then 'abort counting on empty column
fieldNames(y)=currentvalue 'collect field names from the first row
totalColumns=y
Else
y=2
End If
Next
Redim Preserve fieldNames(1 To totalColumns) As String
curRow=2
hasData=True
While hasData=True 'loop until we get to the end of Excel rows
If curRow=2 Then startrow=2 Else startrow=1
For x=startrow To batchRows
curRow=curRow+1
If Cstr(DataArray(x,1))+Cstr(DataArray(x,2))<>"" Then 'when 2 first columns are empty, we assume that it's the end of data
Print Cstr(curRow-2)
Set doc=New NotesDocument(db)
doc.Form="test-forecast"
doc.Type="test-forecast"
For y=1 To totalColumns
currentvalue=Cstr(DataArray(x,y))
Call doc.ReplaceItemValue(fieldNames(y), currentvalue)
Next
Call doc.save(True, False)
Else
hasData=False
x=batchRows
End If
Next
If hasData=True Then DataArray=xlsheet.Range("A"+Cstr(curRow)).Resize(batchRows, totalColumns).Value 'get worksheet area
Wend
timer2=Timer
Call xlApp.Quit() 'close Excel program
结束子
提前致谢。
根据您的评论,您的 excel- 文件中只有 2 行:第一行包含字段名,第二行包含值。
每次导入仅包含一个文档。并且您想在每个 运行.
上更新此单个文档有多种方法可以获取该文档,最快的方法是使用包含该文档的视图。使用选择公式创建视图:SELECT Form = "test-forecast"
。给它起一个会说话的名字,比如 (ViwLkpDocument)。如果你真的只有一个文档的导入,那么你可以保留默认列,否则你可以通过某种键对第一列进行排序以识别匹配的文档。
我会为此添加一个功能,以便您以后可以在需求发生变化时更改方法:
Function GetDocument(db as NotesDatabase) As NotesDocument
Dim viwLkp as NotesView
Dim docTmp as NotesDocument
Set viwLkp = db.GetView( "(ViwLkpDocument)" )
Set docTmp = viwLkp.GetFirstDocument
If docTmp is Nothing then
Set docTmp = New NotesDocument( db )
docTmp.Form="test-forecast"
docTmp.Type="test-forecast"
End If
Set GetDocument = docTmp
End Function
然后像这样更改您的代码:
...
Print Cstr(curRow-2)
Set doc=GetDocument(db)
For y=1 To totalColumns
...