Is there an easy way to copy a style?


#1

Is there an easy way to copy a style if I create a style, and I need another version of that style that is exactly the same, with one minor change? (Like bolding the font, or changing the foreground color.) I have not found an easy way of copying a style other than creating a new style and then writing a line of code for copying each property. For example if I add a new style like newStyle, and want it to be a copy of originalStyle for every style property I would need a line like the following.

newStyle.Font.Color = originalStyle.Font.Color;

If I just tried the following:
Style newStyle = originalStyle;
And then made changes to newStyle, original style would be changed as well.

Is there an easy way to copy a style without having to write a line of code to copy each property and still create 2 seperate styles that can be modified without affecting each other?

Much Appreciated,

Glenn Engelbart


#2

Dear Glenn,

Thanks for your consideration.

I plan to add a Copy method to Style class at the end of this week. Please wait for about 2 days.


#3

Dear Glenn,

The Hot Fix 1.5.10 is released. Please download it.
The Style.Copy method is added. You can refer to http://www.aspose.com/forums/ShowPost.aspx?PostID=1170

Aspose.Excel.Style style1 = excel.Styles[excel.Styles.Add()];
style1.ForegroundColor = Color.Red;
style1.Font.Name = “Times New Roman”;
style1.Font.Size = 14;
style1.HorizontalAlignment = TextAlignmentType.Center;

Aspose.Excel.Style style2 = excel.Styles[excel.Styles.Add()];

style2.Copy(style1);

style2.Font.Name = “Courier New”;
style2.HorizontalAlignment = TextAlignmentType.Right;
style2.ForegroundColor = Color.Yellow;


#4

Thanks much for adding the method. There still is one problem. It isn’t copying BorderTypes: In my copy method I used the following code.

newStyle.Borders[BorderType.BottomBorder].LineStyle = originalStyle.Borders[BorderType.BottomBorder].LineStyle;
newStyle.Borders[BorderType.TopBorder].LineStyle = originalStyle.Borders[BorderType.TopBorder].LineStyle;
newStyle.Borders[BorderType.RightBorder].LineStyle = originalStyle.Borders[BorderType.RightBorder].LineStyle;
newStyle.Borders[BorderType.LeftBorder].LineStyle = originalStyle.Borders[BorderType.LeftBorder].LineStyle;

Is it possible to add borderTypes to the copy style method?


#5

Dear Glenn,

Please download Fix 1.5.10.1.

Thanks for your suggestion.