You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestXSSFPivotTable.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xssf.usermodel;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertNotNull;
  18. import static org.junit.Assert.fail;
  19. import java.io.IOException;
  20. import org.apache.poi.ss.usermodel.Cell;
  21. import org.apache.poi.ss.usermodel.CellType;
  22. import org.apache.poi.ss.usermodel.DataConsolidateFunction;
  23. import org.apache.poi.ss.usermodel.Row;
  24. import org.apache.poi.ss.util.AreaReference;
  25. import org.apache.poi.ss.util.CellReference;
  26. import org.apache.poi.xssf.XSSFITestDataProvider;
  27. import org.junit.After;
  28. import org.junit.Before;
  29. import org.junit.Test;
  30. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageField;
  31. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageFields;
  32. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotFields;
  33. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotTableDefinition;
  34. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STDataConsolidateFunction;
  35. public class TestXSSFPivotTable {
  36. private static final XSSFITestDataProvider _testDataProvider = XSSFITestDataProvider.instance;
  37. private XSSFWorkbook wb;
  38. private XSSFPivotTable pivotTable;
  39. private XSSFPivotTable offsetPivotTable;
  40. private Cell offsetOuterCell;
  41. @Before
  42. public void setUp(){
  43. wb = new XSSFWorkbook();
  44. XSSFSheet sheet = wb.createSheet();
  45. Row row1 = sheet.createRow(0);
  46. // Create a cell and put a value in it.
  47. Cell cell = row1.createCell(0);
  48. cell.setCellValue("Names");
  49. Cell cell2 = row1.createCell(1);
  50. cell2.setCellValue("#");
  51. Cell cell7 = row1.createCell(2);
  52. cell7.setCellValue("Data");
  53. Cell cell10 = row1.createCell(3);
  54. cell10.setCellValue("Value");
  55. Row row2 = sheet.createRow(1);
  56. Cell cell3 = row2.createCell(0);
  57. cell3.setCellValue("Jan");
  58. Cell cell4 = row2.createCell(1);
  59. cell4.setCellValue(10);
  60. Cell cell8 = row2.createCell(2);
  61. cell8.setCellValue("Apa");
  62. Cell cell11 = row1.createCell(3);
  63. cell11.setCellValue(11.11);
  64. Row row3 = sheet.createRow(2);
  65. Cell cell5 = row3.createCell(0);
  66. cell5.setCellValue("Ben");
  67. Cell cell6 = row3.createCell(1);
  68. cell6.setCellValue(9);
  69. Cell cell9 = row3.createCell(2);
  70. cell9.setCellValue("Bepa");
  71. Cell cell12 = row1.createCell(3);
  72. cell12.setCellValue(12.12);
  73. AreaReference source = new AreaReference("A1:C2", _testDataProvider.getSpreadsheetVersion());
  74. pivotTable = sheet.createPivotTable(source, new CellReference("H5"));
  75. XSSFSheet offsetSheet = wb.createSheet();
  76. Row tableRow_1 = offsetSheet.createRow(1);
  77. offsetOuterCell = tableRow_1.createCell(1);
  78. offsetOuterCell.setCellValue(-1);
  79. Cell tableCell_1_1 = tableRow_1.createCell(2);
  80. tableCell_1_1.setCellValue("Row #");
  81. Cell tableCell_1_2 = tableRow_1.createCell(3);
  82. tableCell_1_2.setCellValue("Exponent");
  83. Cell tableCell_1_3 = tableRow_1.createCell(4);
  84. tableCell_1_3.setCellValue("10^Exponent");
  85. Row tableRow_2 = offsetSheet.createRow(2);
  86. Cell tableCell_2_1 = tableRow_2.createCell(2);
  87. tableCell_2_1.setCellValue(0);
  88. Cell tableCell_2_2 = tableRow_2.createCell(3);
  89. tableCell_2_2.setCellValue(0);
  90. Cell tableCell_2_3 = tableRow_2.createCell(4);
  91. tableCell_2_3.setCellValue(1);
  92. Row tableRow_3= offsetSheet.createRow(3);
  93. Cell tableCell_3_1 = tableRow_3.createCell(2);
  94. tableCell_3_1.setCellValue(1);
  95. Cell tableCell_3_2 = tableRow_3.createCell(3);
  96. tableCell_3_2.setCellValue(1);
  97. Cell tableCell_3_3 = tableRow_3.createCell(4);
  98. tableCell_3_3.setCellValue(10);
  99. Row tableRow_4 = offsetSheet.createRow(4);
  100. Cell tableCell_4_1 = tableRow_4.createCell(2);
  101. tableCell_4_1.setCellValue(2);
  102. Cell tableCell_4_2 = tableRow_4.createCell(3);
  103. tableCell_4_2.setCellValue(2);
  104. Cell tableCell_4_3 = tableRow_4.createCell(4);
  105. tableCell_4_3.setCellValue(100);
  106. AreaReference offsetSource = new AreaReference(new CellReference("C2"), new CellReference("E4"));
  107. offsetPivotTable = offsetSheet.createPivotTable(offsetSource, new CellReference("C6"));
  108. }
  109. @After
  110. public void tearDown() throws IOException {
  111. XSSFWorkbook wb2 = _testDataProvider.writeOutAndReadBack(wb);
  112. wb.close();
  113. wb2.close();
  114. }
  115. /**
  116. * Verify that when creating a row label it's created on the correct row
  117. * and the count is increased by one.
  118. */
  119. @Test
  120. public void testAddRowLabelToPivotTable() {
  121. int columnIndex = 0;
  122. assertEquals(0, pivotTable.getRowLabelColumns().size());
  123. pivotTable.addRowLabel(columnIndex);
  124. CTPivotTableDefinition defintion = pivotTable.getCTPivotTableDefinition();
  125. assertEquals(defintion.getRowFields().getFieldArray(0).getX(), columnIndex);
  126. assertEquals(defintion.getRowFields().getCount(), 1);
  127. assertEquals(1, pivotTable.getRowLabelColumns().size());
  128. columnIndex = 1;
  129. pivotTable.addRowLabel(columnIndex);
  130. assertEquals(2, pivotTable.getRowLabelColumns().size());
  131. assertEquals(0, (int)pivotTable.getRowLabelColumns().get(0));
  132. assertEquals(1, (int)pivotTable.getRowLabelColumns().get(1));
  133. }
  134. /**
  135. * Verify that it's not possible to create a row label outside of the referenced area.
  136. */
  137. @Test
  138. public void testAddRowLabelOutOfRangeThrowsException() {
  139. int columnIndex = 5;
  140. try {
  141. pivotTable.addRowLabel(columnIndex);
  142. } catch(IndexOutOfBoundsException e) {
  143. return;
  144. }
  145. fail();
  146. }
  147. /**
  148. * Verify that when creating one column label, no col fields are being created.
  149. */
  150. @Test
  151. public void testAddOneColumnLabelToPivotTableDoesNotCreateColField() {
  152. int columnIndex = 0;
  153. pivotTable.addColumnLabel(DataConsolidateFunction.SUM, columnIndex);
  154. CTPivotTableDefinition defintion = pivotTable.getCTPivotTableDefinition();
  155. assertEquals(defintion.getColFields(), null);
  156. }
  157. /**
  158. * Verify that it's possible to create three column labels with different DataConsolidateFunction
  159. */
  160. @Test
  161. public void testAddThreeDifferentColumnLabelsToPivotTable() {
  162. int columnOne = 0;
  163. int columnTwo = 1;
  164. int columnThree = 2;
  165. pivotTable.addColumnLabel(DataConsolidateFunction.SUM, columnOne);
  166. pivotTable.addColumnLabel(DataConsolidateFunction.MAX, columnTwo);
  167. pivotTable.addColumnLabel(DataConsolidateFunction.MIN, columnThree);
  168. CTPivotTableDefinition defintion = pivotTable.getCTPivotTableDefinition();
  169. assertEquals(defintion.getDataFields().getDataFieldList().size(), 3);
  170. }
  171. /**
  172. * Verify that it's possible to create three column labels with the same DataConsolidateFunction
  173. */
  174. @Test
  175. public void testAddThreeSametColumnLabelsToPivotTable() {
  176. int columnOne = 0;
  177. int columnTwo = 1;
  178. int columnThree = 2;
  179. pivotTable.addColumnLabel(DataConsolidateFunction.SUM, columnOne);
  180. pivotTable.addColumnLabel(DataConsolidateFunction.SUM, columnTwo);
  181. pivotTable.addColumnLabel(DataConsolidateFunction.SUM, columnThree);
  182. CTPivotTableDefinition defintion = pivotTable.getCTPivotTableDefinition();
  183. assertEquals(defintion.getDataFields().getDataFieldList().size(), 3);
  184. }
  185. /**
  186. * Verify that when creating two column labels, a col field is being created and X is set to -2.
  187. */
  188. @Test
  189. public void testAddTwoColumnLabelsToPivotTable() {
  190. int columnOne = 0;
  191. int columnTwo = 1;
  192. pivotTable.addColumnLabel(DataConsolidateFunction.SUM, columnOne);
  193. pivotTable.addColumnLabel(DataConsolidateFunction.SUM, columnTwo);
  194. CTPivotTableDefinition defintion = pivotTable.getCTPivotTableDefinition();
  195. assertEquals(defintion.getColFields().getFieldArray(0).getX(), -2);
  196. }
  197. /**
  198. * Verify that a data field is created when creating a data column
  199. */
  200. @Test
  201. public void testColumnLabelCreatesDataField() {
  202. int columnIndex = 0;
  203. pivotTable.addColumnLabel(DataConsolidateFunction.SUM, columnIndex);
  204. CTPivotTableDefinition defintion = pivotTable.getCTPivotTableDefinition();
  205. assertEquals(defintion.getDataFields().getDataFieldArray(0).getFld(), columnIndex);
  206. assertEquals(defintion.getDataFields().getDataFieldArray(0).getSubtotal(),
  207. STDataConsolidateFunction.Enum.forInt(DataConsolidateFunction.SUM.getValue()));
  208. }
  209. /**
  210. * Verify that it's possible to set a custom name when creating a data column
  211. */
  212. @Test
  213. public void testColumnLabelSetCustomName() {
  214. int columnIndex = 0;
  215. String customName = "Custom Name";
  216. pivotTable.addColumnLabel(DataConsolidateFunction.SUM, columnIndex, customName);
  217. CTPivotTableDefinition defintion = pivotTable.getCTPivotTableDefinition();
  218. assertEquals(defintion.getDataFields().getDataFieldArray(0).getFld(), columnIndex);
  219. assertEquals(defintion.getDataFields().getDataFieldArray(0).getName(), customName);
  220. }
  221. /**
  222. * Verify that it's not possible to create a column label outside of the referenced area.
  223. */
  224. @Test
  225. public void testAddColumnLabelOutOfRangeThrowsException() {
  226. int columnIndex = 5;
  227. try {
  228. pivotTable.addColumnLabel(DataConsolidateFunction.SUM, columnIndex);
  229. } catch(IndexOutOfBoundsException e) {
  230. return;
  231. }
  232. fail();
  233. }
  234. /**
  235. * Verify when creating a data column set to a data field, the data field with the corresponding
  236. * column index will be set to true.
  237. */
  238. @Test
  239. public void testAddDataColumn() {
  240. int columnIndex = 0;
  241. boolean isDataField = true;
  242. pivotTable.addDataColumn(columnIndex, isDataField);
  243. CTPivotFields pivotFields = pivotTable.getCTPivotTableDefinition().getPivotFields();
  244. assertEquals(pivotFields.getPivotFieldArray(columnIndex).getDataField(), isDataField);
  245. }
  246. /**
  247. * Verify that it's not possible to create a data column outside of the referenced area.
  248. */
  249. @Test
  250. public void testAddDataColumnOutOfRangeThrowsException() {
  251. int columnIndex = 5;
  252. boolean isDataField = true;
  253. try {
  254. pivotTable.addDataColumn(columnIndex, isDataField);
  255. } catch(IndexOutOfBoundsException e) {
  256. return;
  257. }
  258. fail();
  259. }
  260. /**
  261. * Verify that it's possible to create a new filter
  262. */
  263. public void testAddReportFilter() {
  264. int columnIndex = 0;
  265. pivotTable.addReportFilter(columnIndex);
  266. CTPageFields fields = pivotTable.getCTPivotTableDefinition().getPageFields();
  267. CTPageField field = fields.getPageFieldArray(0);
  268. assertEquals(field.getFld(), columnIndex);
  269. assertEquals(field.getHier(), -1);
  270. assertEquals(fields.getCount(), 1);
  271. }
  272. /**
  273. * Verify that it's not possible to create a new filter outside of the referenced area.
  274. */
  275. @Test
  276. public void testAddReportFilterOutOfRangeThrowsException() {
  277. int columnIndex = 5;
  278. try {
  279. pivotTable.addReportFilter(columnIndex);
  280. } catch(IndexOutOfBoundsException e) {
  281. return;
  282. }
  283. fail();
  284. }
  285. /**
  286. * Verify that the Pivot Table operates only within the referenced area, even when the
  287. * first column of the referenced area is not index 0.
  288. */
  289. @Test
  290. public void testAddDataColumnWithOffsetData() {
  291. offsetPivotTable.addColumnLabel(DataConsolidateFunction.SUM, 1);
  292. assertEquals(CellType.NUMERIC, offsetOuterCell.getCellTypeEnum());
  293. offsetPivotTable.addColumnLabel(DataConsolidateFunction.SUM, 0);
  294. }
  295. @Test
  296. public void testPivotTableSheetNamesAreCaseInsensitive() {
  297. wb.setSheetName(0, "original");
  298. wb.setSheetName(1, "offset");
  299. XSSFSheet original = wb.getSheet("OriginaL");
  300. XSSFSheet offset = wb.getSheet("OffseT");
  301. // assume sheets are accessible via case-insensitive name
  302. assertNotNull(original);
  303. assertNotNull(offset);
  304. AreaReference source = new AreaReference("ORIGinal!A1:C2", _testDataProvider.getSpreadsheetVersion());
  305. // create a pivot table on the same sheet, case insensitive
  306. original.createPivotTable(source, new CellReference("W1"));
  307. // create a pivot table on a different sheet, case insensitive
  308. offset.createPivotTable(source, new CellReference("W1"));
  309. }
  310. }