Hello community
i Need help to change code i have found for importing a csv file.
i have very low experience in script.
The script works fine only one Change i need.
In the Agent i can define the fields, the view etc. One Point is. Delete docs in DB where is no entry in the csv.
What i want is. The Docs they are not in the csv i dont want to delete them.
But i want that the script write in a existing Field the Text “not present”
In script library in found
Call Me.ImportDatabase.GetDocumentByUNID(ListTag(x)).Remove(True)
in easy words in need
Call Me.ImportDatabase.GetDocumentByUNID(ListTag(x)).writeText"not present" inField xxxx
Heres the Agent:
##############################################
Use “ImportCSV”
Sub Initialize
Dim csv As CSVFile
Dim st_Fields(1 To 10) As String
Dim type_Fields(1 To 10) As Variant
st_Fields(1) = “System” ’ Field is primary key
st_Fields(2) = “null”
st_Fields(3) = “Kundentext”
st_Fields(4) = “Lokation”
st_Fields(5) = “null”
st_Fields(6) = “Funktionstyp”
st_Fields(7) = “Seriennummer”
st_Fields(8) = “null”
st_Fields(9) = “Hardware”
st_Fields(10) = “Hersteller”
type_Fields(1) = “String”
type_Fields(2) = “String”
type_Fields(3) = “String”
type_Fields(4) = “String”
type_Fields(5) = “String”
type_Fields(6) = “String”
type_Fields(7) = “String”
type_Fields(8) = “String”
type_Fields(9) = “String”
type_Fields(10) = “String”
’ Form
Set csv = New CSVFile(“”,“Hardware”, st_Fields, type_Fields)
’ sync with primary key → TRUE - add Docs → False
csv.Synchronize = True
'View to compare
csv.SynchronizeView = “(HilfsAnsichtImportHW)”
'delete Docs which are not in csv → true dont delete-> false
csv.SynchronizeDeletions = true
'CSV Delimiter
csv.Delimiter = “;”
csv.NumChanges = 0
csv.NumAdds = 0
csv.NumDeletes = 0
Call csv.Import
End Sub
###########################################################
script library:
Option Public
Class CSVFile
Public FileName As String
Public Delimiter As String
Public ImportDatabase As NotesDatabase
Public ImportForm As String
Public ImportFields As Variant ’ ARRAY OF FIELD NAMES - INDEX MUST START WITH 1
Public TypeFields As Variant ’ ARRAY OF FIELD TYPES - INDEX MUST START WITH 1
Public RefreshForm As Variant
Public ShowProgress As Variant
Public QuoteDelimiter As Variant ’ IS A DOUBLE QUOTE USED TO SEPARATE VALUES WITH COMMAS
Public Synchronize As Variant ’ SHOULD THE IMPORT SYNCH BASED ON KEY VALUES
Public NumKeyFields As Integer ’ HOW MANY OF THE FIRST FIELDS ARE KEY VALUES
Public SynchronizeView As String ’ NAME OF VIEW TO USE TO SYNCHRONIZE USING KEY VALUES
Public SynchronizeDeletions As Variant
Public SkipTitleLine As Variant
Public NumChanges As Integer
Public NumAdds As Integer
Public NumDeletes As Integer
Private SynchUNIDS List As String
Sub New(st_FileName As String, st_FormName As String, v_FieldArray As Variant, v_TypeArray As Variant)
Dim lib_s As New NotesSession
Dim lib_ws As New NotesUIWorkspace
Dim v_ReturnValue As Variant
If Trim(st_FileName) = “” Then ’ IF NO FILE NAME GIVEN - PROMPT FOR FILE
v_ReturnValue = lib_ws.OpenFileDialog(False, “Select a CSV file”, “CSV Files|*.CSV”, "C:")
If Not IsArray(v_ReturnValue) Then Exit Sub
If v_ReturnValue(0) = “” Then Exit Sub
st_FileName = v_ReturnValue(0)
End If
Me.FileName = st_FileName
Set Me.ImportDatabase = lib_s.CurrentDatabase
Me.ImportForm = st_FormName
Me.ImportFields = v_FieldArray
Me.TypeFields = v_TypeArray
Me.RefreshForm = True
Me.ShowProgress = True
Me.QuoteDelimiter = True
Me.NumKeyFields = 1
Me.NumChanges = 0
Me.NumAdds = 0
Me.NumDeletes = 0
End Sub
Sub Import
Dim i_FileNum As Integer
Dim st_LineData As String
Dim i_ImportCount As Integer
Dim vw_Lookup As NotesView
Dim doc_List As NotesDocument
If Trim(Me.FileName) = “” Then
MessageBox “Keine CSV Datei ausgewählt.”, 16, “Fehler”
Exit Sub
End If
If Me.Synchronize Then
Set vw_Lookup = Me.ImportDatabase.GetView(Me.SynchronizeView)
If vw_Lookup Is Nothing Then
MessageBox “Invalid synch view name.”, 16, “Error”
Exit Sub
End If
Call vw_Lookup.Refresh ’ REFRESH THE VIEW SO IT HAS MOST RECENT DATA
If Me.SynchronizeDeletions Then
’ BUILD LIST
Set doc_List = vw_Lookup.GetFirstDocument
Do While Not doc_List Is Nothing
Me.SynchUNIDS(doc_List.UniversalID) = False
Set doc_List = vw_Lookup.GetNextDocument(doc_List)
Loop
End If
End If
If Not IsArray(Me.ImportFields) Then
MessageBox “Invalid field name array.”, 16, “Error”
Exit Sub
End If
i_FileNum = FreeFile()
Open Me.FileName For Input As i_FileNum
i_ImportCount = 0
’ SKIP THE TITLE LINE IF SPECIFIED
If Not EOF(i_FileNum) And Me.SkipTitleLine Then Line Input #i_FileNum, st_LineData
Do While Not EOF(i_FileNum)
i_ImportCount = i_ImportCount + 1
If Me.ShowProgress Then Print "Importing record # " & CStr(i_ImportCount)
Line Input #i_FileNum, st_LineData ’ GRAB A LINE FROM THE FILE
If Not Me.Synchronize Then
If Not ImportLine(st_LineData) Then
MessageBox “Error during import.”, 16, “Error”
Close i_FileNum
Exit Sub
End If
Else
If Not SynchLine(st_LineData) Then
MessageBox “Error during synchronization.”, 16, “Error”
Close i_FileNum
Exit Sub
End If
End If
Loop
Close i_FileNum
If Me.Synchronize And Me.SynchronizeDeletions Then
ForAll x In Me.SynchUNIDS
If x = “False” Then ’ IF NO MATCH FOUND IN CSV FILE THEN DELETE DOCUMENT (RECORD)
Call Me.ImportDatabase.GetDocumentByUNID(ListTag(x)).Remove(True)
Me.NumDeletes = Me.NumDeletes + 1
End If
End ForAll
End If
Print “Import completed. " & CStr(i_ImportCount) & " records processed. " & CStr(Me.NumAdds) & " added, " & CStr(Me.NumChanges) & " changed, " & CStr(Me.NumDeletes) & " deleted.”
End Sub
’ IMPORTS THE DATA - CREATES A NEW DOCUMENT FOR EACH LINE IN CSV FILE
Private Function ImportLine(st_LineData As String) As Variant
Dim v_ValueArray As Variant
Dim i As Integer
Dim doc_Current As NotesDocument
ImportLine = True
v_ValueArray = ReturnValueArray(st_LineData) ’ BREAK UP THE DATA INTO AN ARRAY
Set doc_Current = Me.ImportDatabase.CreateDocument
For i = LBound(Me.ImportFields) To UBound(Me.ImportFields)
Call doc_Current.ReplaceItemValue(Me.ImportFields(i), v_ValueArray(i))
Next
doc_Current.Form = Me.ImportForm
If Me.RefreshForm Then Call doc_Current.ComputeWithForm(False, True)
Call doc_Current.Save(True, False)
Me.NumAdds = Me.NumAdds + 1
End Function
’ IMPORTS THE DATA - SYNCHRONIZING THE DOCUMENT BASED ON KEY FIELDS
Private Function SynchLine(st_LineData As String) As Variant
Dim v_ValueArray As Variant
Dim v_Key As Variant
Dim v_TempKey (1 To 10) As Variant
Dim i As Integer
Dim doc_Current As NotesDocument
Dim vw_Lookup As NotesView
Dim v_FieldChange As Variant
SynchLine = True
v_ValueArray = ReturnValueArray(st_LineData) ’ BREAK UP THE DATA INTO AN ARRAY
’ BUILD THE KEY
For i = 1 To Me.NumKeyFields
v_TempKey(i) = v_ValueArray(i)
Next
’ INSTANTIATE THE VIEW
Set vw_Lookup = Me.ImportDatabase.GetView(Me.SynchronizeView)
’ LOOKUP THE DOCUMENT
Set doc_Current = vw_Lookup.GetDocumentByKey(v_ValueArray(1), True)
If doc_Current Is Nothing Then ’ CREATE A BRAND NEW DOCUMENT
Set doc_Current = Me.ImportDatabase.CreateDocument
For i = LBound(Me.ImportFields) To UBound(Me.ImportFields)
Call doc_Current.ReplaceItemValue(Me.ImportFields(i), v_ValueArray(i))
Next
doc_Current.Form = Me.ImportForm
If Me.RefreshForm Then Call doc_Current.ComputeWithForm(False, True)
Call doc_Current.Save(True, False)
Me.NumAdds = Me.NumAdds + 1
Else ’ FOUND A DOCUMENT WITH THE SAME KEY - DETERMINE IF NEED TO UPDATE
If Me.SynchronizeDeletions Then
Me.SynchUNIDS(doc_Current.UniversalID) = True ’ FOUND A DOCUMENT WITH THE SAME KEY - DETERMINE IF NEED TO UPDATE
End If
v_FieldChange = False
For i = LBound(Me.ImportFields) To UBound(Me.ImportFields)
If CStr(doc_Current.GetItemValue(Me.ImportFields(i))(0)) <> CStr(v_ValueArray(i)) Then
v_FieldChange = True
Call doc_Current.ReplaceItemValue(Me.ImportFields(i), v_ValueArray(i))
End If
Next
If v_FieldChange Then
If Me.RefreshForm Then Call doc_Current.ComputeWithForm(False, True)
Call doc_Current.Save(True, False)
Me.NumChanges = Me.NumChanges + 1
End If
End If
End Function
Private Function ReturnValueArray(st_LineData) As Variant ’ BREAKS UP A LINE OF CSV DATA INTO AN ARRAY
Dim i As Integer
Dim doc_Current As NotesDocument
Dim i_NextDelimiter As Integer
Dim v_DoubleQuotes As Variant
Dim v_ValueArray(1 To 150) As Variant
Dim go_on As Boolean
go_on = True
i = 1
Do While go_on
v_DoubleQuotes = False
If Me.QuoteDelimiter = True And Left(st_LineData, 1) = Chr(34) Then ’ FOUND A VALUE IN DOUBLE QUOTES
i_NextDelimiter = InStr(st_LineData,Chr(34) & Me.Delimiter) ’ CLOSING DELIMITER WILL BE ANOTHER SET OF DOUBLE QUOTES + DELIMITER
v_DoubleQuotes = True
Else
i_NextDelimiter = InStr(st_LineData, Me.Delimiter)
End If
If i_NextDelimiter = 0 Then ’ NO DELIMITER FOUND - SO MUST BE LAST VALUE
If v_DoubleQuotes Then
v_ValueArray(i) = Mid(st_LineData, 2, Len(st_LineData) - 2) ’ REMOVE DOUBLE QUOTES
Else
v_ValueArray(i) = st_LineData
End If
go_on = False ’ will exit the loop then
Else
If v_DoubleQuotes Then
v_ValueArray(i) = Mid(st_LineData, 2, i_NextDelimiter - 2)
st_LineData = Trim(Mid(st_LineData, i_NextDelimiter + 2))
Else
v_ValueArray(i) = Trim(Left(st_LineData, i_NextDelimiter - 1))
st_LineData = Trim(Mid(st_LineData, i_NextDelimiter + 1))
End If
End If
Select Case Me.TypeFields(i)
Case “Number” : v_ValueArray(i) = Val(v_ValueArray(i))
Case “Date” : v_ValueArray(i) = DateValue(v_ValueArray(i))
Case “DateTime” : v_ValueArray(i) = DateValue(v_ValueArray(i))+Timevalue(v_ValueArray(i))
Case “Time” : v_ValueArray(i) = TimeValue(v_ValueArray(i))
End Select
If i < UBound(Me.ImportFields) Then
i = i + 1
Else
go_on = False
End If
Loop
ReturnValueArray = v_ValueArray
End Function
End Class
Sub Terminate
Dim NotesUIWorkspace As New NotesUIWorkspace
Call notesuiworkspace.viewrefresh
Call notesuiworkspace.viewrebuild
End Sub