Purpose You want to import data from a Microsoft Project file to a SAP transaction. |
Solution
|
Import data from Microsoft project:
The InputScript will import all tasks from a .MPP file and puts the data into the SAP table. |
Using a JavaScript call: Call the following InputScript, which in this example inserts all tasks from the MS Project file:
CreateTable V[tasktable] taskname taskstart taskend
// Read tasks from MS Project file
callJS gettasks "C:\data\simple_project.mpp" "tasktable"
Set V[k] 1
label nextrow
if not V[k>&V[tasktable.rowcount]]
Set cell[table,RCWBT-PIDEN,&V[k]] "&V[tasktable.taskname.&V[k]]"
Set cell[table,RCWBT-STERM,&V[k]] "&V[tasktable.taskstart.&V[k]]"
Set cell[table,RCWBT-ETERM,&V[k]] "&V[tasktable.taskend.&V[k]]"
Set V[k] "&V[k]" + 1
goto nextrow
endif
if V[r]
message "E: &V[r]" -statusline
else
message "Tasks imported from MS Project" -statusline
endif
The method "gettasks" must be present in the JavaScript library specified in the GuiXT profile and looks like this:
function gettasks(filename, outtab) { // Create MS Project connection and open the file var MS = guixt.CreateObject("MSProject.Application"); MS.Application.FileOpenEx(filename); // MS.Visible = true; var proj = MS.ActiveProject; // We want to go through all the elements in the list var tasks = new Enumerator(proj.Tasks); tasks.moveFirst(); // This is the target list for GuiXT var tasksArray = []; while (tasks.atEnd() == false) { // Create a new task object var tstart = new Date(tasks.item().Start); var tend = new Date(tasks.item().Finish); // Convert to date formate DD.MM.YYYY tstart = tstart.getDay() + "." + tstart.getMonth() + "." + tstart.getFullYear(); tend = tend.getDay() + "." + tend.getMonth() + "." + tend.getFullYear(); var task = { "taskname": tasks.item().Name, "taskstart": tstart, "taskend": tend }; // Add the new task to our list tasksArray.push(task); // Get next element tasks.moveNext(); } // When "createTab" is used in GuiXT, you can easily // put the data in JSON notation into the table guixt.Set(outtab, JSON.stringify(tasksArray)); MS.FileSave(); MS.FileCloseAll(); } |
Using a .NET library: The InputScript from above just slightly changes: The JavaScript call is replaced by a call to the function in the guixt_office.dll file:
// callJS gettasks "C:\data\simple_project.mpp" "tasktable"
callVB r = guixt_office.msproject.gettasks _
filename:="C:\data\simple_project.mpp" _
output:="tasktable"
The corresponding function "gettasks" in VB.NET: ''' <summary> ''' Get all task in a MS Project file ''' </summary> ''' <param name="filename">Path to the MS project file</param> ''' <param name="output">Name of GuiXT longtext variable </param> ''' ''' <returns>empty string if success, errortext otherwise</returns> Public Function GetTasks(filename As String, output As String) As String Dim returnText As String = "" Try 'Create the Application object to interact with MS project Dim projectApplication As Microsoft.Office.Interop.MSProject.Application = New Microsoft.Office.Interop.MSProject.Application() Dim mv As Object = System.Reflection.Missing.Value projectApplication.FileOpenEx(filename, mv, mv, mv, mv, mv, mv, mv, mv, mv, mv, Microsoft.Office.Interop.MSProject.PjPoolOpen.pjPoolReadOnly, mv, mv, mv, mv, mv) ' Get the active project in the file Dim project As Microsoft.Office.Interop.MSProject.Project _ = projectApplication.ActiveProject ' Define a list to put the tasks into Dim tasks As ArrayList = New ArrayList ' Hint: ' We create a list containing rows of dictionary objects ' A dictionary stores key-value pairs ' ' We can convert this to JSON notation later For Each task As Microsoft.Office.Interop.MSProject.Task In project.Tasks If task IsNot Nothing Then ' Grab some of the information of this task and save it Dim td As New Dictionary(Of String, String) td.Item("taskname") = task.Name.ToString td.Item("taskstart") = task.Start.ToString td.Item("taskend") = task.Finish.ToString tasks.Add(td) End If Next ' Convert the list of tasks to JSON format Dim serializer As New JavaScriptSerializer() Dim arrayJSON As String = serializer.Serialize(tasks) ' Save the data into a GuiXT table variable myguixt.SetVariable(output, arrayJSON) ' Hint: If you use createTable in GuiXT, you can use JSON ' to exchange data. All rows are automatically updated by GuiXT Catch ex As Exception ' Something went wrong: Return the message to GuiXT ' so it can be shown on the SAP transaction ' ' e.g. file was not found returnText = ex.Message End Try Return returnText End Function
|
Comparison between JavaScript and .NET Using JavaScript is possible without much effort, but it has the following disadvantage: you have to figure out the methods you need and their parameters, and you have few tools or testing options available. You can therefore also create a .NET library, have tools such as code completion available there and can first test independently of GuiXT and the SAP transaction.
How to create your own .NET dll is described here: |
Download: |
Components InputAssistant + Controls |