Percentage Complete Calculations
Microsoft Project shows the percentage of a task that’s been completed. The percentage can be added manually, by a project manager, or automatically calculated by the application. Aspose.Tasks for C++ supports several percentage calculations related to tasks.
Percentages
The Tsk class exposes a number of properties used to calculate percentages:
- PercentComplete represents the completed percentage of a task’s duration (integer).
- PercentWorkComplete represents the completed percentage of a task’s work (integer).
- PhysicalPercentComplete represents the completed percentage as entered by a project manager (integer).
To see the physical percentage complete in Microsoft Project:
- On the Task Entry form, from the Insert menu, select Column.
- Add the column.
To see the completed percentage in Microsoft Project:
- On the Task Entry form, double-click the desired column.
Getting Percentages
The following code example demonstrates how to get the percentages of work relating to tasks.
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"TaskPercentageCompletion.mpp");
4
5// Access tasks and display percentage completion
6auto tasks = project1->get_RootTask()->get_Children();
7
8{
9 auto tsk1_enumerator = (tasks)->GetEnumerator();
10 decltype(tsk1_enumerator->get_Current()) tsk1;
11 while (tsk1_enumerator->MoveNext() && (tsk1 = tsk1_enumerator->get_Current(), true))
12 {
13 System::Console::WriteLine(tsk1->Get<int32_t>(Tsk::PercentComplete()));
14 System::Console::WriteLine(System::Convert::ToString(tsk1->Get<int32_t>(Tsk::PercentWorkComplete())));
15 System::Console::WriteLine(System::Convert::ToString(tsk1->Get<int32_t>(Tsk::PhysicalPercentComplete())));
16 }
17}
Changing Task Progress
Aspose.Tasks for C++ API supports changing a task’s progress in terms of its percentage completion through the Task class’ SetPercentComplete() method. This method takes an integer argument as input for the percentage work completed.
The following piece of code shows how to change the progress of a task.
1System::SharedPtr<Project> project = System::MakeObject<Project>();
2System::Console::WriteLine(u"Project Calculation mode is Automatic: {0}", System::ObjectExt::Box<bool>(System::ObjectExt::Equals(project->get_CalculationMode(), Aspose::Tasks::CalculationMode::Automatic)));
3
4System::SharedPtr<Task> task = project->get_RootTask()->get_Children()->Add(u"Task");
5task->Set<Duration>(Tsk::Duration(), project->GetDuration(2));
6task->Set<int32_t>(Tsk::PercentComplete(), 50);