bug 58043: provide some compatibility between HSSF and XSSF in regards to values for CellStyle.setRotation(). Also adjust JavaDoc to mention the remaining things to note

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1722716 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2016-01-03 13:28:17 +00:00
parent ff8592b190
commit c4c9ac0ccf
9 changed files with 179 additions and 11 deletions

View File

@ -410,7 +410,7 @@ public final class ExtendedFormatRecord
}
/**
* set the degree of rotation. (I've not actually seen this used anywhere)
* set the degree of rotation.
*
*
* @param rotation the degree of rotation
@ -1145,7 +1145,7 @@ public final class ExtendedFormatRecord
}
/**
* get the degree of rotation. (I've not actually seen this used anywhere)
* get the degree of rotation.
*
*
* @return rotation - the degree of rotation

View File

@ -324,6 +324,12 @@ public final class HSSFCellStyle implements CellStyle {
/**
* set the degree of rotation for the text in the cell
*
* Note: HSSF uses values from -90 to 90 degrees, whereas XSSF
* uses values from 0 to 180 degrees. The implementations of this method will map between these two value-ranges
* accordingly, however the corresponding getter is returning values in the range mandated by the current type
* of Excel file-format that this CellStyle is applied to.
*
* @param rotation degrees (between -90 and 90 degrees, of 0xff for vertical)
*/
@Override
@ -337,7 +343,11 @@ public final class HSSFCellStyle implements CellStyle {
//The 4th quadrant (-1 to -90) is stored as (91 to 180)
rotation = (short)(90 - rotation);
}
else if ((rotation < -90) ||(rotation > 90)) {
else if (rotation > 90 && rotation <= 180) {
// stay compatible with the range used by XSSF, map from ]90..180] to ]0..-90]
// we actually don't need to do anything here as the internal value is stored in [0-180] anyway!
}
else if ((rotation < -90) || (rotation > 90)) {
//Do not allow an incorrect rotation to be set
throw new IllegalArgumentException("The rotation must be between -90 and 90 degrees, or 0xff");
}

View File

@ -363,17 +363,26 @@ public interface CellStyle {
short getVerticalAlignment();
/**
* set the degree of rotation for the text in the cell
* @param rotation degrees (between -90 and 90 degrees)
* set the degree of rotation for the text in the cell.
*
* Note: HSSF uses values from -90 to 90 degrees, whereas XSSF
* uses values from 0 to 180 degrees. The implementations of this method will map between these two value-ranges
* accordingly, however the corresponding getter is returning values in the range mandated by the current type
* of Excel file-format that this CellStyle is applied to.
*
* @param rotation degrees (see note above)
*/
void setRotation(short rotation);
/**
* get the degree of rotation for the text in the cell
* @return rotation degrees (between -90 and 90 degrees)
* get the degree of rotation for the text in the cell.
*
* Note: HSSF uses values from -90 to 90 degrees, whereas XSSF
* uses values from 0 to 180 degrees. The implementations of this method will map between these two value-ranges
* value-range as used by the type of Excel file-format that this CellStyle is applied to.
*
* @return rotation degrees (see note above)
*/
short getRotation();
/**

View File

@ -1364,6 +1364,11 @@ public void setFillPattern(short fp) {
* <code>[degrees below horizon] = 90 - textRotation.</code>
* </p>
*
* Note: HSSF uses values from -90 to 90 degrees, whereas XSSF
* uses values from 0 to 180 degrees. The implementations of this method will map between these two value-ranges
* accordingly, however the corresponding getter is returning values in the range mandated by the current type
* of Excel file-format that this CellStyle is applied to.
*
* @param rotation - the rotation degrees (between 0 and 180 degrees)
*/
@Override

View File

@ -133,9 +133,17 @@ public class XSSFCellAlignment {
* <code>[degrees below horizon] = 90 - textRotation.</code>
* </p>
*
* Note: HSSF uses values from -90 to 90 degrees, whereas XSSF
* uses values from 0 to 180 degrees. The implementations of this method will map between these two value-ranges
* accordingly, however the corresponding getter is returning values in the range mandated by the current type
* of Excel file-format that this CellStyle is applied to.
*
* @param rotation - the rotation degrees (between 0 and 180 degrees)
*/
public void setTextRotation(long rotation) {
if(rotation < 0 && rotation >= -90) {
rotation = 90 + ((-1)*rotation);
}
cellAlignement.setTextRotation(rotation);
}

View File

@ -2906,4 +2906,52 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
wb.close();
}
private void createXls() throws IOException
{
Workbook workbook = new HSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream("/tmp/rotated.xls");
Sheet sheet1 = workbook.createSheet();
Row row1 = sheet1.createRow((short) 0);
Cell cell1 = row1.createCell(0);
cell1.setCellValue("Successful rotated text.");
CellStyle style = workbook.createCellStyle();
style.setRotation((short) -90);
cell1.setCellStyle(style);
workbook.write(fileOut);
fileOut.close();
workbook.close();
}
private void createXlsx() throws IOException {
Workbook workbook = new XSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream("/tmp/rotated.xlsx");
Sheet sheet1 = workbook.createSheet();
Row row1 = sheet1.createRow((short) 0);
Cell cell1 = row1.createCell(0);
cell1.setCellValue("Unsuccessful rotated text.");
CellStyle style = workbook.createCellStyle();
style.setRotation((short) -90);
cell1.setCellStyle(style);
workbook.write(fileOut);
fileOut.close();
workbook.close();
}
@Ignore("Creates files for checking results manually, actual values are tested in Test*CellStyle")
@Test
public void test58043() throws Exception {
createXls();
createXlsx();
}
}

View File

@ -1059,4 +1059,31 @@ public class TestXSSFCellStyle {
target.close();
reference.close();
}
@Test
public void test58043() {
assertEquals(0, cellStyle.getRotation());
cellStyle.setRotation((short)89);
assertEquals(89, cellStyle.getRotation());
cellStyle.setRotation((short)90);
assertEquals(90, cellStyle.getRotation());
cellStyle.setRotation((short)179);
assertEquals(179, cellStyle.getRotation());
cellStyle.setRotation((short)180);
assertEquals(180, cellStyle.getRotation());
// negative values are mapped to the correct values for compatibility between HSSF and XSSF
cellStyle.setRotation((short)-1);
assertEquals(91, cellStyle.getRotation());
cellStyle.setRotation((short)-89);
assertEquals(179, cellStyle.getRotation());
cellStyle.setRotation((short)-90);
assertEquals(180, cellStyle.getRotation());
}
}

