Writing and Reading Formulas

Aspose.Tasks for .NET 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

 1Project project = new Project("New Project.mpp");
 2project.Set(Prj.NewTasksAreManual, false);
 3
 4// Create new custom field (Task Text1) with formula which will double task cost
 5ExtendedAttributeDefinition attr = ExtendedAttributeDefinition.CreateTaskDefinition(CustomFieldType.Text, ExtendedAttributeTask.Text1, "Custom");
 6attr.Alias = "Double Costs";
 7attr.Formula = "[Cost]*2";
 8project.ExtendedAttributes.Add(attr);
 9
10// Add a task
11Task task = project.RootTask.Children.Add("Task");
12
13// Set task cost
14task.Set(Tsk.Cost, 100);
15
16project.Save("WriteFormulasInExtendedAttributesToMPP_out.mpp", SaveFileFormat.MPP);

Reading Formulas in Local and Enterprise Extended Attributes from MPP file

1Project project = new Project("New Project.mpp"); // Attached test mpp
2// Read extended attribute formula
3ExtendedAttributeDefinition attr = project.ExtendedAttributes[0];
4Console.WriteLine("Attribute Formula: " + attr.Formula);

Read-Only Access to Custom Field Values when Using Formulas

 1// Create new project and extended attribute definition
 2Project project = new Project();
 3
 4ExtendedAttributeDefinition attribute = ExtendedAttributeDefinition.CreateTaskDefinition(CustomFieldType.Cost, ExtendedAttributeTask.Cost1, "");
 5attribute.Formula  = "[Cost]-[Actual Cost]";
 6
 7project.ExtendedAttributes.Add(attribute);
 8
 9// Add task
10Task task = project.RootTask.Children.Add("Task");
11
12// Create extended attribute
13ExtendedAttribute extendedAttribute = attribute.CreateExtendedAttribute();
14task.ExtendedAttributes.Add(extendedAttribute);
15
16// Display if extended attributes are read only or not
17Console.WriteLine(extendedAttribute.ValueReadOnly == true ? "Value is Read only" : "Value is not read only");
18extendedAttribute.NumericValue = -1000000M;
19Console.WriteLine(extendedAttribute.NumericValue == -1000000M ? "Formula values are read-only" : "Values are not read-only");
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.