--- /dev/null
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+package org.apache.poi.xssf.usermodel;
+
+import org.apache.poi.ss.usermodel.Comment;
+import org.apache.poi.ss.usermodel.RichTextString;
+import org.apache.poi.xssf.usermodel.extensions.XSSFSheetComments;
+import org.apache.poi.xssf.util.CellReference;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
+
+public class XSSFComment implements Comment {
+
+ private CTComment comment;
+ private XSSFSheetComments sheetComments;
+
+ public XSSFComment(XSSFSheetComments sheetComments, CTComment comment) {
+ this.comment = comment;
+ this.sheetComments = sheetComments;
+ }
+
+ public XSSFComment(XSSFSheetComments sheetComments) {
+ this(sheetComments, CTComment.Factory.newInstance());
+ }
+
+ public String getAuthor() {
+ return sheetComments.getAuthor(comment.getAuthorId());
+ }
+
+ public short getColumn() {
+ return (new CellReference(comment.getRef())).getCol();
+ }
+
+ public int getRow() {
+ return (new CellReference(comment.getRef())).getRow();
+ }
+
+ public boolean isVisible() {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ public void setAuthor(String author) {
+ sheetComments.findAuthor(author);
+ }
+
+ public void setColumn(short col) {
+ initializeRef();
+ String newRef = (new CellReference(comment.getRef())).convertRowColToString((short) getRow(), col);
+ comment.setRef(newRef);
+ }
+
+ private void initializeRef() {
+ if (comment.getRef() == null) {
+ comment.setRef("A1");
+ }
+ }
+
+ public void setRow(int row) {
+ initializeRef();
+ String newRef = (new CellReference(comment.getRef())).convertRowColToString((short) row, getColumn());
+ comment.setRef(newRef);
+ }
+
+ public void setString(RichTextString string) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void setVisible(boolean visible) {
+ // TODO Auto-generated method stub
+
+ }
+
+}
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageBreak;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageMargins;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageSetUpPr;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPrintOptions;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetFormatPr;
import org.apache.poi.xssf.usermodel.helpers.ColumnHelper;
import org.apache.poi.xssf.util.CellReference;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBreak;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDialogsheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageBreak;
protected CTSheet sheet;
protected CTWorksheet worksheet;
protected CTDialogsheet dialogsheet;
+ protected CTComment comment;
protected List<Row> rows;
protected ColumnHelper columnHelper;
protected XSSFWorkbook workbook;
}
public void shiftRows(int startRow, int endRow, int n) {
- // TODO Auto-generated method stub
-
+ shiftRows(startRow, endRow, n, false, false);
}
public void shiftRows(int startRow, int endRow, int n, boolean copyRowHeight, boolean resetOriginalRowHeight) {
- // TODO Auto-generated method stub
-
+ for (Iterator<Row> it = rowIterator() ; it.hasNext() ; ) {
+ Row row = it.next();
+ if (!copyRowHeight) {
+ row.setHeight((short)0);
+ }
+ if (resetOriginalRowHeight && getDefaultRowHeight() >= 0) {
+ row.setHeight(getDefaultRowHeight());
+ }
+ if (removeRow(startRow, endRow, n, row.getRowNum())) {
+ it.remove();
+ }
+ else if (row.getRowNum() >= startRow && row.getRowNum() <= endRow) {
+ row.setRowNum(row.getRowNum() + n);
+ }
+ }
+ }
+
+ private boolean removeRow(int startRow, int endRow, int n, int rownum) {
+ if (rownum >= (startRow + n) && rownum <= (endRow + n)) {
+ if (n > 0 && rownum > endRow) {
+ return true;
+ }
+ else if (n < 0 && rownum < startRow) {
+ return true;
+ }
+ }
+ return false;
}
public void showInPane(short toprow, short leftcol) {
--- /dev/null
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+package org.apache.poi.xssf.usermodel.extensions;
+
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTAuthors;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments;
+
+public class XSSFSheetComments {
+
+ private CTComments comments;
+
+ public XSSFSheetComments() {
+ this(CTComments.Factory.newInstance());
+ }
+
+ public XSSFSheetComments(CTComments comments) {
+ this.comments = comments;
+ }
+
+ public String getAuthor(long authorId) {
+ return getCommentsAuthors().getAuthorArray((int)authorId);
+ }
+
+ public int findAuthor(String author) {
+ for (int i = 0 ; i < getCommentsAuthors().sizeOfAuthorArray() ; i++) {
+ if (getCommentsAuthors().getAuthorArray(i).equals(author)) {
+ return i;
+ }
+ }
+ return addNewAuthor(author);
+ }
+
+ private CTAuthors getCommentsAuthors() {
+ if (comments.getAuthors() == null) {
+ comments.addNewAuthors();
+ }
+ return comments.getAuthors();
+ }
+
+ private int addNewAuthor(String author) {
+ int index = getCommentsAuthors().sizeOfAuthorArray();
+ getCommentsAuthors().insertAuthor(index, author);
+ return index;
+ }
+
+}
--- /dev/null
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.usermodel;
+
+import org.apache.poi.xssf.usermodel.extensions.XSSFSheetComments;
+import org.apache.poi.xssf.util.CellReference;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTAuthors;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments;
+
+import junit.framework.TestCase;
+
+
+public class TestXSSFComment extends TestCase {
+
+ private static final String TEST_AUTHOR = "test_author";
+
+ public void testConstructors() {
+ XSSFSheetComments sheetComments = new XSSFSheetComments();
+ XSSFComment comment = new XSSFComment(sheetComments);
+ assertNotNull(comment);
+
+ CTComment ctComment = CTComment.Factory.newInstance();
+ XSSFComment comment2 = new XSSFComment(sheetComments, ctComment);
+ assertNotNull(comment2);
+ }
+
+ public void testGetColumn() {
+ XSSFSheetComments sheetComments = new XSSFSheetComments();
+ CTComment ctComment = CTComment.Factory.newInstance();
+ ctComment.setRef("A1");
+ XSSFComment comment = new XSSFComment(sheetComments, ctComment);
+ assertNotNull(comment);
+ assertEquals(0, comment.getColumn());
+ ctComment.setRef("C10");
+ assertEquals(2, comment.getColumn());
+ }
+
+ public void testGetRow() {
+ XSSFSheetComments sheetComments = new XSSFSheetComments();
+ CTComment ctComment = CTComment.Factory.newInstance();
+ ctComment.setRef("A1");
+ XSSFComment comment = new XSSFComment(sheetComments, ctComment);
+ assertNotNull(comment);
+ assertEquals(0, comment.getRow());
+ ctComment.setRef("C10");
+ assertEquals(9, comment.getRow());
+ }
+
+ public void testGetAuthor() {
+ CTComments ctComments = CTComments.Factory.newInstance();
+ CTComment ctComment = ctComments.addNewCommentList().addNewComment();
+ CTAuthors ctAuthors = ctComments.addNewAuthors();
+ ctAuthors.insertAuthor(0, TEST_AUTHOR);
+ ctComment.setAuthorId(0);
+
+ XSSFSheetComments sheetComments = new XSSFSheetComments(ctComments);
+ XSSFComment comment = new XSSFComment(sheetComments, ctComment);
+ assertNotNull(comment);
+ assertEquals(TEST_AUTHOR, comment.getAuthor());
+ }
+
+ public void testSetColumn() {
+ XSSFSheetComments sheetComments = new XSSFSheetComments();
+ CTComment ctComment = CTComment.Factory.newInstance();
+ XSSFComment comment = new XSSFComment(sheetComments, ctComment);
+ comment.setColumn((short)3);
+ assertEquals(3, comment.getColumn());
+ assertEquals(3, (new CellReference(ctComment.getRef()).getCol()));
+ assertEquals("D1", ctComment.getRef());
+
+ comment.setColumn((short)13);
+ assertEquals(13, comment.getColumn());
+ }
+
+ public void testSetRow() {
+ XSSFSheetComments sheetComments = new XSSFSheetComments();
+ CTComment ctComment = CTComment.Factory.newInstance();
+ XSSFComment comment = new XSSFComment(sheetComments, ctComment);
+ comment.setRow(20);
+ assertEquals(20, comment.getRow());
+ assertEquals(20, (new CellReference(ctComment.getRef()).getRow()));
+ assertEquals("A21", ctComment.getRef());
+
+ comment.setRow(19);
+ assertEquals(19, comment.getRow());
+ }
+
+ public void testSetAuthor() {
+ XSSFSheetComments sheetComments = new XSSFSheetComments();
+ CTComment ctComment = CTComment.Factory.newInstance();
+ XSSFComment comment = new XSSFComment(sheetComments, ctComment);
+ comment.setAuthor(TEST_AUTHOR);
+ assertEquals(TEST_AUTHOR, comment.getAuthor());
+ }
+
+}
-/* ====================================================================
- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright ownership.
- The ASF licenses this file to You under the Apache License, Version 2.0
- (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-==================================================================== */
-
-package org.apache.poi.xssf.usermodel;
-
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
-
-import junit.framework.TestCase;
-
-
-public class TestXSSFHeaderFooter extends TestCase {
-
- public void testGetSetCenterLeftRight() {
-
- XSSFOddFooter footer = new XSSFOddFooter(CTHeaderFooter.Factory.newInstance());
- assertEquals("", footer.getCenter());
- footer.setCenter("My first center section");
- assertEquals("My first center section", footer.getCenter());
- footer.setCenter("No, let's update the center section");
- assertEquals("No, let's update the center section", footer.getCenter());
- footer.setLeft("And add a left one");
- footer.setRight("Finally the right section is added");
- assertEquals("And add a left one", footer.getLeft());
- assertEquals("Finally the right section is added", footer.getRight());
-
- // Test changing the three sections value
- footer.setCenter("Second center version");
- footer.setLeft("Second left version");
- footer.setRight("Second right version");
- assertEquals("Second center version", footer.getCenter());
- assertEquals("Second left version", footer.getLeft());
- assertEquals("Second right version", footer.getRight());
-
- }
-
-}
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.helpers.ColumnHelper;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDialogsheet;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
public class TestXSSFSheet extends TestCase {
// Set a new default row height in twips and test getting the value in points
sheet.setDefaultRowHeight((short) 360);
assertEquals((float) 18, sheet.getDefaultRowHeightInPoints());
+ // Test that defaultRowHeight is a truncated short: E.G. 360inPoints -> 18; 361inPoints -> 18
+ sheet.setDefaultRowHeight((short) 361);
+ assertEquals((float) 18, sheet.getDefaultRowHeightInPoints());
// Set a new default row height in points and test getting the value in twips
sheet.setDefaultRowHeightInPoints((short) 17);
assertEquals((short) 340, sheet.getDefaultRowHeight());
assertEquals((short) 2, sheet.getTopRow());
assertEquals((short) 26, sheet.getLeftCol());
}
+
+ public void testShiftRows() {
+ XSSFWorkbook workbook = new XSSFWorkbook();
+
+ XSSFSheet sheet = (XSSFSheet) createSheet(workbook, "Sheet 1");
+ sheet.shiftRows(1, 2, 4, true, false);
+ assertEquals((short) 1, sheet.getRow(5).getHeight());
+ assertEquals((short) 2, sheet.getRow(6).getHeight());
+ assertNull(sheet.getRow(1));
+ assertNull(sheet.getRow(2));
+ assertEquals(8, sheet.getPhysicalNumberOfRows());
+
+ XSSFSheet sheet2 = (XSSFSheet) createSheet(workbook, "Sheet 2");
+ sheet2.shiftRows(1, 5, 3, true, false);
+ assertEquals((short) 1, sheet2.getRow(4).getHeight());
+ assertEquals((short) 2, sheet2.getRow(5).getHeight());
+ assertEquals((short) 3, sheet2.getRow(6).getHeight());
+ assertEquals((short) 4, sheet2.getRow(7).getHeight());
+ assertEquals((short) 5, sheet2.getRow(8).getHeight());
+ assertNull(sheet2.getRow(1));
+ assertNull(sheet2.getRow(2));
+ assertNull(sheet2.getRow(3));
+ assertEquals(7, sheet2.getPhysicalNumberOfRows());
+
+ XSSFSheet sheet3 = (XSSFSheet) createSheet(workbook, "Sheet 3");
+ sheet3.shiftRows(5, 7, -3, true, false);
+ assertEquals(5, sheet3.getRow(2).getHeight());
+ assertEquals(6, sheet3.getRow(3).getHeight());
+ assertEquals(7, sheet3.getRow(4).getHeight());
+ assertNull(sheet3.getRow(5));
+ assertNull(sheet3.getRow(6));
+ assertNull(sheet3.getRow(7));
+ assertEquals(7, sheet3.getPhysicalNumberOfRows());
+
+ XSSFSheet sheet4 = (XSSFSheet) createSheet(workbook, "Sheet 4");
+ sheet4.shiftRows(5, 7, -2, true, false);
+ assertEquals(5, sheet4.getRow(3).getHeight());
+ assertEquals(6, sheet4.getRow(4).getHeight());
+ assertEquals(7, sheet4.getRow(5).getHeight());
+ assertNull(sheet4.getRow(6));
+ assertNull(sheet4.getRow(7));
+ assertEquals(8, sheet4.getPhysicalNumberOfRows());
+
+ // Test without copying rowHeight
+ XSSFSheet sheet5 = (XSSFSheet) createSheet(workbook, "Sheet 5");
+ sheet5.shiftRows(5, 7, -2, false, false);
+ assertEquals(-1, sheet5.getRow(3).getHeight());
+ assertEquals(-1, sheet5.getRow(4).getHeight());
+ assertEquals(-1, sheet5.getRow(5).getHeight());
+ assertNull(sheet5.getRow(6));
+ assertNull(sheet5.getRow(7));
+ assertEquals(8, sheet5.getPhysicalNumberOfRows());
+
+ // Test without copying rowHeight and resetting to default height
+ XSSFSheet sheet6 = (XSSFSheet) createSheet(workbook, "Sheet 6");
+ sheet6.setDefaultRowHeight((short) 200);
+ sheet6.shiftRows(5, 7, -2, false, true);
+ assertEquals(200, sheet6.getRow(3).getHeight());
+ assertEquals(200, sheet6.getRow(4).getHeight());
+ assertEquals(200, sheet6.getRow(5).getHeight());
+ assertNull(sheet6.getRow(6));
+ assertNull(sheet6.getRow(7));
+ assertEquals(8, sheet6.getPhysicalNumberOfRows());
+ }
+
+ private XSSFSheet createSheet(XSSFWorkbook workbook, String name) {
+ XSSFSheet sheet = (XSSFSheet) workbook.createSheet(name);
+ Row row0 = sheet.createRow(0);
+ row0.setHeight((short) 1);
+ Row row1 = sheet.createRow(1);
+ row1.setHeight((short) 1);
+ Row row2 = sheet.createRow(2);
+ row2.setHeight((short) 2);
+ Row row3 = sheet.createRow(3);
+ row3.setHeight((short) 3);
+ Row row4 = sheet.createRow(4);
+ row4.setHeight((short) 4);
+ Row row5 = sheet.createRow(5);
+ row5.setHeight((short) 5);
+ Row row6 = sheet.createRow(6);
+ row6.setHeight((short) 6);
+ Row row7 = sheet.createRow(7);
+ row7.setHeight((short) 7);
+ Row row8 = sheet.createRow(8);
+ row8.setHeight((short) 8);
+ Row row9 = sheet.createRow(9);
+ row9.setHeight((short) 9);
+ return sheet;
+ }
}
--- /dev/null
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License
+==================================================================== */
+
+package org.apache.poi.xssf.usermodel.extensions;
+
+import org.apache.poi.xssf.usermodel.XSSFOddFooter;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
+
+import junit.framework.TestCase;
+
+
+public class TestXSSFHeaderFooter extends TestCase {
+
+ public void testGetSetCenterLeftRight() {
+
+ XSSFOddFooter footer = new XSSFOddFooter(CTHeaderFooter.Factory.newInstance());
+ assertEquals("", footer.getCenter());
+ footer.setCenter("My first center section");
+ assertEquals("My first center section", footer.getCenter());
+ footer.setCenter("No, let's update the center section");
+ assertEquals("No, let's update the center section", footer.getCenter());
+ footer.setLeft("And add a left one");
+ footer.setRight("Finally the right section is added");
+ assertEquals("And add a left one", footer.getLeft());
+ assertEquals("Finally the right section is added", footer.getRight());
+
+ // Test changing the three sections value
+ footer.setCenter("Second center version");
+ footer.setLeft("Second left version");
+ footer.setRight("Second right version");
+ assertEquals("Second center version", footer.getCenter());
+ assertEquals("Second left version", footer.getLeft());
+ assertEquals("Second right version", footer.getRight());
+
+ }
+
+}
--- /dev/null
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.usermodel.extensions;
+
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments;
+
+import junit.framework.TestCase;
+
+
+public class TestXSSFSheetComments extends TestCase {
+
+ private static final String TEST_AUTHOR = "test author";
+
+ public void testfindAuthor() {
+ CTComments comments = CTComments.Factory.newInstance();
+ XSSFSheetComments sheetComments = new XSSFSheetComments(comments);
+
+ assertEquals(0, sheetComments.findAuthor(TEST_AUTHOR));
+ assertEquals(1, sheetComments.findAuthor("another author"));
+ assertEquals(0, sheetComments.findAuthor(TEST_AUTHOR));
+
+ }
+
+}