Adding new columns is supported by Aspose.Tasks using ExtendedAttribute class. It allows you to add extended attributes to tasks, resources and assignments. Following is the sample code which adds extended attribute “Description” to all the tasks in an existing project.
Project prj = new Project(@“D:\Aspose\Blank2013.mpp”);
foreach (Task task in prj.RootTask.Children) { //ExtendedAttribute ea = new List(); ExtendedAttribute ea = new ExtendedAttribute(); ea.FieldId = ead.FieldId; ea.Value = string.Format(“Description of ‘{0}’.”, task.Name); task.ExtendedAttribute.Add(ea); } prj.Save(@“D:\Aspose\project_res.mpp”, Aspose.Tasks.Saving.SaveFileFormat.MPP);
This new column can be seen in MS Project by selecting it from “Add New Column” in Gantt Chart view.
Regarding hiding the columns, MS Project provides facility to hide individual column. This view can be saved in MPP however this view is not saved in XML.
We are afraid to inform that Aspose.Tasks does not provide any facility to control the view in a project. However you can achieve this feature by using some template MPP project file. This template MPP file can be saved with desired view like hiding the “Resource Names” column. You may use this template MPP file with Aspose.Tasks to add tasks etc. and then saving file as MPP. This resultant MPP file will have the same view as set in template project file.
You can use the SaveOptions’ View class to add additional columns to the GanttChart view as shown in the following code sample. Please let us know if we can be of any additional help to you in this regard.
Sample Code:
static void TestPrint()
{
Project prj = newProject(@"RenderMe.MPP");
//add extended attribute definition
ExtendedAttributeDefinition ead = newExtendedAttributeDefinition();
ead.FieldName = "Text1";
ead.Alias = "Description";
ead.FieldId = Convert.ToInt32(ExtendedAttributeTask.Text1).ToString();
prj.ExtendedAttributes = newList<ExtendedAttributeDefinition>();
prj.ExtendedAttributes.Add(ead);
foreach (Task task in prj.RootTask.Children)
{
ExtendedAttribute ea = newExtendedAttribute();
ea.FieldId = ead.FieldId;
ea.Value = string.Format("Description of '{0}'.", task.Name);
task.ExtendedAttribute.Add(ea);
}
Task.Recalculate(prj.RootTask);
prj.UpdateReferences();
prj.CalcTaskIds();
prj.CalcTaskUids();
MemoryStream ms = newMemoryStream();
prj.Save(ms, Aspose.Tasks.Saving.SaveFileFormat.MPP); //To avoid exception at present - Fix in progress
Aspose.Tasks.Saving.SaveOptions options = new Aspose.Tasks.Saving.PdfSaveOptions();
options.Timescale = Timescale.Months;
options.View = ProjectView.GetDefaultGanttChartView();
GanttChartColumn col = options.View.Columns[2] asGanttChartColumn;
col.StringAlignment = StringAlignment.Center;
col = options.View.Columns[3] asGanttChartColumn;
col.StringAlignment = StringAlignment.Far;
col = options.View.Columns[4] asGanttChartColumn;
col.StringAlignment = StringAlignment.Far;
options.View.Columns.Add(newGanttChartColumn("Description", 100, newTaskToColumnTextConverter(TaskDescription)));
prj.Save(@"result GanttChart.pdf", options);
}
static private string TaskDescription(Task task)
{
if (task.ExtendedAttribute.Count > 0)
{
foreach (ExtendedAttribute ea in task.ExtendedAttribute)
{
if (ea.AttributeDefinition.Alias == "Description")
return ea.Value.ToString();
}
}
return "";
}