Hello,
I’m writing you because I found two issues with columns heads.
If the whole column name has only one font cell.getCharacters() is null
If font’s name is change for portion, color of text is changed too.
We use aspose-cell 8.8.1
Code:
ListObjectCollection tables = sheet.getListObjects();
if (tables != null) {
for (int j = 0; j < tables.getCount(); j++) {
if (tables.get(j) != null) {
ListColumnCollection columns = tables.get(j).getListColumns();
if (columns != null) {
Cell cell = null;
for (int c = 0; c < columns.getCount(); c++) {
ListColumn column = columns.get©;
if (column != null) {
Range range = column.getRange();
if (range != null) {
for (int r = 0; r < range.getRowCount(); r++) {
cell = range.get(r, 0);
FontSetting[] fontSettings = cell != null ? cell.getCharacters() : null; //NULL
if (fontSettings != null) {
for (FontSetting fontSetting : fontSettings) {
font = fontSetting != null ? fontSetting.getFont() : null;
if (font != null) {
Color clr = font.getColor(); //REAL is White, Here is Black
font.setName(exWCallback.checkFont(cell.getStringValue()
.substring(fontSetting.getStartIndex(),
fontSetting.getStartIndex() + fontSetting.getLength()),
font.getName())); //COLOR
font.setColor(clr);
}
}
}
else {
Style style = cell != null ? cell.getStyle() : null;
font = style != null ? style.getFont() : null;
if (font != null) {
font.setName(exWCallback.checkFont(cell.getStringValue(), font.getName()));
}
}
}
}
}
}
}
}
}
}
I’ve attached tested file
Best Regards
Hi,
Thanks for your posting and using Aspose.Cells.
Please see the following sample code that explains how to set the font of the portions of the text correctly. I have attached the screenshot showing the output excel file as well as the output excel of this code for your reference.
Java
Worksheet ws = wb.getWorksheets().get(“Sheet1”);
Cell cell = ws.getCells().get(“C8”);
FontSetting fnt = cell.characters(3, 8);
fnt.getFont().setName(“Cordia New”);
fnt.getFont().setColor(Color.getRed());
fnt.getFont().setSize(15);
FontSetting[] fnts = cell.getCharacters();
FontSetting[] allfnts = new FontSetting[fnts.length + 1];
System.arraycopy(fnts, 0, allfnts, 0, fnts.length);
allfnts[fnts.length] = fnt;
cell.setCharacters(allfnts);
wb.save(“output.xlsx”);
Hello,
Thank you for response.
Unfortunately it doesn’t contain explanation why getCharacters for head of second table is null and how I can get real color of original font.
If I want to change only font of text, not color, it doesn’t work
Hi,
Thanks for your posting and using Aspose.Cells.
The cells C8 and C12 are not same in terms of font settings. Just copy C8 and C12 and paste them into some other cells and you will find that color of cell C8 has changed but the color of cell C12 has not changed.
Besides the cell C12 has a single font, therefore GetCharacters() return null. If it has two or more fonts, then GetCharacters() would not return null.
I have also attached the modified xlsx file and screenshot for your reference.
Hello,
Thank you for response.
Sorry I don’t fully understand you. So there is no way to get text-color of C8? Why? What is so specific for that?
Hi,
Thanks for your posting and using Aspose.Cells.
We were able to observe this issue and found the color of the text in cell C8 is wrong. It should be White+Black but it is Black+Black.
Whenever GetCharacters() return null, you should use Cell.getStyle().getFont() property.
I have attached the modified xlsx file, which is same as yours except I copied cell C8 to C2.
Please see the following sample code and its console output for the reference. The console output for C2 and C8 should match but they don’t, so there is a problem.
Please let us know if this is the issue you are facing so that we log this issue in our database for investigation and a fix.
Java
Worksheet ws = wb.getWorksheets().get(0);
String[] cellNames = {“C2”, “C8”, “C12” };
for(int i=0; i<cellNames.length; i++)
{
Cell cell = ws.getCells().get(cellNames[i]);
System.out.println("Printing Fonts for Cell: " + cell.getName());
System.out.println();
//First check if GetCharacters return null
FontSetting[] fnts = cell.getCharacters();
if (fnts == null)
{
Style st = cell.getStyle();
Font fnt = st.getFont();
System.out.println(“Name: " + fnt.getName());
Color clr =fnt.getColor();
String clrName = “R=” + clr.getR() + “,G=”+clr.getG() + “,B=”+ clr.getB();
System.out.println(“Color: " + clrName);
System.out.println(”--------------------------------------------------------------”);
}
else
{
for(int j=0; j<fnts.length; j++)
{
System.out.println("Font Setting Number: " + j);
Font fnt = fnts[j].getFont();
Color clr =fnt.getColor();
String clrName = “R=” + clr.getR() + “,G=”+clr.getG() + “,B=”+ clr.getB();
System.out.println(“Name: " + fnt.getName());
System.out.println(“Color: " + clrName);
System.out.println();
}
System.out.println(”--------------------------------------------------------------”);
}
}//end-for
Console Output
Font Setting Number: 0
Name: Calibri
Color: R=-1,G=-1,B=-1
Font Setting Number: 1
Name: Adine Kirnberg
Color: R=0,G=0,B=0
--------------------------------------------------------------
Printing Fonts for Cell: C8
Font Setting Number: 0
Name: Calibri
Color: R=0,G=0,B=0
Font Setting Number: 1
Name: Adine Kirnberg
Color: R=0,G=0,B=0
--------------------------------------------------------------
Printing Fonts for Cell: C12
Name: Calibri
Color: R=0,G=0,B=0
--------------------------------------------------------------
Hello,
Thank you for investigation.
Yes please log the issue. Otherwise it looks really strange: impossibility of getting real color.
Best Regards
Hi,
Thanks for your posting and using Aspose.Cells.
We have looked into your issue further and found that the color of cell C8 is actually Black not White. It looks White because of Table Style. For such cells, you should use Cell.getDisplayStyle().getFont() method instead of Cell.getStyle().getFont() method.
Please see the following sample code and its console output for your reference.
Java
Worksheet ws = wb.getWorksheets().get(0);
Cell cell = ws.getCells().get(“C8”);
Style st = cell.getDisplayStyle();
Color clr = st.getFont().getColor();
int R = (int) clr.getR() & 0xFF;
int G = (int) clr.getG() & 0xFF;
int B = (int) clr.getB() & 0xFF;
String clrName = “R=” + R + “,G=” + G + “,B=” + B;
System.out.println(clrName);
Console Output
Hello,
Thank you very much for workaround, but I suppose the situation, when only font is changed but as result color of font is unexpectedly changed too, is not very good. May we hope that fix for it will be apply in the future?
Hi,
Thanks for your posting and using Aspose.Cells.
Please explain your point with some simplified sample code. I have tested your point with this code and changed the font name of the cells C8 and C11 and found their font has changed but their color is still White.
I have attached the output excel file generated by the code and screenshot illustrating this issue for a reference.
I have tested this issue with the latest version:
Aspose.Cells for Java 8.8.1.
Java
Worksheet ws = wb.getWorksheets().get(0);
String[] cellNames = { “C8”, “C11” };
for(int i=0; i<cellNames.length; i++)
{
Cell cell = ws.getCells().get(cellNames[i]);
Style st = cell.getStyle();
st.getFont().setName(“Arial”);
cell.setStyle(st);
}
wb.save(dirPath + “out.xlsx”);
Hello,
Since first table contains rich text with two posrtions of different style I used:
ListObjectCollection tables = sheet.getListObjects();
if (tables != null) {
for (int j = 0; j < tables.getCount(); j++) {
if (tables.get(j) != null) {
ListColumnCollection columns = tables.get(j).getListColumns();
if (columns != null) {
Cell cell = null;
for (int c = 0; c < columns.getCount(); c++) {
ListColumn column = columns.get©;
if (column != null) {
Range range = column.getRange();
if (range != null) {
for (int r = 0; r < range.getRowCount(); r++) {
cell = range.get(r, 0);
FontSetting[] fontSettings = cell != null ? cell.getCharacters() : null; //NULL
if (fontSettings != null) {
for (FontSetting fontSetting : fontSettings) {
font = fontSetting != null ? fontSetting.getFont() : null;
if (font != null) {
font.setName(exWCallback.checkFont(cell.getStringValue()
.substring(fontSetting.getStartIndex(),
fontSetting.getStartIndex() + fontSetting.getLength()),
font.getName())); //COLOR
}
}
}
else {
Style style = cell != null ? cell.getStyle() : null;
font = style != null ? style.getFont() : null;
if (font != null) {
font.setName(exWCallback.checkFont(cell.getStringValue(), font.getName()));
}
}
}
}
}
}
}
}
}
}
So color is changed when I change font of portion when there are multiple FontSettings for one cell
Best Regards
Hi,
Thanks for your posting and using Aspose.Cells.
This is a special case and the issue occurs because of the fact that cell contains rich text and it is inside the table. So we have logged this issue in our database for investigation. We will look into it and fix this issue. Once the issue is resolved or we have some other update for you, we will update you asap.
This issue has been logged as improvement as follows
- CELLSNET-44480 - Apply table style to rich text setting
Hi,
Thanks for using Aspose.Cells.
This is to inform you that we have fixed your issue CELLSNET-44480 now. We will soon provide the fix after performing QA and including other enhancements and fixes.
Hello,
Thank you for so quick reaction and fixing.
Looking forward for official release
Hi,
The issues you have found earlier (filed as CELLSNET-44480) have been fixed in this update.
This message was posted using Notification2Forum from Downloads module by Aspose Notifier.