ExtendedAttributeDefinition.Formula
Contents
[
Hide
]ExtendedAttributeDefinition.Formula property
Gets or sets the formula that Microsoft Project uses to populate a custom task field.
public string Formula { get; set; }
Examples
Shows how to use boolean functions with extended attributes.
var project = CreateTestProjectWithCustomField4();
// Set formula for extended attribute
project.ExtendedAttributes[0].Formula = "[Critical]-[Marked]+4+[Active]-Not [Active]";
// Print value of extended attribute
var task = project.RootTask.Children.GetById(1);
Console.WriteLine("Formula with boolean values: " + task.ExtendedAttributes[0].TextValue);
Shows how to use extended attribute functions by using task number fields.
var project = CreateTestProjectWithCustomField6();
// Set formula
var attr = project.ExtendedAttributes[0];
attr.Alias = "Task number fields";
attr.Formula = "([Outline Level] + [Priority] + [% Complete])/2";
var task = project.RootTask.Children.GetById(1);
// Print extended attribute value before and after updating task percent complete
Console.WriteLine(task.ExtendedAttributes[0].NumericValue);
task.Set(Tsk.PercentComplete, 50);
Console.WriteLine(task.ExtendedAttributes[0].NumericValue);
Shows how to use extended attribute formulas with aliases.
var project = new Project(DataDir + "Project1.mpp");
project.Set(Prj.NewTasksAreManual, false);
// Create new custom field (Task Text1) with formula which will double task cost
var attr = ExtendedAttributeDefinition.CreateTaskDefinition(CustomFieldType.Text, ExtendedAttributeTask.Text1, "Custom");
attr.Alias = "Double Costs";
attr.Formula = "[Cost]*2";
project.ExtendedAttributes.Add(attr);
// Add a task
var task = project.RootTask.Children.Add("Task");
// Set task cost
task.Set(Tsk.Cost, 100);
project.Save(OutDir + "WriteFormulasInExtendedAttributesToMPP_out.mpp", SaveFileFormat.Mpp);
Shows how to use transcendent math functions with extended attributes.
public void CalculateMathExpressions()
{
var project = CreateTestProjectWithCustomField2();
// Set formula Sin(pi/2)
project.ExtendedAttributes[0].Formula = "Sin(3.1415926/2)";
// Print Calculated value
var task = project.RootTask.Children.GetById(1);
Console.WriteLine("Sin(pi/2): {0}", task.ExtendedAttributes[0].NumericValue);
}
public static Project CreateTestProjectWithCustomField2()
{
var project = new Project();
var attr = ExtendedAttributeDefinition.CreateTaskDefinition(CustomFieldType.Number, ExtendedAttributeTask.Number1, "Sine");
project.ExtendedAttributes.Add(attr);
var task = project.RootTask.Children.Add("Task");
var attribute = attr.CreateExtendedAttribute();
task.ExtendedAttributes.Add(attribute);
return project;
}
Shows how to use arithmetic functions with extended attributes.
var project = CreateTestProjectWithCustomField5();
// Set arithmetic formula for extended attribute
var attr = project.ExtendedAttributes[0];
attr.Alias = "Arithmetic Expression";
attr.Formula = "(1+3*(2+ -5)+8/2)^3";
// Display extended attribute value
var task = project.RootTask.Children.GetById(1);
Console.WriteLine(task.ExtendedAttributes[0].NumericValue);
}
public static Project CreateTestProjectWithCustomField5()
{
var project = new Project();
project.Set(Prj.StartDate, new DateTime(2015, 3, 6, 8, 0, 0));
var attr = ExtendedAttributeDefinition.CreateTaskDefinition(CustomFieldType.Number, ExtendedAttributeTask.Number1, "Custom");
project.ExtendedAttributes.Add(attr);
var task = project.RootTask.Children.Add("Task");
var extendedAttribute = attr.CreateExtendedAttribute();
task.ExtendedAttributes.Add(extendedAttribute);
return project;
}
Shows how to use extended attribute functions by using project fields.
public void FormulaWithProjectFields()
{
var project = CreateTestProjectWithCustomFieldWithoutResource();
// Set formula
project.ExtendedAttributes[0].Formula = "\"Total tasks: \" & [Task Count] & \" Total resources: \" & [Resource Count]";
// Print if formula value is computed correctly
var task = project.RootTask.Children.GetById(1);
Console.WriteLine("Check Total tasks: 1 Total resources: 0 - {0}", task.ExtendedAttributes[0].TextValue.Equals("Total tasks: 1 Total resources: 0"));
}
public static Project CreateTestProjectWithCustomFieldWithoutResource()
{
var project = new Project();
project.Set(Prj.StartDate, new DateTime(2015, 3, 6, 8, 0, 0));
var attr = ExtendedAttributeDefinition.CreateTaskDefinition(CustomFieldType.Text, ExtendedAttributeTask.Text1, "Custom");
project.ExtendedAttributes.Add(attr);
var task = project.RootTask.Children.Add("Task");
var attribute = attr.CreateExtendedAttribute();
task.ExtendedAttributes.Add(attribute);
return project;
}
Shows how to use text functions with extended attributes.
public static void EvaluateStrConv()
{
var project = CreateTestProjectWithCustomField3();
var task = project.RootTask.Children.GetById(1);
// Set formulas and print extended attribute value
project.ExtendedAttributes[0].Formula = "StrConv(\"sTring and sTRINg\",3)";
Console.WriteLine(task.ExtendedAttributes[0].TextValue);
project.ExtendedAttributes[0].Formula = "StrConv(\"sTring and sTRINg\",1)";
Console.WriteLine(task.ExtendedAttributes[0].TextValue);
project.ExtendedAttributes[0].Formula = "StrConv(\"sTring and sTRINg\",2)";
Console.WriteLine(task.ExtendedAttributes[0].TextValue);
}
public static void EvaluateStringFunction()
{
var project = CreateTestProjectWithCustomField3();
var task = project.RootTask.Children.GetById(1);
// Set formulas and print extended attribute value
project.ExtendedAttributes[0].Formula = "String(5, 40)";
Console.WriteLine(task.ExtendedAttributes[0].TextValue);
project.ExtendedAttributes[0].Formula = "String(5, \"A\")";
Console.WriteLine(task.ExtendedAttributes[0].TextValue);
project.ExtendedAttributes[0].Formula = "String(-5, \"A\")";
// #Error
Console.WriteLine(task.ExtendedAttributes[0].TextValue);
}
public static Project CreateTestProjectWithCustomField3()
{
var project = new Project();
var attr = ExtendedAttributeDefinition.CreateTaskDefinition(CustomFieldType.Text, ExtendedAttributeTask.Text1, "Custom Field");
project.ExtendedAttributes.Add(attr);
var task = project.RootTask.Children.Add("Task");
var attribute = attr.CreateExtendedAttribute();
task.ExtendedAttributes.Add(attribute);
return project;
}
Shows how to use extended attribute functions by using task’s and/or resource’s fields.
public void UsingTasksAndResourceFieldsInFormulaCalculations()
{
try
{
var project = CreateTestProjectWithCustomField7();
var task = project.RootTask.Children.GetById(1);
// Set formula for extended attribute
var extendedAttributeDefinition1 = project.ExtendedAttributes[0];
extendedAttributeDefinition1.Alias = "Days from finish to deadline";
extendedAttributeDefinition1.Formula = "[Deadline] - [Finish]";
// Set Task Deadline and save project
task.Set(Tsk.Deadline, new DateTime(2015, 3, 20, 17, 0, 0));
project.Save(OutDir + "UsingTasksAndResourceFields_out.mpp", SaveFileFormat.Mpp);
}
catch (NotSupportedException ex)
{
Console.WriteLine(
ex.Message
+ "\nThis example will only work if you apply a valid Aspose License. You can purchase full license or get 30 day temporary license from http://www.aspose.com/purchase/default.aspx.");
}
}
// Helper method to create project
public static Project CreateTestProjectWithCustomField7()
{
var project = new Project(DataDir + "Blank2010.mpp");
project.Set(Prj.StartDate, new DateTime(2015, 3, 6, 8, 0, 0));
// Add new task with extended attribute
var task = project.RootTask.Children.Add("Task");
var extendedAttributeDefinition = ExtendedAttributeDefinition.CreateTaskDefinition(CustomFieldType.Text, ExtendedAttributeTask.Text5, "My Ext Attr");
project.ExtendedAttributes.Add(extendedAttributeDefinition);
var extendedAttribute = extendedAttributeDefinition.CreateExtendedAttribute();
task.ExtendedAttributes.Add(extendedAttribute);
// Add resource and resource assignment
var rsc = project.Resources.Add("Rsc");
project.ResourceAssignments.Add(task, rsc);
return project;
}
Shows how to use common math functions with extended attributes.
public static void EvaluateChoose()
{
var project = CreateTestProjectWithCustomField();
// Set Formula
project.ExtendedAttributes[0].Formula = "Choose(3, \"This is a\", \"right\", \"choice\")";
// Print extended attribute value
var task = project.RootTask.Children.GetById(1);
Console.WriteLine(task.ExtendedAttributes[0].TextValue);
}
public static void EvaluateIsNumeric()
{
string[] numericFormulas =
{
"IsNumeric('AAA')", @"IsNUmeric(1)", "IsNumeric(1<0)", "IsNumeric(\"1.1\")", "IsNumeric(Choose((2 + Sgn(2^-3)), 123, \"one two three\"))"
};
var project = CreateTestProjectWithCustomField();
foreach (var numericFormula in numericFormulas)
{
// Set Formula
project.ExtendedAttributes[0].Formula = numericFormula;
// Print extended attribute value
var task = project.RootTask.Children.GetById(1);
Console.WriteLine(task.ExtendedAttributes[0].TextValue);
}
}
public static void EvaluateSwitch()
{
var project = CreateTestProjectWithCustomField();
// Set Formula
project.ExtendedAttributes[0].Formula = "Switch( 0 < 1, \"0 is lesser than 1\", 0 > 1, \"0 is greater than 1\")";
// Print extended attribute value
var task = project.RootTask.Children.GetById(1);
Console.WriteLine(task.ExtendedAttributes[0].TextValue);
}
public static Project CreateTestProjectWithCustomField()
{
var project = new Project();
var definition = ExtendedAttributeDefinition.CreateTaskDefinition(CustomFieldType.Text, ExtendedAttributeTask.Text1, "Custom Field");
project.ExtendedAttributes.Add(definition);
var task = project.RootTask.Children.Add("Task");
var attribute = definition.CreateExtendedAttribute();
task.ExtendedAttributes.Add(attribute);
return project;
}
See Also
- class ExtendedAttributeDefinition
- namespace Aspose.Tasks
- assembly Aspose.Tasks