Removing existing columns and adding custom columns

Hi,

I am trying to read an existing “.mpp” file and manipulate it.
I want to remove the existing columns and add my own custom columns(that i want to create dynamically).

Does Aspose.Tasks support doing that? I am not able to find anything much useful to make it clear.


Hi,


Thanks for writing to Aspose support team.

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”);

//add extended attribute definition
ExtendedAttributeDefinition ead = new ExtendedAttributeDefinition();
ead.FieldName = “Text1”;
ead.Alias = “Description”;
ead.FieldId = Convert.ToInt32(ExtendedAttributeTask.Text1).ToString();
prj.ExtendedAttributes = new List();
prj.ExtendedAttributes.Add(ead);

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.

Hi Kashif, thanks for your quick reply.


With the help of information you have shared, i have been able to achieve what i was trying to do.
But i also want the same columns to appear in the exported pdf too, is that possible?

Hi,

We are sorry for a delayed response.

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 "";
}