Cannot apply a copied style without setting explicit to true or using a flag

var wb = new Workbook();

Worksheet sheet = wb.Worksheets[0];
Cell cell = sheet.Cells["B2"];
cell.PutValue(123.45);
Style heading1 = wb.CreateBuiltinStyle(BuiltinStyleType.Header1);
Style myStyle = wb.CreateStyle();
myStyle.Number = 41;
myStyle.Copy(heading1);

// This does not work
cell.SetStyle(myStyle);

// This does work. However, I want to customize the style without changing the built-in style of the documentt.
//cell.SetStyle(heading1);

// This does work. However, I don't want to overwrite existing style properties of the cell tha are NOT explicitly set in the built-in style.
//cell.SetStyle(myStyle, true);

// Technically, I can also create a flag. The problem is how to figure out which flag properties I need to set to true programatically? I don't want to hard-code it.

wb.Save("Demo1.xlsx");

@bingxie,

Thanks for the sample and details.

Please notice, after an initial test, I am able to reproduce the issue as you mentioned. I found we cannot apply a copied style without setting explicit to true or using a flag. We need to evaluate it in details whether it is by design and one has to explicitly overwrite the style by setting the flag to true if he wants to apply the copied style or it is an issue with the APIs. I have logged a ticket with an id “CELLSNET-51921” for your issue. We will look into it soon.

Once we have an update on it, we will let you know.

@bingxie
We have fixed this issue.
And you also can use the following codes to set your excepted style:

Style heading1 = wb.CreateBuiltinStyle(BuiltinStyleType.Header1);
//Style myStyle = wb.CreateStyle();
//myStyle.Number = 41;
//myStyle.Copy(heading1);
// This does not work
cell.SetStyle(heading1);
Style myStyle = cell.GetStyle();
myStyle.Number = 41;
cell.SetStyle(myStyle);

@simon.zhao Thanks for your update.

I’m a bit puzzled by the sample code you shared. Can you please clarify?

Also, I noticed that the “builtin styles” created from Workbook.CreateBuiltinStyle are not true built-in styles.

@bingxie
1,There is an issue that copying named style in the current version.
If you set the named style to the cell, a new cell style will be applied to the cell.The issue will be ignored.
2,Could you explain more about “not true built-in styles.”?

@simon.zhao Excel marks them as “Custom” styles in the UI. Also, when opened in an Excel in a differen language, these “fake” built-in styles will keep their name in English in the “Custom” styles section whereas real built-in styles will match the language of the Office and show in their normal sections in the Style panel.

@bingxie,

Thanks for providing further details.

We will evaluate and get back to you soon.

@bingxie
Thanks for your info.
We have found the issue and logged it as CELLSNET-51944.

@bingxie
Please check the attached file dest.zip (6.0 KB)
for builtin styles with the following codes:
Style heading1 = wb.CreateBuiltinStyle(BuiltinStyleType.Header1);
cell.SetStyle(heading1);
Style myStyle = cell.GetStyle();
myStyle.Number = 41;
cell.SetStyle( myStyle);
Style.Copy() method will copy the named style as a cell style.
So please directly set the named style to the cell.

@simon.zhao I have tried your steps, but I’m still seeing the problem.

var wb = new Workbook();
Worksheet sheet = wb.Worksheets[0];

var cell = sheet.Cells["A2"];
cell.PutValue("カテゴリー1");

Style heading1 = wb.CreateBuiltinStyle(BuiltinStyleType.Header1);
cell.SetStyle(heading1);
Style myStyle = cell.GetStyle();
myStyle.Number = 41;
cell.SetStyle(myStyle);
wb.Save("Demo5.xlsx");

I have attached the screenshot and generated file.

Demo5.zip (6.7 KB)
Issue.png (12.4 KB)

@bingxie
We have fixed this issue and will release 22.9 in the next week.
Please wait for 22.9.

@simon.zhao Thanks for the clarification. I somehow thought you were suggesting I did not use the API correctly. We will verify this fix next week.

@bingxie,

You are welcome.

The issues you have found earlier (filed as CELLSNET-51944) have been fixed in this update. This message was posted using Bugs notification tool by johnson.shi