Working with Expressions

Aspose.Tasks for Java 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 Tasks and Resource Fields in Formula Calculations

 1// Create new project instance
 2   Project project = new Project();
 3        
 4java.util.Calendar cal = java.util.Calendar.getInstance();
 5cal.set(2015, 26, 3, 8,0,0);
 6        
 7project.set(Prj.START_DATE, cal.getTime());
 8        
 9   // Add new task with extended attribute
10   Task task = project.getRootTask().getChildren().add("Task");
11   ExtendedAttributeDefinition extendedAttributeDefinition = ExtendedAttributeDefinition.createTaskDefinition(CustomFieldType.Text, ExtendedAttributeTask.Text5, "My Ext Attr");
12   project.getExtendedAttributes().add(extendedAttributeDefinition);
13   ExtendedAttribute extendedAttribute = extendedAttributeDefinition.createExtendedAttribute();
14   task.getExtendedAttributes().add(extendedAttribute);
15
16   // Add resource and resource assignment
17   Resource rsc = project.getResources().add("Rsc");
18   ResourceAssignment assn = project.getResourceAssignments().add(task, rsc);
19   return project;
 1// see this helper method below
 2Project project = CreateTestProjectWithCustomField();
 3        
 4ExtendedAttributeDefinition attr = project.getExtendedAttributes().get(0);
 5attr.setAlias("Days from finish to deadline");
 6attr.setFormula("[Deadline] - [Finish]");
 7        
 8java.util.Calendar cal = java.util.Calendar.getInstance();
 9cal.set(2015, 26, 3, 8,0,0);
10
11Task task = project.getRootTask().getChildren().getById(1);
12task.set(Tsk.DEADLINE, cal.getTime());
13
14project.save("SaveFile.mpp", SaveFileFormat.MPP);

Using Arithmetic Expression

 1Project project = CreateTestProjectWithCustomField();
 2      
 3// Set arithmetic formula for extended attribute
 4ExtendedAttributeDefinition attr = project.getExtendedAttributes().get(0);
 5attr.setAlias("Arithmetic Expression");
 6attr.setFormula("(1+3*(2+ -5)+8/2)^3");
 7
 8// Display extended attribute value
 9Task task = project.getRootTask().getChildren().getById(1);
10System.out.println(task.getExtendedAttributes().get(0).getNumericValue());

Using Task Number Fields

 1Project project = CreateTestProjectWithCustomField();
 2
 3// Set Formula
 4ExtendedAttributeDefinition attr = project.getExtendedAttributes().get(0);
 5attr.setAlias("Task number fields");
 6attr.setFormula("(([Outline Level] + [Priority] + [% Complete])/2");
 7
 8Task task = project.getRootTask().getChildren().getById(1);
 9
10// Print extended attribute value before and after updating task percent complete
11System.out.println(task.getExtendedAttributes().get(0).getNumericValue());
12
13task.set(Tsk.PERCENT_COMPLETE, 50);
14System.out.println(task.getExtendedAttributes().get(0).getNumericValue());

Formula With Boolean Values

1Project project = CreateTestProjectWithCustomField();
2
3// Set formula for extended attribute
4project.getExtendedAttributes().get(0).setFormula("[Critical]-[Marked]+4+[Active]-Not [Active]");
5        
6// Print value of extened attribute
7Task task = project.getRootTask().getChildren().getById(1);
8System.out.println("Formula with boolean values" + task.getExtendedAttributes().get(0).getTextValue());

Formula With Project Fields

1Project project = CreateTestProjectWithCustomField();
2
3// Set formula for extended attribute
4project.getExtendedAttributes().get(0).setFormula("\"Total tasks: \" & [Task Count] & \" Total resources: \" & [Resource Count]");
5        
6// Print value of extened attribute
7Task task = project.getRootTask().getChildren().getById(1);
8System.out.println("Formula with boolean values" + task.getExtendedAttributes().get(0).getTextValue());
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.