Writing Updated Task Data to MPP
With Aspose.Tasks for Java it is possible to update tasks and then write the updated data back to a Microsoft Project MPP file.
Updating Task Data The following code snippet shows how to update a project’s task data and write it back to the MPP file. The step involved in this activity are:
- Create an instance of Project Reader.
- Read the source MPP file.
- Create a Task and add it to the root task.
- Update the task parameters such as Deadline, NotesText, etc.
- Add additional tasks to the root task.
- Save the project.
The following code finds a task’s original and external ID through the cross-project.
1// For complete examples and data files, please go to https://github.com/aspose-tasks/Aspose.Tasks-for-Java
2// The path to the documents directory.
3String dataDir = Utils.getDataDir(UpdatedTaskLinkDataToMpp.class);
4
5long OneSec = 10000000;// microsecond * 10
6long OneMin = 60 * OneSec;
7long OneHour = 60 * OneMin;
8
9Project project = new Project(dataDir + "Aspose\\SampleNewMSP.mpp");
10Task task1 = project.getRootTask().getChildren().add("First task");
11
12java.util.Calendar cal = java.util.Calendar.getInstance();
13cal.set(2013, 12, 10, 8, 0, 0);
14project.set(Prj.START_DATE, cal.getTime());
15task1.set(Tsk.START, cal.getTime());
16task1.set(Tsk.DURATION, project.getDuration(OneHour * 24, TimeUnitType.Hour));
17
18Task summary = project.getRootTask().getChildren().add("Summary task");
19summary.getChildren().add(task1);
20
21project.getRootTask().getChildren().add(summary);
22
23cal.setTime(task1.get(Tsk.START));
24cal.add(java.util.Calendar.DATE, 10);
25task1.set(Tsk.DEADLINE, cal.getTime());
26
27task1.set(Tsk.NOTES_TEXT, "The first task.");
28task1.set(Tsk.DURATION_FORMAT, TimeUnitType.DayEstimated);
29task1.set(Tsk.CONSTRAINT_TYPE, ConstraintType.FinishNoLaterThan);
30
31cal.setTime(task1.get(Tsk.DEADLINE));
32cal.add(java.util.Calendar.DATE, -1);
33task1.set(Tsk.CONSTRAINT_DATE, cal.getTime());
34
35// Create 10 new tasks
36for (int i = 0; i < 10; i++) {
37 String taskName = "Task " + (i + 2);
38 Task task = summary.getChildren().add(taskName);
39 task.set(Tsk.START, task1.get(Tsk.START));
40 long lDuration = (long) task1.get(Tsk.DURATION).toDouble();
41 lDuration = (long) (lDuration + (OneHour * 8 * (i + 1)));
42 task.set(Tsk.DURATION, project.getDuration(lDuration, TimeUnitType.Hour));
43 task1.set(Tsk.DURATION_FORMAT, TimeUnitType.Day);
44
45 cal.setTime(task1.get(Tsk.DEADLINE));
46 cal.add(java.util.Calendar.DATE, 1 + i);
47 task.set(Tsk.DEADLINE, cal.getTime());
48 task.set(Tsk.FINISH,
49 project.get(Prj.CALENDAR).getFinishDateByStartAndWork(task.get(Tsk.START), task.get(Tsk.DURATION)));
50 summary.getChildren().add(task);
51}
52
53// Save the Project
54project.save(dataDir + "Aspose\\WritingUpdatedTaskDataToMpp.mpp", SaveFileFormat.MPP);