We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Row.applyStyle works unexpected

Hi, i’ve found some unexpected behavoir in Row.applyStyle method.


Please note, that it is EXAMPLE, that illustrated behavoir our complex system. Described actions performs on different stages of our system and we unable to transport information about actions, that were performed on another stage.

Following code do:
1) We have a book with two ranges A8:F11 and G8:M11
2) First line of each range should be copied several times on next row
3) When row copied, it should to copy row’s style

So when copied first range all is correct. But when copied second range styles of first range are lost. Can you please explain a behavoir “applyStyle” method, or fix this issue (if it is issue)

@Test
public void copyRowStyleTest() {
try {
Workbook wb = new Workbook(“D:\in.xlsx”);
Worksheet ws = wb.getWorksheets().get(0);

copyRow(ws, 7, 8, 0, 5);
copyRow(ws, 7, 9, 0, 5);

wb.save(“D:\1.xlsx”);

copyRow(ws, 7, 8, 6, 12);
copyRow(ws, 7, 9, 6, 12);

wb.save(“D:\2.xlsx”);

} catch (Exception e) {
e.printStackTrace();
assertNull(e);
}
}

private void copyRow(Worksheet ws, int srcRow, int dstRow, int col, int col1) throws AsposeBookAdapterException {
copyRowStyle(ws, srcRow, ws, dstRow);
copyRange(ws, dstRow, col, dstRow, col1, ws, srcRow, col, srcRow, col1);
}

public static void copyRowStyle(Worksheet srcSheet, int srcRow, Worksheet dstSheet, int dstRow) {
RowCollection srcRows = srcSheet.getCells().getRows();
RowCollection dstRows = dstSheet.getCells().getRows();
StyleFlag styleFlag = new StyleFlag();
styleFlag.setAll(true);
dstRows.get(dstRow).applyStyle(srcRows.get(srcRow).getStyle(), styleFlag);
}

public static void copyRange(Worksheet dstSheet, int dstRow, int dstCol, int dstRow1, int dstCol1, Worksheet srcSheet, int srcRow, int srcCol, int srcRow1, int srcCol1) throws AsposeBookAdapterException {
Range range = createRange(dstSheet, dstRow, dstCol, dstRow1, dstCol1);
Range source = createRange(srcSheet, srcRow, srcCol, srcRow1, srcCol1);

copy(range, source, PasteType.ALL);
}

private static Range createRange(Worksheet worksheet, int row, int col, int row1, int col1) {
return worksheet.getCells().createRange(row, col, row1 - row + 1, col1 - col + 1);
}

private static void copy(Range range, Range source, int format) {
PasteOptions options = new PasteOptions();
options.setPasteType(format);
range.copy(source, options);
}

Best regards. Alexey

Hi,


I suspect the culprit line might be:
StyleFlag styleFlag = new StyleFlag();
styleFlag.setAll(true);

Please do not set all the options on with StyleFlag.setAll(true) rather you should choose which StyleFlag attributes you want to set/apply accordingly and only make them on individually.

Hope, this helps a bit.

Thank you.