Split Tasks
Sometimes it is not possible to complete a task as scheduled and it needs to be split into two or more parts. Aspose.Tasks for C++ API supports this Microsoft Project feature.
Splitting Tasks
The SplitParts property exposed by the Task class is used to determine the split parts of a task whereas SplitTask method exposed by the ResourceAssignment class is used to split a single task into multiple parts. SplitParts returns a collection of split parts whereas SplitTask method accepts start date, finish date and calendar arguments to split the task.
Splitting and Viewing Tasks in Microsoft Project
To split a task in Microsoft Project:
- Identify a long task and you want to divide.
- On the Task menu, select the Schedule group and click Split Task.
- Click at the position you want to split the task.
To see split tasks in Microsoft Project one can select Gantt Chart on the View menu. Split tasks are linked by a dotted line.
Creating and Splitting Task
To create and split a task, follow these steps:
- Create a new project.
- Create and assign a new calendar for the project.
- Create and add a new task in the project.
- Create and add a new resource assignment in the project.
- Use the SplitTask method exposed by the ResourceAssignment class to split the task.
- Write the new project to the disk.
The following code shows how to accomplish these tasks:
1// Create new project
2System::SharedPtr<Project> splitTaskProject = System::MakeObject<Project>();
3
4// Get a standard calendar
5System::SharedPtr<Calendar> calendar = splitTaskProject->Get<System::SharedPtr<Calendar>>(Prj::Calendar());
6
7// Set project's calendar settings
8splitTaskProject->Set(Prj::StartDate(), System::DateTime(2000, 3, 15, 8, 0, 0));
9splitTaskProject->Set(Prj::FinishDate(), System::DateTime(2000, 4, 21, 17, 0, 0));
10
11// Add a new task to root task
12System::SharedPtr<Task> rootTask = splitTaskProject->get_RootTask();
13rootTask->Set<System::String>(Tsk::Name(), u"Root");
14System::SharedPtr<Task> taskToSplit = rootTask->get_Children()->Add(u"Task1");
15taskToSplit->Set<Duration>(Tsk::Duration(), splitTaskProject->GetDuration(3));
16
17// Create a new resource assignment and generate timephased data
18System::SharedPtr<ResourceAssignment> splitResourceAssignment = splitTaskProject->get_ResourceAssignments()->Add(taskToSplit, nullptr);
19splitResourceAssignment->TimephasedDataFromTaskDuration(calendar);
20
21// Split the task into 3 parts.
22// Provide start date and finish date arguments to SplitTask method which will be used for split
23splitResourceAssignment->SplitTask(System::DateTime(2000, 3, 16, 8, 0, 0), System::DateTime(2000, 3, 16, 17, 0, 0), calendar);
24splitResourceAssignment->SplitTask(System::DateTime(2000, 3, 18, 8, 0, 0), System::DateTime(2000, 3, 18, 17, 0, 0), calendar);
25splitResourceAssignment->Set<WorkContourType>(Asn::WorkContour(), Aspose::Tasks::WorkContourType::Contoured);
26
27// Save the Project
28System::String dataDir = RunExamples::GetDataDir(System::Reflection::MethodBase::GetCurrentMethod(ASPOSE_CURRENT_FUNCTION)->get_DeclaringType().get_FullName());
29splitTaskProject->Save(dataDir + u"CreateSplitTasks_out.xml", Aspose::Tasks::Saving::SaveFileFormat::XML);
Viewing Split Tasks with Aspose.Tasks
The following example shows how to identify split tasks and printing their details to a console window.
1// Create project instance
2System::String dataDir = RunExamples::GetDataDir(System::Reflection::MethodBase::GetCurrentMethod(ASPOSE_CURRENT_FUNCTION)->get_DeclaringType().get_FullName());
3System::SharedPtr<Project> project1 = System::MakeObject<Project>(dataDir + u"ViewSplitTasks.mpp");
4
5// Access task
6System::SharedPtr<Task> splitTask = project1->get_RootTask()->get_Children()->GetById(4);
7
8// Display split parts of task
9System::SharedPtr<SplitPartCollection> collection = splitTask->get_SplitParts();
10
11{
12 auto splitPart_enumerator = (collection)->GetEnumerator();
13 decltype(splitPart_enumerator->get_Current()) splitPart;
14 while (splitPart_enumerator->MoveNext() && (splitPart = splitPart_enumerator->get_Current(), true))
15 {
16 System::Console::WriteLine(System::String(u"Index: ") + splitPart->get_Index() + u"\nStart: " + splitPart->get_Start() + u"\nFinish: " + splitPart->get_Finish() + u"\n");
17 }
18}
Calculating a Split Task’s Finish Date
The following code example calculates finish date for a split task.
1// Read project
2System::String dataDir = RunExamples::GetDataDir(System::Reflection::MethodBase::GetCurrentMethod(ASPOSE_CURRENT_FUNCTION)->get_DeclaringType().get_FullName());
3System::String projectName = u"SplitTaskFinishDate.mpp";
4System::SharedPtr<Project> project = System::MakeObject<Project>(dataDir + projectName);
5
6// Find a split task
7System::SharedPtr<Task> splitTask = project->get_RootTask()->get_Children()->GetByUid(4);
8
9// Find the project calendar
10System::SharedPtr<Calendar> calendar = project->Get<System::SharedPtr<Calendar>>(Prj::Calendar());
11
12// Calculate task's finish date with different durations
13System::Console::WriteLine(System::String(u"Start Date: ") + splitTask->Get<System::DateTime>(Tsk::Start()).ToShortDateString() + u"\n+ Duration 8 hours\nFinish Date: " + calendar->GetTaskFinishDateFromDuration(splitTask, System::TimeSpan(8, 0, 0)));
14System::Console::WriteLine(System::String(u"Start Date: ") + splitTask->Get<System::DateTime>(Tsk::Start()).ToShortDateString() + u"\n+ Duration 16 hours\nFinish Date: " + calendar->GetTaskFinishDateFromDuration(splitTask, System::TimeSpan(16, 0, 0)));
15System::Console::WriteLine(System::String(u"Start Date: ") + splitTask->Get<System::DateTime>(Tsk::Start()).ToShortDateString() + u"\n+ Duration 24 hours\nFinish Date: " + calendar->GetTaskFinishDateFromDuration(splitTask, System::TimeSpan(24, 0, 0)));
16System::Console::WriteLine(System::String(u"Start Date: ") + splitTask->Get<System::DateTime>(Tsk::Start()).ToShortDateString() + u"\n+ Duration 28 hours\nFinish Date: " + calendar->GetTaskFinishDateFromDuration(splitTask, System::TimeSpan(28, 0, 0)));
17System::Console::WriteLine(System::String(u"Start Date: ") + splitTask->Get<System::DateTime>(Tsk::Start()).ToShortDateString() + u"\n+ Duration 32 hours\nFinish Date: " + calendar->GetTaskFinishDateFromDuration(splitTask, System::TimeSpan(32, 0, 0)));
18System::Console::WriteLine(System::String(u"Start Date: ") + splitTask->Get<System::DateTime>(Tsk::Start()).ToShortDateString() + u"\n+ Duration 46 hours\nFinish Date: " + calendar->GetTaskFinishDateFromDuration(splitTask, System::TimeSpan(46, 0, 0)));
19System::Console::WriteLine(System::String(u"Start Date: ") + splitTask->Get<System::DateTime>(Tsk::Start()).ToShortDateString() + u"\n+ Duration 61 hours\nFinish Date: " + calendar->GetTaskFinishDateFromDuration(splitTask, System::TimeSpan(61, 0, 0)));
20System::Console::WriteLine(System::String(u"Start Date: ") + splitTask->Get<System::DateTime>(Tsk::Start()).ToShortDateString() + u"\n+ Duration 75 hours\nFinish Date: " + calendar->GetTaskFinishDateFromDuration(splitTask, System::TimeSpan(75, 0, 0)));
21System::Console::WriteLine(System::String(u"Start Date: ") + splitTask->Get<System::DateTime>(Tsk::Start()).ToShortDateString() + u"\n+ Duration 80 hours\nFinish Date: " + calendar->GetTaskFinishDateFromDuration(splitTask, System::TimeSpan(80, 0, 0)));
22System::Console::WriteLine(System::String(u"Start Date: ") + splitTask->Get<System::DateTime>(Tsk::Start()).ToShortDateString() + u"\n+ Duration 120 hours\nFinish Date: " + calendar->GetTaskFinishDateFromDuration(splitTask, System::TimeSpan(120, 0, 0)));
23System::Console::WriteLine(System::String(u"Start Date: ") + splitTask->Get<System::DateTime>(Tsk::Start()).ToShortDateString() + u"\n+ Duration 150 hours\nFinish Date: " + calendar->GetTaskFinishDateFromDuration(splitTask, System::TimeSpan(150, 0, 0)));