Writing and Reading Formulas
Contents
[
Hide
Show
]Aspose.Tasks for Java API supports reading/writing formulas to MPP project files. The Formula property of the ExtendedAttributeDefinition provides the interface for reading the formula value. This article describes how to read formulas from local as well as Enterprise Extended Attributes from the MPP file.
Writing Formulas in Extended Attributes to MPP File Formats
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");