Support for Evaluation of Functions in Formulas

Aspose.Tasks for Java API supports evaluating functions defined as formula expression in Extended Attribute. These include the calculation of Math, General, Text, and DateTime functions.

Calculation of Math Expressions

  1. Abs( number )
  2. Atn( number )
  3. Cos( number )
  4. Exp( number )
  5. Fix( number )
  6. Int( number )
  7. Log( number )
  8. Rnd( number )
  9. Sgn( number )
  10. Sin( number )
  11. Sqr( number )
  12. Tan( number )
1Project project = CreateTestProjectWithCustomField();
2        
3// Set formula Sin(pi/2)
4project.getExtendedAttributes().get(0).setFormula("Sin(3.1415926/2)");
5
6// Print Calculated value
7Task task = project.getRootTask().getChildren().getById(1);
8System.out.println("Sin(pi/2): {0}" + task.getExtendedAttributes().get(0).getNumericValue());

Calculation of General Functions

The following General functions can be calculated by the API.

  1. Choose( index, choice-1, choice-2, … , choice-n|, choice-n)
  2. IIf( expr, truepart, falsepart )
  3. IsNumeric( expression)
  4. IsNull( expression )
  5. Switch( expr-1, value-1, expr-2, value-2, … , expr-n,value-n|, expr-n,value-n )
 1public static void EvaluateChoose()
 2{    
 3    Project project = CreateTestProjectWithCustomField();
 4    
 5    // Set Formula
 6    project.getExtendedAttributes().get(0).setFormula("Choose(3, \"This is a\", \"right\", \"choice\")");
 7    
 8    // Print extended attribute value
 9    Task task = project.getRootTask().getChildren().getById(1);
10    System.out.println(task.getExtendedAttributes().get(0).getTextValue());
11}
12
13public static void EvaluateIsNumeric()
14{
15    String[] numericFormulas = { "IsNumeric('AAA')", "IsNUmeric(1)", "IsNumeric(1<0)", "IsNumeric(\"1.1\")", "IsNumeric(Choose((2 + Sgn(2^-3)), 123, \"one two three\"))" };
16    
17    Project project = CreateTestProjectWithCustomField();
18    
19    for (String numericFormula : numericFormulas)    
20    {
21        // Set Formula
22        project.getExtendedAttributes().get(0).setFormula(numericFormula);
23
24        // Print extended attribute value
25        Task task = project.getRootTask().getChildren().getById(1);
26        System.out.println(task.getExtendedAttributes().get(0).getTextValue());
27    }           
28}
29
30public static void EvaluateSwitch()
31{    
32    Project project = CreateTestProjectWithCustomField();
33
34    // Set Formula
35    project.getExtendedAttributes().get(0).setFormula("Switch( 0 < 1, \"0 is lesser than 1\", 0 > 1, \"0 is greater than 1\")");
36
37    // Print extended attribute value
38    Task task = project.getRootTask().getChildren().getById(1);
39    System.out.println(task.getExtendedAttributes().get(0).getTextValue());
40}    

Calculation of Text Functions

  1. Asc( string )
  2. Chr( charcode )
  3. Format( expression, format, firstdayofweek, firstweekofyear)
  4. Instr( start,string1, string2, compare )
  5. LCase( string )
  6. Left( string, length )
  7. Len( string )
  8. LTrim( string )
  9. Mid( string, start, length )
  10. Right( string, length )
  11. RTrim( string )
  12. Space( number )
  13. StrComp( string1, string2, compare )
  14. StrConv( string, conversion, LCID )
  15. String( number, character )
  16. Trim( string )
  17. UCase( string )
 1Project project = CreateTestProjectWithCustomField();
 2Task task = project.getRootTask().getChildren().getById(1);
 3
 4// EvaluateStrConv
 5// Set formulas and print extended attribute value
 6project.getExtendedAttributes().get(0).setFormula("StrConv(\"sTring and sTRINg\",3)");
 7System.out.println(task.getExtendedAttributes().get(0).getTextValue());
 8        
 9project.getExtendedAttributes().get(0).setFormula("StrConv(\"sTring and sTRINg\",1)");
10System.out.println(task.getExtendedAttributes().get(0).getTextValue());
11        
12project.getExtendedAttributes().get(0).setFormula("StrConv(\"sTring and sTRINg\",2)");
13System.out.println(task.getExtendedAttributes().get(0).getTextValue());
14   
15// EvaluateStringFunction
16// Set formulas and print extended attribute value
17project.getExtendedAttributes().get(0).setFormula("String(5, 40)");
18System.out.println(task.getExtendedAttributes().get(0).getTextValue());
19        
20project.getExtendedAttributes().get(0).setFormula("String(5, \"A\")");
21System.out.println(task.getExtendedAttributes().get(0).getTextValue());
22        
23project.getExtendedAttributes().get(0).setFormula("String(-5, \"A\")");
24// #Error
25System.out.println(task.getExtendedAttributes().get(0).getTextValue());

