数式内の関数評価のサポート
Contents
[
Hide
Show
]Aspose.Tasks for .NET API はサポートしています 数式として定義された関数の評価 プロジェクトのExtended Attributes内で。
これにより開発者はMicrosoft Projectで利用できるものと同様の計算を実行でき、カスタムロジックに依存する動的な項目を実装できるようになります。
このAPIは以下のカテゴリの関数の評価をサポートします:
- 数学関数
- 一般関数
- テキスト関数
- 日付/時刻関数
この機能は特に次のような場合に便利です:
- プロジェクトの計算を自動化する、
- カスタムの業務ルールを実装する、
- MS ProjectとAspose.Tasks間での数式の互換性を確保する。
数学式の計算
以下の数学関数がサポートされています:
- Abs( number )
- Atn( number )
- Cos( number )
- Exp( number )
- Fix( number )
- Int( number )
- Log( number )
- Rnd( number )
- Sgn( number )
- Sin( number )
- Sqr( number )
- Tan( number )
1static void EvaluateSine()
2{
3 Project project = CreateTestProjectWithCustomField();
4
5 // Set formula Sin(pi/2)
6 project.ExtendedAttributes[0].Formula = "Sin(3.1415926/2)";
7
8 // Print Calculated value
9 Task task = project.RootTask.Children.GetById(1);
10 Console.WriteLine("Sin(pi/2): {0}", task.ExtendedAttributes[0].NumericValue);
11}
12
13static Project CreateTestProjectWithCustomField()
14{
15 Project project = new Project();
16 ExtendedAttributeDefinition attr = ExtendedAttributeDefinition.CreateTaskDefinition(CustomFieldType.Number, ExtendedAttributeTask.Number1, "Sine");
17 project.ExtendedAttributes.Add(attr);
18
19 Task task = project.RootTask.Children.Add("Task");
20
21 ExtendedAttribute a = attr.CreateExtendedAttribute();
22 task.ExtendedAttributes.Add(a);
23 return project;
24}一般関数の計算
以下の関数は論理および条件評価を提供します:
Choose( index, choice-1, choice-2, ... , choice-n )– リストから値を選択します。IIf( expr, truepart, falsepart )– 条件に応じて2つの値のうちいずれかを返します。IsNumeric( expression )– 式が数値かどうかを判定します。IsNull( expression )– 式がnullかどうかを判定します。Switch( expr-1, value-1, expr-2, value-2, ... )– 順に式を評価し、最初に一致した値を返します。
1static void EvaluateChoose()
2{
3 Project project = CreateTestProjectWithCustomField();
4
5 // Set Formula
6 project.ExtendedAttributes[0].Formula = "Choose(3, \"This is a\", \"right\", \"choice\")";
7
8 // Print extended attribute value
9 Task task = project.RootTask.Children.GetById(1);
10 Console.WriteLine(task.ExtendedAttributes[0].TextValue);
11}
12
13static 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 foreach (string numericFormula in numericFormulas)
20 {
21 // Set Formula
22 project.ExtendedAttributes[0].Formula = numericFormula;
23
24 // Print extended attribute value
25 Task task = project.RootTask.Children.GetById(1);
26 Console.WriteLine(task.ExtendedAttributes[0].TextValue);
27 }
28}
29
30static void EvaluateSwitch()
31{
32 Project project = CreateTestProjectWithCustomField();
33
34 // Set Formula
35 project.ExtendedAttributes[0].Formula = "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.RootTask.Children.GetById(1);
39 Console.WriteLine(task.ExtendedAttributes[0].TextValue);
40}テキスト関数の計算
文字列操作関数は書式設定や比較を可能にします:
Asc( string )– 先頭文字のASCIIコードを返します。Chr( charcode )– ASCIIコードに対応する文字を返します。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 )– 2つの文字列を比較します。StrConv( string, conversion, LCID )– ロケールに応じて文字列を変換します。String( number, character )– 文字列をn回繰り返して返します。Trim( string )– 先頭と末尾の空白を削除します。UCase( string )– 大文字に変換します。
1static void EvaluateStrConv()
2{
3 Project project = CreateTestProjectWithCustomField();
4 Task task = project.RootTask.Children.GetById(1);
5
6 // Set formulas and print extended attribute value
7 project.ExtendedAttributes[0].Formula = "StrConv(\"sTring and sTRINg\",3)";
8 Console.WriteLine(task.ExtendedAttributes[0].TextValue);
9 project.ExtendedAttributes[0].Formula = "StrConv(\"sTring and sTRINg\",1)";
10 Console.WriteLine(task.ExtendedAttributes[0].TextValue);
11 project.ExtendedAttributes[0].Formula = "StrConv(\"sTring and sTRINg\",2)";
12 Console.WriteLine(task.ExtendedAttributes[0].TextValue);
13}
14
15static void EvaluateStringFunction()
16{
17 Project project = CreateTestProjectWithCustomField();
18 Task task = project.RootTask.Children.GetById(1);
19
20 // Set formulas and print extended attribute value
21 project.ExtendedAttributes[0].Formula = "String(5, 40)";
22 Console.WriteLine(task.ExtendedAttributes[0].TextValue);
23 project.ExtendedAttributes[0].Formula = "String(5, \"A\")";
24 Console.WriteLine(task.ExtendedAttributes[0].TextValue);
25 project.ExtendedAttributes[0].Formula = "String(-5, \"A\")";
26 // #Error
27 Console.WriteLine(task.ExtendedAttributes[0].TextValue);
28}日付/時刻関数の計算
Aspose.Tasksは標準的なVBスタイルの日付関数とMS Project特有のスケジューリング関数の両方をサポートします:
CDate( expression )– 式を日付に変換します。Date()– 現在のシステム日付を返します。DateAdd( interval, number, date )– 日付に間隔を加算します。DateDiff( interval, date1, date2 )– 2つの日付の差を計算します。DatePart( interval, date )– 日付の一部(年、月、日など)を抽出します。DateSerial( year, month, day )– 構成要素から日付を作成します。DateValue( date )– 文字列を日付に変換します。Day( date )– 月の日を返します。Hour( time )– 時を抽出します。IsDate( expression )– 式が有効な日付かどうかを判定します。Minute( time )– 分を抽出します。Month( date )– 月を抽出します。Now()– 現在の日付と時刻を返します。ProjDateAdd( date, duration, calendar )– project calendarを考慮して期間を加算します。ProjDateConv( expression, dateformat )– 日付をプロジェクト形式に変換します。ProjDateDiff( date1, date2, calendar )– project 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 = CreateTestProject();
2Task task = project.RootTask.Children.GetById(1);
3
4ExtendedAttributeDefinition numberDefinition = ExtendedAttributeDefinition.CreateTaskDefinition(ExtendedAttributeTask.Number1, null);
5project.ExtendedAttributes.Add(numberDefinition);
6
7ExtendedAttribute numberAttribute = numberDefinition.CreateExtendedAttribute();
8task.ExtendedAttributes.Add(numberAttribute);
9
10// Set ProjDateDiff formula and print extended attribute value
11numberDefinition.Formula = "ProjDateDiff(\"03/23/2015\",\"03/18/2015\")";
12Console.WriteLine(numberAttribute.NumericValue);
13numberDefinition.Formula = "ProjDateDiff(\"03/23/2015\",\"03/25/2015\")";
14Console.WriteLine(numberAttribute.NumericValue);
15
16ExtendedAttributeDefinition dateDefinition = ExtendedAttributeDefinition.CreateTaskDefinition(ExtendedAttributeTask.Date1, null);
17project.ExtendedAttributes.Add(dateDefinition);
18ExtendedAttribute dateAttribute = dateDefinition.CreateExtendedAttribute();
19task.ExtendedAttributes.Add(dateAttribute);
20
21ExtendedAttributeDefinition durationDefinition = ExtendedAttributeDefinition.CreateTaskDefinition(ExtendedAttributeTask.Duration4, "Custom duration field");
22project.ExtendedAttributes.Add(durationDefinition);
23ExtendedAttribute durationAttribute = durationDefinition.CreateExtendedAttribute();
24task.ExtendedAttributes.Add(durationAttribute);
25
26ExtendedAttributeDefinition textDefinition = ExtendedAttributeDefinition.CreateTaskDefinition(ExtendedAttributeTask.Text5, "Custom text field");
27project.ExtendedAttributes.Add(textDefinition);
28ExtendedAttribute textAttribute = textDefinition.CreateExtendedAttribute();
29task.ExtendedAttributes.Add(textAttribute);
30
31// Set ProjDateSub formula and print extended attribute value
32dateDefinition.Formula = "ProjDateSub(\"3/19/2015\", \"1d\")";
33Console.WriteLine(dateAttribute.DateValue);
34
35// We can set ProjDurConv formula to duration-valued attribute as well as to text-valued attribute.
36
37// Set ProjDurConv formula to duration-valued extended attribute and print its value.
38durationDefinition.Formula = "ProjDurConv([Duration], pjHours)";
39Console.WriteLine(durationAttribute.DurationValue);
40
41// Set ProjDurConv formula to text-valued extended attribute and print its value.
42textDefinition.Formula = "ProjDurConv([Duration], pjHours)";
43Console.WriteLine(textAttribute.TextValue);
44
45textDefinition.Formula = "ProjDurConv([Duration], pjWeeks)";
46Console.WriteLine(textAttribute.TextValue);
47
48// Set Second formula and print entended attribute value
49numberDefinition.Formula = "Second(\"4/21/2015 2:53:41 AM\")";
50Console.WriteLine(numberAttribute.NumericValue);
51
52// Set Weekday formula and print entended attribute value
53numberDefinition.Formula = "Weekday(\"24/3/2015\", 1)";
54Console.WriteLine(numberAttribute.NumericValue);
55numberDefinition.Formula = "Weekday(\"24/3/2015\", 2)";
56Console.WriteLine(numberAttribute.NumericValue);
57numberDefinition.Formula = "Weekday(\"24/3/2015\", 3)";
58Console.WriteLine(numberAttribute.NumericValue);実用例
数式の実際の利用シナリオは次の通りです:
1 var project = new Project();
2 var task = project.RootTask.Children.Add("Task 1");
3
4 // Create an extended attribute with a custom formula
5 ExtendedAttributeDefinition attrDef = ExtendedAttributeDefinition.CreateTaskDefinition(
6 ExtendedAttributeTask.Text1, "Custom Formula");
7 attrDef.Formula = "IIf([Duration] > 480, \"Long Task\", \"Short Task\")";
8 project.ExtendedAttributes.Add(attrDef);
9
10 var attr = attrDef.CreateExtendedAttribute();
11 task.ExtendedAttributes.Add(attr);
12
13 // The formula is automatically evaluated by Aspose.Tasks
14 Console.WriteLine(task.Get(Tsk.Name) + " -> " + task.ExtendedAttributes[0].TextValue);数式サポートの主な利点
- MS Projectの数式との完全な互換性。
- 自動化 動的な項目 と 条件付きの論理。
- 組み込みのサポート: プロジェクトカレンダー対応の関数 (ProjDateAdd, ProjDateDiff, etc.).
- カスタムのC#コードを書かずに複雑な計算を簡素化します。