View File

@ -129,4 +129,28 @@ public final class TestExtendedFormatRecord extends TestCase {
for (int i = 0; i < data.length; i++)
assertEquals("At offset " + i, data[i], recordBytes[i + 4]);
}
public void testRotation() {
ExtendedFormatRecord record = createEFR();
assertEquals(0, record.getRotation());
record.setRotation((short)1);
assertEquals(1, record.getRotation());
record.setRotation((short)89);
assertEquals(89, record.getRotation());
record.setRotation((short)90);
assertEquals(90, record.getRotation());
// internally values below zero are stored differently
record.setRotation((short)-1);
assertEquals(255, record.getRotation());
record.setRotation((short)-89);
assertEquals(-77, 90-record.getRotation());
record.setRotation((short)-90);
assertEquals(-76, 90-record.getRotation());
}
}

View File

@ -23,8 +23,6 @@ import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
@ -34,6 +32,9 @@ import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.LocaleUtil;
import org.apache.poi.util.TempFile;
import org.junit.Test;
import junit.framework.TestCase;
/**
* Class to test cell styling functionality
@ -507,4 +508,40 @@ public final class TestCellStyle extends TestCase {
// out.close();
// }
}
@Test
public void test58043() throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFCellStyle cellStyle = wb.createCellStyle();
assertEquals(0, cellStyle.getRotation());
cellStyle.setRotation((short)89);
assertEquals(89, cellStyle.getRotation());
cellStyle.setRotation((short)90);
assertEquals(90, cellStyle.getRotation());
cellStyle.setRotation((short)-1);
assertEquals(-1, cellStyle.getRotation());
cellStyle.setRotation((short)-89);
assertEquals(-89, cellStyle.getRotation());
cellStyle.setRotation((short)-90);
assertEquals(-90, cellStyle.getRotation());
cellStyle.setRotation((short)-89);
assertEquals(-89, cellStyle.getRotation());
// values above 90 are mapped to the correct values for compatibility between HSSF and XSSF
cellStyle.setRotation((short)179);
assertEquals(-89, cellStyle.getRotation());
cellStyle.setRotation((short)180);
assertEquals(-90, cellStyle.getRotation());
wb.close();
}
}