您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TestColumnHelper.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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.helpers;
  16. import junit.framework.TestCase;
  17. import org.apache.poi.xssf.model.StylesTable;
  18. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  19. import org.apache.poi.xssf.usermodel.XSSFSheet;
  20. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  21. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
  22. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
  23. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
  24. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
  25. /**
  26. * Tests for {@link ColumnHelper}
  27. *
  28. */
  29. public final class TestColumnHelper extends TestCase {
  30. public void testCleanColumns() {
  31. CTWorksheet worksheet = CTWorksheet.Factory.newInstance();
  32. CTCols cols1 = worksheet.addNewCols();
  33. CTCol col1 = cols1.addNewCol();
  34. col1.setMin(1);
  35. col1.setMax(1);
  36. col1.setWidth(88);
  37. col1.setHidden(true);
  38. CTCol col2 = cols1.addNewCol();
  39. col2.setMin(2);
  40. col2.setMax(3);
  41. CTCols cols2 = worksheet.addNewCols();
  42. CTCol col4 = cols2.addNewCol();
  43. col4.setMin(13);
  44. col4.setMax(16384);
  45. // Test cleaning cols
  46. assertEquals(2, worksheet.sizeOfColsArray());
  47. int count = countColumns(worksheet);
  48. assertEquals(16375, count);
  49. // Clean columns and test a clean worksheet
  50. ColumnHelper helper = new ColumnHelper(worksheet);
  51. assertEquals(1, worksheet.sizeOfColsArray());
  52. count = countColumns(worksheet);
  53. assertEquals(16375, count);
  54. // Remember - POI column 0 == OOXML column 1
  55. assertEquals(88.0, helper.getColumn(0, false).getWidth(), 0.0);
  56. assertTrue(helper.getColumn(0, false).getHidden());
  57. assertEquals(0.0, helper.getColumn(1, false).getWidth(), 0.0);
  58. assertFalse(helper.getColumn(1, false).getHidden());
  59. }
  60. public void testSortColumns() {
  61. CTWorksheet worksheet = CTWorksheet.Factory.newInstance();
  62. ColumnHelper helper = new ColumnHelper(worksheet);
  63. CTCols cols1 = CTCols.Factory.newInstance();
  64. CTCol col1 = cols1.addNewCol();
  65. col1.setMin(1);
  66. col1.setMax(1);
  67. col1.setWidth(88);
  68. col1.setHidden(true);
  69. CTCol col2 = cols1.addNewCol();
  70. col2.setMin(2);
  71. col2.setMax(3);
  72. CTCol col3 = cols1.addNewCol();
  73. col3.setMin(13);
  74. col3.setMax(16750);
  75. assertEquals(3, cols1.sizeOfColArray());
  76. CTCol col4 = cols1.addNewCol();
  77. col4.setMin(8);
  78. col4.setMax(11);
  79. assertEquals(4, cols1.sizeOfColArray());
  80. CTCol col5 = cols1.addNewCol();
  81. col5.setMin(4);
  82. col5.setMax(5);
  83. assertEquals(5, cols1.sizeOfColArray());
  84. CTCol col6 = cols1.addNewCol();
  85. col6.setMin(8);
  86. col6.setMax(9);
  87. col6.setHidden(true);
  88. CTCol col7 = cols1.addNewCol();
  89. col7.setMin(6);
  90. col7.setMax(8);
  91. col7.setWidth(17.0);
  92. CTCol col8 = cols1.addNewCol();
  93. col8.setMin(25);
  94. col8.setMax(27);
  95. CTCol col9 = cols1.addNewCol();
  96. col9.setMin(20);
  97. col9.setMax(30);
  98. assertEquals(9, cols1.sizeOfColArray());
  99. assertEquals(20, cols1.getColArray(8).getMin());
  100. assertEquals(30, cols1.getColArray(8).getMax());
  101. helper.sortColumns(cols1);
  102. assertEquals(9, cols1.sizeOfColArray());
  103. assertEquals(25, cols1.getColArray(8).getMin());
  104. assertEquals(27, cols1.getColArray(8).getMax());
  105. }
  106. public void testCloneCol() {
  107. CTWorksheet worksheet = CTWorksheet.Factory.newInstance();
  108. ColumnHelper helper = new ColumnHelper(worksheet);
  109. CTCols cols = CTCols.Factory.newInstance();
  110. CTCol col = CTCol.Factory.newInstance();
  111. col.setMin(2);
  112. col.setMax(8);
  113. col.setHidden(true);
  114. col.setWidth(13.4);
  115. CTCol newCol = helper.cloneCol(cols, col);
  116. assertEquals(2, newCol.getMin());
  117. assertEquals(8, newCol.getMax());
  118. assertTrue(newCol.getHidden());
  119. assertEquals(13.4, newCol.getWidth(), 0.0);
  120. }
  121. public void testAddCleanColIntoCols() {
  122. CTWorksheet worksheet = CTWorksheet.Factory.newInstance();
  123. ColumnHelper helper = new ColumnHelper(worksheet);
  124. CTCols cols1 = CTCols.Factory.newInstance();
  125. CTCol col1 = cols1.addNewCol();
  126. col1.setMin(1);
  127. col1.setMax(1);
  128. col1.setWidth(88);
  129. col1.setHidden(true);
  130. CTCol col2 = cols1.addNewCol();
  131. col2.setMin(2);
  132. col2.setMax(3);
  133. CTCol col3 = cols1.addNewCol();
  134. col3.setMin(13);
  135. col3.setMax(16750);
  136. assertEquals(3, cols1.sizeOfColArray());
  137. CTCol col4 = cols1.addNewCol();
  138. col4.setMin(8);
  139. col4.setMax(9);
  140. assertEquals(4, cols1.sizeOfColArray());
  141. CTCol col5 = CTCol.Factory.newInstance();
  142. col5.setMin(4);
  143. col5.setMax(5);
  144. helper.addCleanColIntoCols(cols1, col5);
  145. assertEquals(5, cols1.sizeOfColArray());
  146. CTCol col6 = CTCol.Factory.newInstance();
  147. col6.setMin(8);
  148. col6.setMax(11);
  149. col6.setHidden(true);
  150. helper.addCleanColIntoCols(cols1, col6);
  151. assertEquals(6, cols1.sizeOfColArray());
  152. CTCol col7 = CTCol.Factory.newInstance();
  153. col7.setMin(6);
  154. col7.setMax(8);
  155. col7.setWidth(17.0);
  156. helper.addCleanColIntoCols(cols1, col7);
  157. assertEquals(8, cols1.sizeOfColArray());
  158. CTCol col8 = CTCol.Factory.newInstance();
  159. col8.setMin(20);
  160. col8.setMax(30);
  161. helper.addCleanColIntoCols(cols1, col8);
  162. assertEquals(10, cols1.sizeOfColArray());
  163. CTCol col9 = CTCol.Factory.newInstance();
  164. col9.setMin(25);
  165. col9.setMax(27);
  166. helper.addCleanColIntoCols(cols1, col9);
  167. // TODO - assert something interesting
  168. CTCol[] colArray = cols1.getColArray();
  169. assertEquals(12, colArray.length);
  170. assertEquals(1, colArray[0].getMin());
  171. assertEquals(16750, colArray[11].getMax());
  172. }
  173. public void testGetColumn() {
  174. CTWorksheet worksheet = CTWorksheet.Factory.newInstance();
  175. CTCols cols1 = worksheet.addNewCols();
  176. CTCol col1 = cols1.addNewCol();
  177. col1.setMin(1);
  178. col1.setMax(1);
  179. col1.setWidth(88);
  180. col1.setHidden(true);
  181. CTCol col2 = cols1.addNewCol();
  182. col2.setMin(2);
  183. col2.setMax(3);
  184. CTCols cols2 = worksheet.addNewCols();
  185. CTCol col4 = cols2.addNewCol();
  186. col4.setMin(3);
  187. col4.setMax(6);
  188. // Remember - POI column 0 == OOXML column 1
  189. ColumnHelper helper = new ColumnHelper(worksheet);
  190. assertNotNull(helper.getColumn(0, false));
  191. assertNotNull(helper.getColumn(1, false));
  192. assertEquals(88.0, helper.getColumn(0, false).getWidth(), 0.0);
  193. assertEquals(0.0, helper.getColumn(1, false).getWidth(), 0.0);
  194. assertTrue(helper.getColumn(0, false).getHidden());
  195. assertFalse(helper.getColumn(1, false).getHidden());
  196. assertNull(helper.getColumn(99, false));
  197. assertNotNull(helper.getColumn(5, false));
  198. }
  199. public void testSetColumnAttributes() {
  200. CTCol col = CTCol.Factory.newInstance();
  201. col.setWidth(12);
  202. col.setHidden(true);
  203. CTCol newCol = CTCol.Factory.newInstance();
  204. assertEquals(0.0, newCol.getWidth(), 0.0);
  205. assertFalse(newCol.getHidden());
  206. ColumnHelper helper = new ColumnHelper(CTWorksheet.Factory
  207. .newInstance());
  208. helper.setColumnAttributes(col, newCol);
  209. assertEquals(12.0, newCol.getWidth(), 0.0);
  210. assertTrue(newCol.getHidden());
  211. }
  212. public void testGetOrCreateColumn() {
  213. XSSFWorkbook workbook = new XSSFWorkbook();
  214. XSSFSheet sheet = workbook.createSheet("Sheet 1");
  215. ColumnHelper columnHelper = sheet.getColumnHelper();
  216. // Check POI 0 based, OOXML 1 based
  217. CTCol col = columnHelper.getOrCreateColumn1Based(3, false);
  218. assertNotNull(col);
  219. assertNull(columnHelper.getColumn(1, false));
  220. assertNotNull(columnHelper.getColumn(2, false));
  221. assertNotNull(columnHelper.getColumn1Based(3, false));
  222. assertNull(columnHelper.getColumn(3, false));
  223. CTCol col2 = columnHelper.getOrCreateColumn1Based(30, false);
  224. assertNotNull(col2);
  225. assertNull(columnHelper.getColumn(28, false));
  226. assertNotNull(columnHelper.getColumn(29, false));
  227. assertNotNull(columnHelper.getColumn1Based(30, false));
  228. assertNull(columnHelper.getColumn(30, false));
  229. }
  230. public void testGetSetColDefaultStyle() {
  231. XSSFWorkbook workbook = new XSSFWorkbook();
  232. XSSFSheet sheet = workbook.createSheet();
  233. CTWorksheet ctWorksheet = sheet.getCTWorksheet();
  234. ColumnHelper columnHelper = sheet.getColumnHelper();
  235. // POI column 3, OOXML column 4
  236. CTCol col = columnHelper.getOrCreateColumn1Based(4, false);
  237. assertNotNull(col);
  238. assertNotNull(columnHelper.getColumn(3, false));
  239. columnHelper.setColDefaultStyle(3, 2);
  240. assertEquals(2, columnHelper.getColDefaultStyle(3));
  241. assertEquals(-1, columnHelper.getColDefaultStyle(4));
  242. StylesTable stylesTable = workbook.getStylesSource();
  243. CTXf cellXf = CTXf.Factory.newInstance();
  244. cellXf.setFontId(0);
  245. cellXf.setFillId(0);
  246. cellXf.setBorderId(0);
  247. cellXf.setNumFmtId(0);
  248. cellXf.setXfId(0);
  249. stylesTable.putCellXf(cellXf);
  250. CTCol col_2 = ctWorksheet.getColsArray(0).addNewCol();
  251. col_2.setMin(10);
  252. col_2.setMax(12);
  253. col_2.setStyle(1);
  254. assertEquals(1, columnHelper.getColDefaultStyle(11));
  255. XSSFCellStyle cellStyle = new XSSFCellStyle(0, 0, stylesTable, null);
  256. columnHelper.setColDefaultStyle(11, cellStyle);
  257. assertEquals(0, col_2.getStyle());
  258. assertEquals(1, columnHelper.getColDefaultStyle(10));
  259. }
  260. private static int countColumns(CTWorksheet worksheet) {
  261. int count;
  262. count = 0;
  263. for (int i = 0; i < worksheet.sizeOfColsArray(); i++) {
  264. for (int y = 0; y < worksheet.getColsArray(i).sizeOfColArray(); y++) {
  265. for (long k = worksheet.getColsArray(i).getColArray(y).getMin(); k <= worksheet
  266. .getColsArray(i).getColArray(y).getMax(); k++) {
  267. count++;
  268. }
  269. }
  270. }
  271. return count;
  272. }
  273. }