Formula Expressions

Aspose.Tasks for C++ API can use Task, Resource and Project fields as a formula in expressions. Fields such as Work, Cost, Number fields, DateTime, Boolean and Project Fields such as task count and resource count can be used in formula expressions in an extended attribute.

Using Arithmetic Expression

 1System::SharedPtr<Project> project = CreateTestProjectWithCustomField();
 2    
 3// Set arithmetic formula for extended attribute
 4System::SharedPtr<ExtendedAttributeDefinition> attr = project->get_ExtendedAttributes()->idx_get(0);
 5attr->set_Alias(u"Arithmetic Expression");
 6attr->set_Formula(u"(1+3*(2+ -5)+8/2)^3");
 7    
 8// Display extended attribute value
 9System::SharedPtr<Task> task = project->get_RootTask()->get_Children()->GetById(1);
10System::Console::WriteLine(task->get_ExtendedAttributes()->idx_get(0)->get_NumericValue());

Using Task Number Fields

 1System::SharedPtr<Project> project = CreateTestProjectWithCustomField();
 2    
 3// Set formula
 4System::SharedPtr<ExtendedAttributeDefinition> attr = project->get_ExtendedAttributes()->idx_get(0);
 5attr->set_Alias(u"Task number fields");
 6attr->set_Formula(u"([Outline Level] + [Priority] + [% Complete])/2");
 7    
 8System::SharedPtr<Task> task = project->get_RootTask()->get_Children()->GetById(1);
 9    
10// Print extended attribute value before and after updating task percent complete
11System::Console::WriteLine(task->get_ExtendedAttributes()->idx_get(0)->get_NumericValue());
12task->Set<int32_t>(Tsk::PercentComplete(), 50);
13System::Console::WriteLine(task->get_ExtendedAttributes()->idx_get(0)->get_NumericValue());

Formula With Boolean Values

1System::SharedPtr<Project> project = CreateTestProjectWithCustomField();
2    
3// Set formula for extended attribute
4project->get_ExtendedAttributes()->idx_get(0)->set_Formula(u"[Critical]-[Marked]+4+[Active]-Not [Active]");
5    
6// Print value of extened attribute
7System::SharedPtr<Task> task = project->get_RootTask()->get_Children()->GetById(1);
8System::Console::WriteLine(System::String(u"Formula with boolean values: ") + task->get_ExtendedAttributes()->idx_get(0)->get_TextValue());

Formula With Project Fields

1System::SharedPtr<Project> project = CreateTestProjectWithCustomFieldWithoutResource();
2    
3// Set formula
4project->get_ExtendedAttributes()->idx_get(0)->set_Formula(u"\"Total tasks: \" & [Task Count] & \" Total resources: \" & [Resource Count]");
5    
6// Print if formula value is computed correctly
7System::SharedPtr<Task> task = project->get_RootTask()->get_Children()->GetById(1);
8System::Console::WriteLine(u"Check Total tasks: 1 Total resources: 0 - {0}", System::ObjectExt::Box<bool>(System::ObjectExt::Equals(task->get_ExtendedAttributes()->idx_get(0)->get_TextValue(), u"Total tasks: 1 Total resources: 0")));
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.