JavaのAspose.Tasksを使用したフォーミュラを作成および読み取ります
Contents
[
Hide
Show
]Java APIのAspose.Tasksは、MPPプロジェクトファイルへの読み取り/書き込みをサポートしています。 extendedAttributedefinitionの式プロパティは、式値を読み取るためのインターフェイスを提供します。この記事では、MPPファイルからのローカルおよびエンタープライズ拡張属性の式を読み取る方法について説明します。
MPPファイル形式への拡張属性の書き込み式
1// The path to the documents directory.
2String dataDir = Utils.getDataDir(WriteReadFormula.class);
3
4Project project = new Project(dataDir + "New project 2010.mpp");
5project.set(Prj.NEW_TASKS_ARE_MANUAL, new NullableBool(false));
6
7// create new custom field (Task Text1) with formula which will double task cost
8ExtendedAttributeDefinition attr = ExtendedAttributeDefinition.createTaskDefinition(CustomFieldType.Text,
9 ExtendedAttributeTask.Text1, "Custom");
10attr.setAlias("Double Costs");
11attr.setFormula("[Cost]*2");
12
13project.getExtendedAttributes().add(attr);
14
15// add a task to see the result in MSP
16Task task = project.getRootTask().getChildren().add("Task");
17// set task cost
18task.set(Tsk.COST, BigDecimal.valueOf(100));
19// see the result in the attached screenshot (result.jpg)
20project.save(dataDir + "saved.mpp", SaveFileFormat.MPP);
Reading Formulas in Local and Enterprise Extended Attributes from MPP file
1// The path to the documents directory.
2String dataDir = Utils.getDataDir(WriteReadFormula.class);
3
4Project proj = new Project(dataDir + "FormulaField.mpp"); // attached test mpp
5ExtendedAttributeDefinition attr = proj.getExtendedAttributes().get(0);
6
7System.out.println("Attribute Formula: " + attr.getFormula());
Read-Only Access to Custom Field Values when Using Formulas
1// The path to the documents directory.
2String dataDir = Utils.getDataDir(WriteReadFormula.class);
3
4Project project = new Project();
5
6ExtendedAttributeDefinition attribute = ExtendedAttributeDefinition.createTaskDefinition(CustomFieldType.Cost,
7 ExtendedAttributeTask.Cost1, "");
8attribute.setFormula("[Cost]-[Actual Cost]");
9
10project.getExtendedAttributes().add(attribute);
11
12// Add task
13Task task = project.getRootTask().getChildren().add("Task");
14
15// Create extended attribute
16ExtendedAttribute extendedAttribute = attribute.createExtendedAttribute();
17task.getExtendedAttributes().add(extendedAttribute);
18
19// Display if extended attributes are read only or not
20System.out.println(
21 extendedAttribute.getValueReadOnly() == true ? "Value is Read only" : "Value is not read only");
22extendedAttribute.setTextValue("-36755");
23System.out.println(
24 extendedAttribute.getTextValue() == " " ? "Formula values are read-only" : "Values are not read-only");