How to set background color for specific cell

Please help me with code example - how to set background color for specific cell of the task. For example I want to highlight Duration, Start, Finish of the selected task.


Thanks.

Hi,

Thank you for contacting Aspose support team.

You may please use following sample code which demonstrates the formatting of task cell and share the feedback.

string path = url + “Tasks_793461_793835\”;
Project project = new Project(@"D:\Aspose_DotNetProjects\blank2010.mpp");
project.Set(Prj.NewTasksAreManual, false);
project.RootTask.Children.Add("Task 1");
project.RootTask.Children.Add("Task 2");

GanttChartView view = project.Views.ToList()[1] as GanttChartView;
// Set first task name text style
TableTextStyle tableTextStyle = new TableTextStyle(1);
tableTextStyle.BackgroundColor = Color.Blue;
tableTextStyle.Color = Color.Red;
tableTextStyle.Field = Field.TaskName;
tableTextStyle.FontStyle = FontStyle.Bold | FontStyle.Italic;
view.TableTextStyles.Add(tableTextStyle);

// Set second task duration text style
tableTextStyle = new TableTextStyle(2);
tableTextStyle.Color = Color.Yellow;
//tableTextStyle.BackgroundColor = Color.Red;
tableTextStyle.Field = Field.TaskDurationText;
tableTextStyle.FontStyle = FontStyle.Underline;
view.TableTextStyles.Add(tableTextStyle);

MPPSaveOptions saveOptions = new MPPSaveOptions();
saveOptions.WriteViewData = true;

project.Save(path + "temp updated.mpp", saveOptions);

Thanks for the sample, can you please also help me with saving mpp as Stream, the problem place is here


var ms = new MemoryStream();
var saveOptions = new MPPSaveOptions {WriteViewData = true};
project.Save(ms, saveOptions);

I try to use
Project.Save(System.IO.Stream,Aspose.Tasks.Saving.SaveOptions)

but can’t resolve MPPSaveOptions to SaveOptions convertation

Hi,


I have checked the scenario and observed that it is an issue and needs consideration. It is logged under Id: TASKSNET-1859 for further investigation by the product team. You will be automatically notified once any update is received.

@makardotnet,

We have just released Aspose.Tasks for .NET 17.7 version where this issue has been fixed. Please download this latest version for testing at your end and let us know if we can be of any additional help to you.

Can I get this example in Python?

@johnWeeksGrp ,
Yes. We need some time to prepare the example.

@johnWeeksGrp ,
here is the example in Python:

prj = tsk.Project()
license = tsk.License()
license.set_license("license.lic")
view = cast(tsk.GanttChartView, prj.views.get_by_name("&Gantt Chart"))

tableTextStyle = visualization.TableTextStyle(1, visualization.FontStyles.BOLD | visualization.FontStyles.ITALIC)
tableTextStyle.background_color = drawing.Color.blue
tableTextStyle.color = drawing.Color.red
tableTextStyle.field = tsk.Field.TASK_NAME
view.table_text_styles.insert(0, tableTextStyle)

# Set second task duration text style
tableTextStyle2 = visualization.TableTextStyle(2, visualization.FontStyles.UNDERLINE)
tableTextStyle2.color = drawing.Color.yellow
tableTextStyle2.field = tsk.Field.TASK_DURATION_TEXT
view.table_text_styles.insert(1, tableTextStyle2)


saveOptions = saving.MPPSaveOptions()
saveOptions.write_view_data = True

prj.save("output.mpp", saveOptions)

This is good stuff, is there a way to set these to individual cells?

@johnWeeksGrp ,
the defined styles are applied to individual cell.
For example, style

# 
tableTextStyle = visualization.TableTextStyle(1, ...
...
tableTextStyle.field = tsk.Field.TASK_NAME

is applied to the cell in a row corresponding to the task with Unique Id = 1 and in the column labeled ‘Task Name’

Updated code snippet for Aspose.Tasks for .NET 23.12:

Project project = new Project(@"blank2010.mpp");
project.RootTask.Children.Add("Task 1");
project.RootTask.Children.Add("Task 2");

GanttChartView view = project.Views.ToList()[1] as GanttChartView;
// Set first task name text style
TableTextStyle tableTextStyle = new TableTextStyle(1, 11, FontStyles.Bold | FontStyles.Italic);
tableTextStyle.BackgroundColor = Color.Blue;
tableTextStyle.Color = Color.Red;
tableTextStyle.Field = Field.TaskName;
view.TableTextStyles.Add(tableTextStyle);

// Set second task duration text style
tableTextStyle = new TableTextStyle(2, 11, FontStyles.Underline);
tableTextStyle.Color = Color.Yellow;
tableTextStyle.Field = Field.TaskDurationText;
view.TableTextStyles.Add(tableTextStyle);

MPPSaveOptions saveOptions = new MPPSaveOptions();
saveOptions.WriteViewData = true;

project.Save("temp updated.mpp", saveOptions);