数式の操作

Aspose.Tasks for .NET API は MPP プロジェクト ファイルへの数式の読み取りと書き込みをサポートします。Formula プロパティは ExtendedAttributeDefinition は数式値を読み取るためのインターフェースを提供します。本記事では、MPP ファイルからローカルおよび Enterprise Extended Attributes の数式を読み取る方法について説明します。また、MPP ファイルに数式を書き込む方法も示します。

Extended Attributes の数式を MPP ファイル形式に書き込む

プロジェクト内でカスタム数式を定義し、それを Extended Attributes に割り当てることができます。Aspose.Tasks はこれらの数式を Microsoft Project との互換性を保ったまま MPP ファイルに書き込みます。

 1Project project = new Project("New Project.mpp");
 2project.Set(Prj.NewTasksAreManual, false);
 3
 4// Create new custom field (Task Text1) with formula which will double task cost
 5ExtendedAttributeDefinition attr = ExtendedAttributeDefinition.CreateTaskDefinition(CustomFieldType.Text, ExtendedAttributeTask.Text1, "Custom");
 6attr.Alias = "Double Costs";
 7attr.Formula = "[Cost]*2";
 8project.ExtendedAttributes.Add(attr);
 9
10// Add a task
11Task task = project.RootTask.Children.Add("Task");
12
13// Set task cost
14task.Set(Tsk.Cost, 100);
15
16project.Save("WriteFormulasInExtendedAttributesToMPP_out.mpp", SaveFileFormat.MPP);

MPP ファイルからのローカルおよび Enterprise Extended Attributes の数式の読み取り

Aspose.Tasks は、ローカルおよび Enterprise Extended Attributes に定義された数式へアクセスできます。これにより、MPP ファイルに埋め込まれたカスタムの業務ロジックをプログラムで抽出および解析することが可能になります。

1Project project = new Project("New Project.mpp"); // Attached test mpp
2// Read extended attribute formula
3ExtendedAttributeDefinition attr = project.ExtendedAttributes[0];
4Console.WriteLine("Attribute Formula: " + attr.Formula);

数式使用時のカスタムフィールド値の読み取り専用アクセス

数式が Extended Attributes に適用されると、結果として得られるカスタムフィールド値は 読み取り専用。Aspose.Tasks は、計算された値が MPP ファイルに定義された数式と一致することを保証します。

 1// Create new project and extended attribute definition
 2Project project = new Project();
 3
 4ExtendedAttributeDefinition attribute = ExtendedAttributeDefinition.CreateTaskDefinition(CustomFieldType.Cost, ExtendedAttributeTask.Cost1, "");
 5attribute.Formula  = "[Cost]-[Actual Cost]";
 6
 7project.ExtendedAttributes.Add(attribute);
 8
 9// Add task
10Task task = project.RootTask.Children.Add("Task");
11
12// Create extended attribute
13ExtendedAttribute extendedAttribute = attribute.CreateExtendedAttribute();
14task.ExtendedAttributes.Add(extendedAttribute);
15
16// Display if extended attributes are read only or not
17Console.WriteLine(extendedAttribute.ValueReadOnly == true ? "Value is Read only" : "Value is not read only");
18extendedAttribute.NumericValue = -1000000M;
19Console.WriteLine(extendedAttribute.NumericValue == -1000000M ? "Formula values are read-only" : "Values are not read-only");
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.