Writing and Reading Formulas

Aspose.Tasks for C++ API supports reading/writing formulas to MPP project files. The Formula property of the ExtendedAttributeDefinition provides the interface for reading the formula value. This article describes how to read formulas from local as well as Enterprise Extended Attributes from the MPP file. It also shows how to write formulas to MPP file.

Writing Formulas in Extended Attributes to MPP File Formats

 1// Create project instance
 2System::String dataDir = RunExamples::GetDataDir(System::Reflection::MethodBase::GetCurrentMethod(ASPOSE_CURRENT_FUNCTION)->get_DeclaringType().get_FullName());
 3System::SharedPtr<Project> project = System::MakeObject<Project>(dataDir + u"Project1.mpp");
 4project->Set<NullableBool>(Prj::NewTasksAreManual(), NullableBool::to_NullableBool(false));
 5        
 6// Create new custom field (Task Text1) with formula which will double task cost
 7System::SharedPtr<ExtendedAttributeDefinition> attr = ExtendedAttributeDefinition::CreateTaskDefinition(Aspose::Tasks::CustomFieldType::Text, Aspose::Tasks::ExtendedAttributeTask::Text1, u"Custom");
 8attr->set_Alias(u"Double Costs");
 9attr->set_Formula(u"[Cost]*2");
10project->get_ExtendedAttributes()->Add(attr);
11        
12// Add a task
13System::SharedPtr<Task> task = project->get_RootTask()->get_Children()->Add(u"Task");
14        
15// Set task cost            
16task->Set<System::Decimal>(Tsk::Cost(), static_cast<System::Decimal>(100));
17        
18// Save project
19project->Save(dataDir + u"WriteFormulasInExtendedAttributesToMPP_out.mpp", Aspose::Tasks::Saving::SaveFileFormat::MPP);

Reading Formulas in Local and Enterprise Extended Attributes from MPP file

1// The path to the documents directory.
2// Create project instance
3System::String dataDir = RunExamples::GetDataDir(System::Reflection::MethodBase::GetCurrentMethod(ASPOSE_CURRENT_FUNCTION)->get_DeclaringType().get_FullName());
4System::SharedPtr<Project> project1 = System::MakeObject<Project>(dataDir + u"ReadFormulas.mpp");
5// Attached test mpp
6    
7// Read extended attribute formula
8System::SharedPtr<ExtendedAttributeDefinition> attr = project1->get_ExtendedAttributes()->idx_get(0);
9System::Console::WriteLine(System::String(u"Attribute Formula: ") + attr->get_Formula());

Read-Only Access to Custom Field Values when Using Formulas

 1// Create new project and extended attribute definition
 2System::SharedPtr<Project> project = System::MakeObject<Project>();
 3    
 4System::SharedPtr<ExtendedAttributeDefinition> attribute = ExtendedAttributeDefinition::CreateTaskDefinition(Aspose::Tasks::CustomFieldType::Cost, Aspose::Tasks::ExtendedAttributeTask::Cost1, u"");
 5attribute->set_Formula(u"[Cost]-[Actual Cost]");
 6    
 7project->get_ExtendedAttributes()->Add(attribute);
 8    
 9// Add task
10System::SharedPtr<Task> task = project->get_RootTask()->get_Children()->Add(u"Task");
11    
12// Create extended attribute
13System::SharedPtr<ExtendedAttribute> extendedAttribute = attribute->CreateExtendedAttribute();
14task->get_ExtendedAttributes()->Add(extendedAttribute);
15    
16// Display if extended attributes are read only or not
17System::Console::WriteLine(extendedAttribute->get_ValueReadOnly() == true ? System::String(u"Value is Read only") : System::String(u"Value is not read only"));
18extendedAttribute->set_NumericValue(-System::Decimal(1000000));
19System::Console::WriteLine(extendedAttribute->get_NumericValue() == -System::Decimal(1000000) ? System::String(u"Formula values are read-only") : System::String(u"Values are not read-only"));
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.