Calculation of Date/Time Functions

  1. CDate( expression )
  2. Date ()
  3. DateAdd( interval, number, date )
  4. DateDiff( interval, date1, date2, firstdayofweek, firstweekofyear )
  5. DatePart( interval, date, firstdayofweek, firstweekofyear)
  6. DateSerial( year, month, day )
  7. DateValue( date)
  8. Day( date)
  9. Hour( time )
  10. IsDate( expression )
  11. Minute( time)
  12. Month( date)
  13. Now ()
  14. ProjDateAdd( date, duration, calendar )
  15. ProjDateConv( expression, dateformat )
  16. ProjDateDiff( date1, date2, calendar )
  17. ProjDateSub( date, duration, calendar )
  18. ProjDateValue( expression )
  19. ProjDurConv( expression, durationunits )
  20. ProjDurValue( duration )
  21. Second( time )
  22. Time ()
  23. Timer ()
  24. TimeSerial( hour, minute, second)
  25. TimeValue( time)
  26. Weekday( date, firstdayofweek )
  27. Year( date)
 1Project project = CreateTestProjectWithCustomField();
 2Task task = project.getRootTask().getChildren().getById(1);
 3
 4ExtendedAttributeDefinition numberDefinition = ExtendedAttributeDefinition.createTaskDefinition(ExtendedAttributeTask.Number1, null);
 5project.getExtendedAttributes().add(numberDefinition);
 6
 7ExtendedAttribute numberAttribute = numberDefinition.createExtendedAttribute();
 8task.getExtendedAttributes().add(numberAttribute);
 9
10// Set ProjDateDiff formula and print extended attribute value
11numberDefinition.setFormula("ProjDateDiff(\"03/23/2015\",\"03/18/2015\")");
12System.out.println(task.getExtendedAttributes().get(0).getNumericValue());
13        
14numberDefinition.setFormula("ProjDateDiff(\"03/23/2015\",\"03/25/2015\")");
15System.out.println(task.getExtendedAttributes().get(0).getNumericValue());
16
17ExtendedAttributeDefinition dateDefinition = ExtendedAttributeDefinition.createTaskDefinition(ExtendedAttributeTask.Date1, null);
18project.getExtendedAttributes().add(dateDefinition);
19ExtendedAttribute dateAttribute = dateDefinition.createExtendedAttribute();
20task.getExtendedAttributes().add(dateAttribute);
21
22ExtendedAttributeDefinition durationDefinition = ExtendedAttributeDefinition.createTaskDefinition(ExtendedAttributeTask.Duration4, "Custom duration field");
23project.getExtendedAttributes().add(durationDefinition);
24ExtendedAttribute durationAttribute = durationDefinition.createExtendedAttribute();
25task.getExtendedAttributes().add(durationAttribute);
26
27ExtendedAttributeDefinition textDefinition = ExtendedAttributeDefinition.createTaskDefinition(ExtendedAttributeTask.Text5, "Custom text field");
28project.getExtendedAttributes().add(textDefinition);
29ExtendedAttribute textAttribute = textDefinition.createExtendedAttribute();
30task.getExtendedAttributes().add(textAttribute);
31
32// Set ProjDateSub formula and print extended attribute value
33dateDefinition.setFormula("ProjDateSub(\"3/19/2015\", \"1d\")");
34System.out.println(dateAttribute.getDateValue());
35
36// We can set ProjDurConv formula to duration-valued attribute as well as to text-valued attribute.
37
38// Set ProjDurConv formula to duration-valued extended attribute and print its value.
39durationDefinition.setFormula("ProjDurConv([Duration], pjHours)");
40System.out.println(durationAttribute.getDurationValue());
41
42// Set ProjDurConv formula to text-valued extended attribute and print its value.
43textDefinition.setFormula("ProjDurConv([Duration], pjHours)");
44System.out.println(task.getExtendedAttributes().get(0).getTextValue());
45
46textDefinition.setFormula("ProjDurConv([Duration], pjWeeks)");
47System.out.println(task.getExtendedAttributes().get(0).getTextValue());
48
49// Set Second formula and print entended attribute value
50numberDefinition.setFormula("Second(\"4/21/2015 2:53:41 AM\")");
51System.out.println(task.getExtendedAttributes().get(0).getNumericValue());
52
53// Set Weekday formula and print entended attribute value
54numberDefinition.setFormula("Weekday(\"24/3/2015\", 1)");
55System.out.println(task.getExtendedAttributes().get(0).getNumericValue());
56        
57numberDefinition.setFormula("Weekday(\"24/3/2015\", 2)");
58System.out.println(task.getExtendedAttributes().get(0).getNumericValue());
59        
60numberDefinition.setFormula("Weekday(\"24/3/2015\", 3)");
61System.out.println(task.getExtendedAttributes().get(0).getNumericValue());
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.