Working with Expressions

Working with Expressions in Aspose.Tasks

Aspose.Tasks for .NET allows you to use Task, Resource, and Project fields in formula expressions.
You can build custom calculations with fields such as:

These formulas can be applied in Extended Attributes, enabling advanced project customization and reporting.

Using Tasks and Resource Fields in Formula Calculations

The following example shows how task and resource fields can be used in formula-based calculations:

 1static void Run()
 2{
 3    Project project = CreateTestProjectWithCustomField();
 4    Task task = project.RootTask.Children.GetById(1);
 5
 6    // Set formula for extended attribute
 7    ExtendedAttributeDefinition extendedAttributeDefinition1 = project.ExtendedAttributes[0];
 8    extendedAttributeDefinition1.Alias = "Days from finish to deadline";
 9    extendedAttributeDefinition1.Formula = "[Deadline] - [Finish]";
10
11    // Set Task Deadline and save project
12    Task task = project.RootTask.Children.GetById(1);
13    task.Set(Tsk.Deadline, new DateTime(2015, 3, 20, 17, 0, 0));
14    project.Save("UsingTasksAndResourceFields_out.mpp", SaveFileFormat.MPP);
15}
16
17// Helper method to create project
18static Project CreateTestProjectWithCustomField()
19{
20    // Create new project instance
21    Project project = new Project("New Project.mpp");
22    project.Set(Prj.StartDate, new DateTime(2015, 3, 6, 8, 0, 0));
23
24    // Add new task with extended attribute
25    Task task = project.RootTask.Children.Add("Task");
26    ExtendedAttributeDefinition extendedAttributeDefinition = ExtendedAttributeDefinition.CreateTaskDefinition(CustomFieldType.Text, ExtendedAttributeTask.Text5, "My Ext Attr");
27    project.ExtendedAttributes.Add(extendedAttributeDefinition);
28    ExtendedAttribute extendedAttribute = extendedAttributeDefinition.CreateExtendedAttribute();
29    task.ExtendedAttributes.Add(extendedAttribute);
30
31    // Add resource and resource assignment
32    Resource resource = project.Resources.Add("Rsc");
33    ResourceAssignment assignment = project.ResourceAssignments.Add(task, resource);
34    return project;
35}

Using Arithmetic Expression

You can define arithmetic expressions to perform calculations between fields:

 1Project project = CreateTestProjectWithCustomField();
 2
 3// Set arithmetic formula for extended attribute
 4ExtendedAttributeDefinition attr = project.ExtendedAttributes[0];
 5attr.Alias = "Arithmetic Expression";
 6attr.Formula = "(1+3*(2+ -5)+8/2)^3";
 7
 8// Display extended attribute value
 9Task task = project.RootTask.Children.GetById(1);
10Console.WriteLine(task.ExtendedAttributes[0].NumericValue);

Using Task Number Fields

Formulas can also reference number fields for flexible computations:

 1Project project = CreateTestProjectWithCustomField();
 2
 3// Set formula
 4ExtendedAttributeDefinition attr = project.ExtendedAttributes[0];
 5attr.Alias = "Task number fields";
 6attr.Formula = "([Outline Level] + [Priority] + [% Complete])/2";
 7
 8Task task = project.RootTask.Children.GetById(1);
 9
10// Print extended attribute value before and after updating task percent complete
11Console.WriteLine(task.ExtendedAttributes[0].NumericValue);
12task.Set(Tsk.PercentComplete, 50);
13Console.WriteLine(task.ExtendedAttributes[0].NumericValue);

Formula With Boolean Values

Boolean expressions allow conditional logic within formulas:

1Project project = CreateTestProjectWithCustomField();
2
3// Set formula for extended attribute
4project.ExtendedAttributes[0].Formula = "[Critical]-[Marked]+4+[Active]-Not [Active]";
5
6// Print value of extended attribute
7Task task = project.RootTask.Children.GetById(1);
8Console.WriteLine("Formula with boolean values: " + task.ExtendedAttributes[0].TextValue);

Formula With Project Fields

Project-level fields, such as total task count or resource count, can also be included in formulas:

 1static void Run()
 2{
 3    Project project = CreateTestProjectWithCustomFieldWithoutResource();
 4
 5    // Set formula
 6    project.ExtendedAttributes[0].Formula = "\"Total tasks: \" & [Task Count] & \" Total resources: \" & [Resource Count]";
 7    
 8    // Print if formula value is computed correctly
 9    Task task = project.RootTask.Children.GetById(1);
10    Console.WriteLine("Check Total tasks: 1 Total resources: 0 - {0}", task.ExtendedAttributes[0].TextValue.Equals("Total tasks: 1 Total resources: 0"));
11}
12
13static Project CreateTestProjectWithCustomFieldWithoutResource()
14{
15    Project project = new Project();
16    project.Set(Prj.StartDate, new DateTime(2015, 3, 6, 8, 0, 0));
17    ExtendedAttributeDefinition attr = ExtendedAttributeDefinition.CreateTaskDefinition(CustomFieldType.Text, ExtendedAttributeTask.Text1, "Custom");
18    project.ExtendedAttributes.Add(attr);
19 
20    Task task = project.RootTask.Children.Add("Task");
21    ExtendedAttribute a = attr.CreateExtendedAttribute();
22    task.ExtendedAttributes.Add(a);
23    return project;
24}
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.