Writing Updated Task Data to MPP
Contents
[
Hide
Show
]With Aspose.Tasks for C++ 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 the Project class.
- Load 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 in the project.
1// Create project instance
2System::String dataDir = RunExamples::GetDataDir(System::Reflection::MethodBase::GetCurrentMethod(ASPOSE_CURRENT_FUNCTION)->get_DeclaringType().get_FullName());
3System::String newProject = u"UpdateTaskData.mpp";
4System::SharedPtr<Project> project = System::MakeObject<Project>(dataDir + newProject);
5
6// Set project start date
7project->Set(Prj::StartDate(), System::DateTime(2012, 7, 29, 8, 0, 0));
8
9// Add summary task and set its properties
10System::SharedPtr<Task> summary = project->get_RootTask()->get_Children()->Add(u"Summary task");
11System::SharedPtr<Task> task1 = summary->get_Children()->Add(u"First task");
12task1->Set<Duration>(Tsk::Duration(), project->GetDuration(3));
13task1->Set(Tsk::Deadline(), task1->Get<System::DateTime>(Tsk::Start()).AddDays(10));
14task1->Set<System::String>(Tsk::NotesText(), u"The first task.");
15task1->Set<TimeUnitType>(Tsk::DurationFormat(), Aspose::Tasks::TimeUnitType::MinuteEstimated);
16task1->Set<ConstraintType>(Tsk::ConstraintType(), Aspose::Tasks::ConstraintType::FinishNoLaterThan);
17task1->Set(Tsk::ConstraintDate(), task1->Get<System::DateTime>(Tsk::Deadline()).AddDays(-1));
18
19
20// Create 10 new sub tasks for summary task
21for (int32_t i = 0; i < 10; i++)
22{
23 System::SharedPtr<Task> subTask = summary->get_Children()->Add(System::String::Format(u"Task{0}",i + 2));
24 subTask->Set<Duration>(Tsk::Duration(), task1->Get<Duration>(Tsk::Duration()).Add(project->GetDuration(i + 1)));
25 subTask->Set<TimeUnitType>(Tsk::DurationFormat(), Aspose::Tasks::TimeUnitType::Day);
26 subTask->Set(Tsk::Deadline(), task1->Get<System::DateTime>(Tsk::Deadline()).AddDays(i + 1));
27}
28
29// Save the Project
30project->Save(dataDir + u"project_UpdateTaskData_updated_out.mpp", Aspose::Tasks::Saving::SaveFileFormat::MPP);