Support for Evaluation of Functions in Formulas
Contents
[
Hide
Show
]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
- Abs( number )
- Atn( number )
- Cos( number )
- Exp( number )
- Fix( number )
- Int( number )
- Log( number )
- Rnd( number )
- Sgn( number )
- Sin( number )
- Sqr( number )
- 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.
- Choose( index, choice-1, choice-2, … , choice-n|, choice-n)
- IIf( expr, truepart, falsepart )
- IsNumeric( expression)
- IsNull( expression )
- 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
- Asc( string )
- Chr( charcode )
- Format( expression, format, firstdayofweek, firstweekofyear)
- Instr( start,string1, string2, compare )
- LCase( string )
- Left( string, length )
- Len( string )
- LTrim( string )
- Mid( string, start, length )
- Right( string, length )
- RTrim( string )
- Space( number )
- StrComp( string1, string2, compare )
- StrConv( string, conversion, LCID )
- String( number, character )
- Trim( string )
- 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
- CDate( expression )
- Date ()
- DateAdd( interval, number, date )
- DateDiff( interval, date1, date2, firstdayofweek, firstweekofyear )
- DatePart( interval, date, firstdayofweek, firstweekofyear)
- DateSerial( year, month, day )
- DateValue( date)
- Day( date)
- Hour( time )
- IsDate( expression )
- Minute( time)
- Month( date)
- Now ()
- ProjDateAdd( date, duration, calendar )
- ProjDateConv( expression, dateformat )
- ProjDateDiff( date1, date2, calendar )
- ProjDateSub( date, duration, calendar )
- ProjDateValue( expression )
- ProjDurConv( expression, durationunits )
- ProjDurValue( duration )
- Second( time )
- Time ()
- Timer ()
- TimeSerial( hour, minute, second)
- TimeValue( time)
- Weekday( date, firstdayofweek )
- 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());