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.

BaseTestConditionalFormatting.java 58KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.ss.usermodel;
  20. import static org.junit.Assert.assertEquals;
  21. import static org.junit.Assert.assertFalse;
  22. import static org.junit.Assert.assertNotNull;
  23. import static org.junit.Assert.assertNull;
  24. import static org.junit.Assert.assertTrue;
  25. import static org.junit.Assert.fail;
  26. import java.io.IOException;
  27. import org.apache.poi.hssf.usermodel.HSSFConditionalFormatting;
  28. import org.apache.poi.hssf.usermodel.HSSFConditionalFormattingRule;
  29. import org.apache.poi.ss.ITestDataProvider;
  30. import org.apache.poi.ss.usermodel.ConditionalFormattingThreshold.RangeType;
  31. import org.apache.poi.ss.usermodel.IconMultiStateFormatting.IconSet;
  32. import org.apache.poi.ss.util.CellRangeAddress;
  33. import org.junit.Test;
  34. /**
  35. * Base tests for Conditional Formatting, for both HSSF and XSSF
  36. */
  37. public abstract class BaseTestConditionalFormatting {
  38. private final ITestDataProvider _testDataProvider;
  39. public BaseTestConditionalFormatting(ITestDataProvider testDataProvider){
  40. _testDataProvider = testDataProvider;
  41. }
  42. protected abstract void assertColour(String hexExpected, Color actual);
  43. @Test
  44. public void testBasic() throws IOException {
  45. Workbook wb = _testDataProvider.createWorkbook();
  46. Sheet sh = wb.createSheet();
  47. SheetConditionalFormatting sheetCF = sh.getSheetConditionalFormatting();
  48. assertEquals(0, sheetCF.getNumConditionalFormattings());
  49. try {
  50. assertNull(sheetCF.getConditionalFormattingAt(0));
  51. fail("expected exception");
  52. } catch (IllegalArgumentException e) {
  53. assertTrue(e.getMessage().startsWith("Specified CF index 0 is outside the allowable range"));
  54. }
  55. try {
  56. sheetCF.removeConditionalFormatting(0);
  57. fail("expected exception");
  58. } catch (IllegalArgumentException e) {
  59. assertTrue(e.getMessage().startsWith("Specified CF index 0 is outside the allowable range"));
  60. }
  61. ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule("1");
  62. ConditionalFormattingRule rule2 = sheetCF.createConditionalFormattingRule("2");
  63. ConditionalFormattingRule rule3 = sheetCF.createConditionalFormattingRule("3");
  64. ConditionalFormattingRule rule4 = sheetCF.createConditionalFormattingRule("4");
  65. try {
  66. sheetCF.addConditionalFormatting(null, rule1);
  67. fail("expected exception");
  68. } catch (IllegalArgumentException e) {
  69. assertTrue(e.getMessage().startsWith("regions must not be null"));
  70. }
  71. try {
  72. sheetCF.addConditionalFormatting(
  73. new CellRangeAddress[]{ CellRangeAddress.valueOf("A1:A3") },
  74. (ConditionalFormattingRule)null);
  75. fail("expected exception");
  76. } catch (IllegalArgumentException e) {
  77. assertTrue(e.getMessage().startsWith("cfRules must not be null"));
  78. }
  79. try {
  80. sheetCF.addConditionalFormatting(
  81. new CellRangeAddress[]{ CellRangeAddress.valueOf("A1:A3") },
  82. new ConditionalFormattingRule[0]);
  83. fail("expected exception");
  84. } catch (IllegalArgumentException e) {
  85. assertTrue(e.getMessage().startsWith("cfRules must not be empty"));
  86. }
  87. try {
  88. sheetCF.addConditionalFormatting(
  89. new CellRangeAddress[]{ CellRangeAddress.valueOf("A1:A3") },
  90. new ConditionalFormattingRule[]{rule1, rule2, rule3, rule4});
  91. fail("expected exception");
  92. } catch (IllegalArgumentException e) {
  93. assertTrue(e.getMessage().startsWith("Number of rules must not exceed 3"));
  94. }
  95. wb.close();
  96. }
  97. /**
  98. * Test format conditions based on a boolean formula
  99. */
  100. @Test
  101. @SuppressWarnings("deprecation")
  102. public void testBooleanFormulaConditions() throws IOException {
  103. Workbook wb = _testDataProvider.createWorkbook();
  104. Sheet sh = wb.createSheet();
  105. SheetConditionalFormatting sheetCF = sh.getSheetConditionalFormatting();
  106. ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule("SUM(A1:A5)>10");
  107. assertEquals(ConditionType.FORMULA.id, rule1.getConditionType());
  108. assertEquals(ConditionType.FORMULA, rule1.getConditionTypeType());
  109. assertEquals("SUM(A1:A5)>10", rule1.getFormula1());
  110. int formatIndex1 = sheetCF.addConditionalFormatting(
  111. new CellRangeAddress[]{
  112. CellRangeAddress.valueOf("B1"),
  113. CellRangeAddress.valueOf("C3"),
  114. }, rule1);
  115. assertEquals(0, formatIndex1);
  116. assertEquals(1, sheetCF.getNumConditionalFormattings());
  117. CellRangeAddress[] ranges1 = sheetCF.getConditionalFormattingAt(formatIndex1).getFormattingRanges();
  118. assertEquals(2, ranges1.length);
  119. assertEquals("B1", ranges1[0].formatAsString());
  120. assertEquals("C3", ranges1[1].formatAsString());
  121. // adjacent address are merged
  122. int formatIndex2 = sheetCF.addConditionalFormatting(
  123. new CellRangeAddress[]{
  124. CellRangeAddress.valueOf("B1"),
  125. CellRangeAddress.valueOf("B2"),
  126. CellRangeAddress.valueOf("B3"),
  127. }, rule1);
  128. assertEquals(1, formatIndex2);
  129. assertEquals(2, sheetCF.getNumConditionalFormattings());
  130. CellRangeAddress[] ranges2 = sheetCF.getConditionalFormattingAt(formatIndex2).getFormattingRanges();
  131. assertEquals(1, ranges2.length);
  132. assertEquals("B1:B3", ranges2[0].formatAsString());
  133. wb.close();
  134. }
  135. @Test
  136. @SuppressWarnings("deprecation")
  137. public void testSingleFormulaConditions() throws IOException {
  138. Workbook wb = _testDataProvider.createWorkbook();
  139. Sheet sh = wb.createSheet();
  140. SheetConditionalFormatting sheetCF = sh.getSheetConditionalFormatting();
  141. ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(
  142. ComparisonOperator.EQUAL, "SUM(A1:A5)+10");
  143. assertEquals(ConditionType.CELL_VALUE_IS.id, rule1.getConditionType());
  144. assertEquals(ConditionType.CELL_VALUE_IS, rule1.getConditionTypeType());
  145. assertEquals("SUM(A1:A5)+10", rule1.getFormula1());
  146. assertEquals(ComparisonOperator.EQUAL, rule1.getComparisonOperation());
  147. ConditionalFormattingRule rule2 = sheetCF.createConditionalFormattingRule(
  148. ComparisonOperator.NOT_EQUAL, "15");
  149. assertEquals(ConditionType.CELL_VALUE_IS.id, rule2.getConditionType());
  150. assertEquals(ConditionType.CELL_VALUE_IS, rule2.getConditionTypeType());
  151. assertEquals("15", rule2.getFormula1());
  152. assertEquals(ComparisonOperator.NOT_EQUAL, rule2.getComparisonOperation());
  153. ConditionalFormattingRule rule3 = sheetCF.createConditionalFormattingRule(
  154. ComparisonOperator.NOT_EQUAL, "15");
  155. assertEquals(ConditionType.CELL_VALUE_IS.id, rule3.getConditionType());
  156. assertEquals(ConditionType.CELL_VALUE_IS, rule3.getConditionTypeType());
  157. assertEquals("15", rule3.getFormula1());
  158. assertEquals(ComparisonOperator.NOT_EQUAL, rule3.getComparisonOperation());
  159. ConditionalFormattingRule rule4 = sheetCF.createConditionalFormattingRule(
  160. ComparisonOperator.GT, "0");
  161. assertEquals(ConditionType.CELL_VALUE_IS.id, rule4.getConditionType());
  162. assertEquals(ConditionType.CELL_VALUE_IS, rule4.getConditionTypeType());
  163. assertEquals("0", rule4.getFormula1());
  164. assertEquals(ComparisonOperator.GT, rule4.getComparisonOperation());
  165. ConditionalFormattingRule rule5 = sheetCF.createConditionalFormattingRule(
  166. ComparisonOperator.LT, "0");
  167. assertEquals(ConditionType.CELL_VALUE_IS.id, rule5.getConditionType());
  168. assertEquals(ConditionType.CELL_VALUE_IS, rule5.getConditionTypeType());
  169. assertEquals("0", rule5.getFormula1());
  170. assertEquals(ComparisonOperator.LT, rule5.getComparisonOperation());
  171. ConditionalFormattingRule rule6 = sheetCF.createConditionalFormattingRule(
  172. ComparisonOperator.GE, "0");
  173. assertEquals(ConditionType.CELL_VALUE_IS.id, rule6.getConditionType());
  174. assertEquals(ConditionType.CELL_VALUE_IS, rule6.getConditionTypeType());
  175. assertEquals("0", rule6.getFormula1());
  176. assertEquals(ComparisonOperator.GE, rule6.getComparisonOperation());
  177. ConditionalFormattingRule rule7 = sheetCF.createConditionalFormattingRule(
  178. ComparisonOperator.LE, "0");
  179. assertEquals(ConditionType.CELL_VALUE_IS.id, rule7.getConditionType());
  180. assertEquals(ConditionType.CELL_VALUE_IS, rule7.getConditionTypeType());
  181. assertEquals("0", rule7.getFormula1());
  182. assertEquals(ComparisonOperator.LE, rule7.getComparisonOperation());
  183. ConditionalFormattingRule rule8 = sheetCF.createConditionalFormattingRule(
  184. ComparisonOperator.BETWEEN, "0", "5");
  185. assertEquals(ConditionType.CELL_VALUE_IS.id, rule8.getConditionType());
  186. assertEquals(ConditionType.CELL_VALUE_IS, rule8.getConditionTypeType());
  187. assertEquals("0", rule8.getFormula1());
  188. assertEquals("5", rule8.getFormula2());
  189. assertEquals(ComparisonOperator.BETWEEN, rule8.getComparisonOperation());
  190. ConditionalFormattingRule rule9 = sheetCF.createConditionalFormattingRule(
  191. ComparisonOperator.NOT_BETWEEN, "0", "5");
  192. assertEquals(ConditionType.CELL_VALUE_IS.id, rule9.getConditionType());
  193. assertEquals(ConditionType.CELL_VALUE_IS, rule9.getConditionTypeType());
  194. assertEquals("0", rule9.getFormula1());
  195. assertEquals("5", rule9.getFormula2());
  196. assertEquals(ComparisonOperator.NOT_BETWEEN, rule9.getComparisonOperation());
  197. wb.close();
  198. }
  199. @Test
  200. @SuppressWarnings("deprecation")
  201. public void testCopy() throws IOException {
  202. Workbook wb = _testDataProvider.createWorkbook();
  203. Sheet sheet1 = wb.createSheet();
  204. Sheet sheet2 = wb.createSheet();
  205. SheetConditionalFormatting sheet1CF = sheet1.getSheetConditionalFormatting();
  206. SheetConditionalFormatting sheet2CF = sheet2.getSheetConditionalFormatting();
  207. assertEquals(0, sheet1CF.getNumConditionalFormattings());
  208. assertEquals(0, sheet2CF.getNumConditionalFormattings());
  209. ConditionalFormattingRule rule1 = sheet1CF.createConditionalFormattingRule(
  210. ComparisonOperator.EQUAL, "SUM(A1:A5)+10");
  211. ConditionalFormattingRule rule2 = sheet1CF.createConditionalFormattingRule(
  212. ComparisonOperator.NOT_EQUAL, "15");
  213. // adjacent address are merged
  214. int formatIndex = sheet1CF.addConditionalFormatting(
  215. new CellRangeAddress[]{
  216. CellRangeAddress.valueOf("A1:A5"),
  217. CellRangeAddress.valueOf("C1:C5")
  218. }, rule1, rule2);
  219. assertEquals(0, formatIndex);
  220. assertEquals(1, sheet1CF.getNumConditionalFormattings());
  221. assertEquals(0, sheet2CF.getNumConditionalFormattings());
  222. sheet2CF.addConditionalFormatting(sheet1CF.getConditionalFormattingAt(formatIndex));
  223. assertEquals(1, sheet2CF.getNumConditionalFormattings());
  224. ConditionalFormatting sheet2cf = sheet2CF.getConditionalFormattingAt(0);
  225. assertEquals(2, sheet2cf.getNumberOfRules());
  226. assertEquals("SUM(A1:A5)+10", sheet2cf.getRule(0).getFormula1());
  227. assertEquals(ComparisonOperator.EQUAL, sheet2cf.getRule(0).getComparisonOperation());
  228. assertEquals(ConditionalFormattingRule.CONDITION_TYPE_CELL_VALUE_IS, sheet2cf.getRule(0).getConditionType());
  229. assertEquals("15", sheet2cf.getRule(1).getFormula1());
  230. assertEquals(ComparisonOperator.NOT_EQUAL, sheet2cf.getRule(1).getComparisonOperation());
  231. assertEquals(ConditionalFormattingRule.CONDITION_TYPE_CELL_VALUE_IS, sheet2cf.getRule(1).getConditionType());
  232. wb.close();
  233. }
  234. @Test
  235. public void testRemove() throws IOException {
  236. Workbook wb = _testDataProvider.createWorkbook();
  237. Sheet sheet1 = wb.createSheet();
  238. SheetConditionalFormatting sheetCF = sheet1.getSheetConditionalFormatting();
  239. assertEquals(0, sheetCF.getNumConditionalFormattings());
  240. ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(
  241. ComparisonOperator.EQUAL, "SUM(A1:A5)");
  242. // adjacent address are merged
  243. int formatIndex = sheetCF.addConditionalFormatting(
  244. new CellRangeAddress[]{
  245. CellRangeAddress.valueOf("A1:A5")
  246. }, rule1);
  247. assertEquals(0, formatIndex);
  248. assertEquals(1, sheetCF.getNumConditionalFormattings());
  249. sheetCF.removeConditionalFormatting(0);
  250. assertEquals(0, sheetCF.getNumConditionalFormattings());
  251. try {
  252. assertNull(sheetCF.getConditionalFormattingAt(0));
  253. fail("expected exception");
  254. } catch (IllegalArgumentException e) {
  255. assertTrue(e.getMessage().startsWith("Specified CF index 0 is outside the allowable range"));
  256. }
  257. formatIndex = sheetCF.addConditionalFormatting(
  258. new CellRangeAddress[]{
  259. CellRangeAddress.valueOf("A1:A5")
  260. }, rule1);
  261. assertEquals(0, formatIndex);
  262. assertEquals(1, sheetCF.getNumConditionalFormattings());
  263. sheetCF.removeConditionalFormatting(0);
  264. assertEquals(0, sheetCF.getNumConditionalFormattings());
  265. try {
  266. assertNull(sheetCF.getConditionalFormattingAt(0));
  267. fail("expected exception");
  268. } catch (IllegalArgumentException e) {
  269. assertTrue(e.getMessage().startsWith("Specified CF index 0 is outside the allowable range"));
  270. }
  271. wb.close();
  272. }
  273. @Test
  274. public void testCreateCF() throws IOException {
  275. Workbook workbook = _testDataProvider.createWorkbook();
  276. Sheet sheet = workbook.createSheet();
  277. String formula = "7";
  278. SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
  279. ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(formula);
  280. FontFormatting fontFmt = rule1.createFontFormatting();
  281. fontFmt.setFontStyle(true, false);
  282. BorderFormatting bordFmt = rule1.createBorderFormatting();
  283. bordFmt.setBorderBottom(BorderStyle.THIN);
  284. bordFmt.setBorderTop(BorderStyle.THICK);
  285. bordFmt.setBorderLeft(BorderStyle.DASHED);
  286. bordFmt.setBorderRight(BorderStyle.DOTTED);
  287. PatternFormatting patternFmt = rule1.createPatternFormatting();
  288. patternFmt.setFillBackgroundColor(IndexedColors.YELLOW.index);
  289. ConditionalFormattingRule rule2 = sheetCF.createConditionalFormattingRule(ComparisonOperator.BETWEEN, "1", "2");
  290. ConditionalFormattingRule [] cfRules =
  291. {
  292. rule1, rule2
  293. };
  294. short col = 1;
  295. CellRangeAddress [] regions = {
  296. new CellRangeAddress(0, 65535, col, col)
  297. };
  298. sheetCF.addConditionalFormatting(regions, cfRules);
  299. sheetCF.addConditionalFormatting(regions, cfRules);
  300. // Verification
  301. assertEquals(2, sheetCF.getNumConditionalFormattings());
  302. sheetCF.removeConditionalFormatting(1);
  303. assertEquals(1, sheetCF.getNumConditionalFormattings());
  304. ConditionalFormatting cf = sheetCF.getConditionalFormattingAt(0);
  305. assertNotNull(cf);
  306. regions = cf.getFormattingRanges();
  307. assertNotNull(regions);
  308. assertEquals(1, regions.length);
  309. CellRangeAddress r = regions[0];
  310. assertEquals(1, r.getFirstColumn());
  311. assertEquals(1, r.getLastColumn());
  312. assertEquals(0, r.getFirstRow());
  313. assertEquals(65535, r.getLastRow());
  314. assertEquals(2, cf.getNumberOfRules());
  315. rule1 = cf.getRule(0);
  316. assertEquals("7",rule1.getFormula1());
  317. assertNull(rule1.getFormula2());
  318. FontFormatting r1fp = rule1.getFontFormatting();
  319. assertNotNull(r1fp);
  320. assertTrue(r1fp.isItalic());
  321. assertFalse(r1fp.isBold());
  322. BorderFormatting r1bf = rule1.getBorderFormatting();
  323. assertNotNull(r1bf);
  324. assertEquals(BorderStyle.THIN, r1bf.getBorderBottom());
  325. assertEquals(BorderStyle.THICK,r1bf.getBorderTop());
  326. assertEquals(BorderStyle.DASHED,r1bf.getBorderLeft());
  327. assertEquals(BorderStyle.DOTTED,r1bf.getBorderRight());
  328. PatternFormatting r1pf = rule1.getPatternFormatting();
  329. assertNotNull(r1pf);
  330. // assertEquals(IndexedColors.YELLOW.index,r1pf.getFillBackgroundColor());
  331. rule2 = cf.getRule(1);
  332. assertEquals("2",rule2.getFormula2());
  333. assertEquals("1",rule2.getFormula1());
  334. workbook.close();
  335. }
  336. @Test
  337. public void testClone() throws IOException {
  338. Workbook wb = _testDataProvider.createWorkbook();
  339. Sheet sheet = wb.createSheet();
  340. String formula = "7";
  341. SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
  342. ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(formula);
  343. FontFormatting fontFmt = rule1.createFontFormatting();
  344. fontFmt.setFontStyle(true, false);
  345. PatternFormatting patternFmt = rule1.createPatternFormatting();
  346. patternFmt.setFillBackgroundColor(IndexedColors.YELLOW.index);
  347. ConditionalFormattingRule rule2 = sheetCF.createConditionalFormattingRule(ComparisonOperator.BETWEEN, "1", "2");
  348. ConditionalFormattingRule [] cfRules =
  349. {
  350. rule1, rule2
  351. };
  352. short col = 1;
  353. CellRangeAddress [] regions = {
  354. new CellRangeAddress(0, 65535, col, col)
  355. };
  356. sheetCF.addConditionalFormatting(regions, cfRules);
  357. try {
  358. wb.cloneSheet(0);
  359. assertEquals(2, wb.getNumberOfSheets());
  360. } catch (RuntimeException e) {
  361. if (e.getMessage().indexOf("needs to define a clone method") > 0) {
  362. fail("Indentified bug 45682");
  363. }
  364. throw e;
  365. } finally {
  366. wb.close();
  367. }
  368. }
  369. @Test
  370. public void testShiftRows() throws IOException {
  371. Workbook wb = _testDataProvider.createWorkbook();
  372. Sheet sheet = wb.createSheet();
  373. SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
  374. ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(
  375. ComparisonOperator.BETWEEN, "SUM(A10:A15)", "1+SUM(B16:B30)");
  376. FontFormatting fontFmt = rule1.createFontFormatting();
  377. fontFmt.setFontStyle(true, false);
  378. PatternFormatting patternFmt = rule1.createPatternFormatting();
  379. patternFmt.setFillBackgroundColor(IndexedColors.YELLOW.index);
  380. ConditionalFormattingRule rule2 = sheetCF.createConditionalFormattingRule(
  381. ComparisonOperator.BETWEEN, "SUM(A10:A15)", "1+SUM(B16:B30)");
  382. BorderFormatting borderFmt = rule2.createBorderFormatting();
  383. borderFmt.setBorderDiagonal(BorderStyle.MEDIUM);
  384. CellRangeAddress [] regions = {
  385. new CellRangeAddress(2, 4, 0, 0), // A3:A5
  386. };
  387. sheetCF.addConditionalFormatting(regions, rule1);
  388. sheetCF.addConditionalFormatting(regions, rule2);
  389. // This row-shift should destroy the CF region
  390. sheet.shiftRows(10, 20, -9);
  391. assertEquals(0, sheetCF.getNumConditionalFormattings());
  392. // re-add the CF
  393. sheetCF.addConditionalFormatting(regions, rule1);
  394. sheetCF.addConditionalFormatting(regions, rule2);
  395. // This row shift should only affect the formulas
  396. sheet.shiftRows(14, 17, 8);
  397. ConditionalFormatting cf1 = sheetCF.getConditionalFormattingAt(0);
  398. assertEquals("SUM(A10:A23)", cf1.getRule(0).getFormula1());
  399. assertEquals("1+SUM(B24:B30)", cf1.getRule(0).getFormula2());
  400. ConditionalFormatting cf2 = sheetCF.getConditionalFormattingAt(1);
  401. assertEquals("SUM(A10:A23)", cf2.getRule(0).getFormula1());
  402. assertEquals("1+SUM(B24:B30)", cf2.getRule(0).getFormula2());
  403. sheet.shiftRows(0, 8, 21);
  404. cf1 = sheetCF.getConditionalFormattingAt(0);
  405. assertEquals("SUM(A10:A21)", cf1.getRule(0).getFormula1());
  406. assertEquals("1+SUM(#REF!)", cf1.getRule(0).getFormula2());
  407. cf2 = sheetCF.getConditionalFormattingAt(1);
  408. assertEquals("SUM(A10:A21)", cf2.getRule(0).getFormula1());
  409. assertEquals("1+SUM(#REF!)", cf2.getRule(0).getFormula2());
  410. wb.close();
  411. }
  412. protected void testRead(String filename) throws IOException {
  413. Workbook wb = _testDataProvider.openSampleWorkbook(filename);
  414. Sheet sh = wb.getSheet("CF");
  415. SheetConditionalFormatting sheetCF = sh.getSheetConditionalFormatting();
  416. assertEquals(3, sheetCF.getNumConditionalFormattings());
  417. ConditionalFormatting cf1 = sheetCF.getConditionalFormattingAt(0);
  418. assertEquals(2, cf1.getNumberOfRules());
  419. CellRangeAddress[] regions1 = cf1.getFormattingRanges();
  420. assertEquals(1, regions1.length);
  421. assertEquals("A1:A8", regions1[0].formatAsString());
  422. // CF1 has two rules: values less than -3 are bold-italic red, values greater than 3 are green
  423. ConditionalFormattingRule rule1 = cf1.getRule(0);
  424. assertEquals(ConditionType.CELL_VALUE_IS, rule1.getConditionTypeType());
  425. assertEquals(ComparisonOperator.GT, rule1.getComparisonOperation());
  426. assertEquals("3", rule1.getFormula1());
  427. assertNull(rule1.getFormula2());
  428. // fills and borders are not set
  429. assertNull(rule1.getPatternFormatting());
  430. assertNull(rule1.getBorderFormatting());
  431. FontFormatting fmt1 = rule1.getFontFormatting();
  432. // assertEquals(IndexedColors.GREEN.index, fmt1.getFontColorIndex());
  433. assertTrue(fmt1.isBold());
  434. assertFalse(fmt1.isItalic());
  435. ConditionalFormattingRule rule2 = cf1.getRule(1);
  436. assertEquals(ConditionType.CELL_VALUE_IS, rule2.getConditionTypeType());
  437. assertEquals(ComparisonOperator.LT, rule2.getComparisonOperation());
  438. assertEquals("-3", rule2.getFormula1());
  439. assertNull(rule2.getFormula2());
  440. assertNull(rule2.getPatternFormatting());
  441. assertNull(rule2.getBorderFormatting());
  442. FontFormatting fmt2 = rule2.getFontFormatting();
  443. // assertEquals(IndexedColors.RED.index, fmt2.getFontColorIndex());
  444. assertTrue(fmt2.isBold());
  445. assertTrue(fmt2.isItalic());
  446. ConditionalFormatting cf2 = sheetCF.getConditionalFormattingAt(1);
  447. assertEquals(1, cf2.getNumberOfRules());
  448. CellRangeAddress[] regions2 = cf2.getFormattingRanges();
  449. assertEquals(1, regions2.length);
  450. assertEquals("B9", regions2[0].formatAsString());
  451. ConditionalFormattingRule rule3 = cf2.getRule(0);
  452. assertEquals(ConditionType.FORMULA, rule3.getConditionTypeType());
  453. assertEquals(ComparisonOperator.NO_COMPARISON, rule3.getComparisonOperation());
  454. assertEquals("$A$8>5", rule3.getFormula1());
  455. assertNull(rule3.getFormula2());
  456. FontFormatting fmt3 = rule3.getFontFormatting();
  457. // assertEquals(IndexedColors.RED.index, fmt3.getFontColorIndex());
  458. assertTrue(fmt3.isBold());
  459. assertTrue(fmt3.isItalic());
  460. PatternFormatting fmt4 = rule3.getPatternFormatting();
  461. // assertEquals(IndexedColors.LIGHT_CORNFLOWER_BLUE.index, fmt4.getFillBackgroundColor());
  462. // assertEquals(IndexedColors.AUTOMATIC.index, fmt4.getFillForegroundColor());
  463. assertEquals(PatternFormatting.NO_FILL, fmt4.getFillPattern());
  464. // borders are not set
  465. assertNull(rule3.getBorderFormatting());
  466. ConditionalFormatting cf3 = sheetCF.getConditionalFormattingAt(2);
  467. CellRangeAddress[] regions3 = cf3.getFormattingRanges();
  468. assertEquals(1, regions3.length);
  469. assertEquals("B1:B7", regions3[0].formatAsString());
  470. assertEquals(2, cf3.getNumberOfRules());
  471. ConditionalFormattingRule rule4 = cf3.getRule(0);
  472. assertEquals(ConditionType.CELL_VALUE_IS, rule4.getConditionTypeType());
  473. assertEquals(ComparisonOperator.LE, rule4.getComparisonOperation());
  474. assertEquals("\"AAA\"", rule4.getFormula1());
  475. assertNull(rule4.getFormula2());
  476. ConditionalFormattingRule rule5 = cf3.getRule(1);
  477. assertEquals(ConditionType.CELL_VALUE_IS, rule5.getConditionTypeType());
  478. assertEquals(ComparisonOperator.BETWEEN, rule5.getComparisonOperation());
  479. assertEquals("\"A\"", rule5.getFormula1());
  480. assertEquals("\"AAA\"", rule5.getFormula2());
  481. wb.close();
  482. }
  483. public void testReadOffice2007(String filename) throws IOException {
  484. Workbook wb = _testDataProvider.openSampleWorkbook(filename);
  485. Sheet s = wb.getSheet("CF");
  486. ConditionalFormatting cf = null;
  487. ConditionalFormattingRule cr = null;
  488. // Sanity check data
  489. assertEquals("Values", s.getRow(0).getCell(0).toString());
  490. assertEquals("10.0", s.getRow(2).getCell(0).toString());
  491. // Check we found all the conditional formatting rules we should have
  492. SheetConditionalFormatting sheetCF = s.getSheetConditionalFormatting();
  493. int numCF = 3;
  494. int numCF12 = 15;
  495. int numCFEX = 0; // TODO This should be 2, but we don't support CFEX formattings yet, see #58149
  496. assertEquals(numCF+numCF12+numCFEX, sheetCF.getNumConditionalFormattings());
  497. int fCF = 0, fCF12 = 0, fCFEX = 0;
  498. for (int i=0; i<sheetCF.getNumConditionalFormattings(); i++) {
  499. cf = sheetCF.getConditionalFormattingAt(i);
  500. if (cf instanceof HSSFConditionalFormatting) {
  501. String str = cf.toString();
  502. if (str.contains("[CF]")) fCF++;
  503. if (str.contains("[CF12]")) fCF12++;
  504. if (str.contains("[CFEX]")) fCFEX++;
  505. } else {
  506. ConditionType type = cf.getRule(cf.getNumberOfRules()-1).getConditionTypeType();
  507. if (type == ConditionType.CELL_VALUE_IS ||
  508. type == ConditionType.FORMULA) {
  509. fCF++;
  510. } else {
  511. // TODO Properly detect Ext ones from the xml
  512. fCF12++;
  513. }
  514. }
  515. }
  516. assertEquals(numCF, fCF);
  517. assertEquals(numCF12, fCF12);
  518. assertEquals(numCFEX, fCFEX);
  519. // Check the rules / values in detail
  520. // Highlight Positive values - Column C
  521. cf = sheetCF.getConditionalFormattingAt(0);
  522. assertEquals(1, cf.getFormattingRanges().length);
  523. assertEquals("C2:C17", cf.getFormattingRanges()[0].formatAsString());
  524. assertEquals(1, cf.getNumberOfRules());
  525. cr = cf.getRule(0);
  526. assertEquals(ConditionType.CELL_VALUE_IS, cr.getConditionTypeType());
  527. assertEquals(ComparisonOperator.GT, cr.getComparisonOperation());
  528. assertEquals("0", cr.getFormula1());
  529. assertEquals(null, cr.getFormula2());
  530. // When it matches:
  531. // Sets the font colour to dark green
  532. // Sets the background colour to lighter green
  533. // TODO Should the colours be slightly different between formats? Would CFEX support help for HSSF?
  534. if (cr instanceof HSSFConditionalFormattingRule) {
  535. assertColour("0:8080:0", cr.getFontFormatting().getFontColor());
  536. assertColour("CCCC:FFFF:CCCC", cr.getPatternFormatting().getFillBackgroundColorColor());
  537. } else {
  538. assertColour("006100", cr.getFontFormatting().getFontColor());
  539. assertColour("C6EFCE", cr.getPatternFormatting().getFillBackgroundColorColor());
  540. }
  541. // Highlight 10-30 - Column D
  542. cf = sheetCF.getConditionalFormattingAt(1);
  543. assertEquals(1, cf.getFormattingRanges().length);
  544. assertEquals("D2:D17", cf.getFormattingRanges()[0].formatAsString());
  545. assertEquals(1, cf.getNumberOfRules());
  546. cr = cf.getRule(0);
  547. assertEquals(ConditionType.CELL_VALUE_IS, cr.getConditionTypeType());
  548. assertEquals(ComparisonOperator.BETWEEN, cr.getComparisonOperation());
  549. assertEquals("10", cr.getFormula1());
  550. assertEquals("30", cr.getFormula2());
  551. // When it matches:
  552. // Sets the font colour to dark red
  553. // Sets the background colour to lighter red
  554. // TODO Should the colours be slightly different between formats? Would CFEX support help for HSSF?
  555. if (cr instanceof HSSFConditionalFormattingRule) {
  556. assertColour("8080:0:8080", cr.getFontFormatting().getFontColor());
  557. assertColour("FFFF:9999:CCCC", cr.getPatternFormatting().getFillBackgroundColorColor());
  558. } else {
  559. assertColour("9C0006", cr.getFontFormatting().getFontColor());
  560. assertColour("FFC7CE", cr.getPatternFormatting().getFillBackgroundColorColor());
  561. }
  562. // Data Bars - Column E
  563. cf = sheetCF.getConditionalFormattingAt(2);
  564. assertEquals(1, cf.getFormattingRanges().length);
  565. assertEquals("E2:E17", cf.getFormattingRanges()[0].formatAsString());
  566. assertDataBar(cf, "FF63C384");
  567. // Colours Red->Yellow->Green - Column F
  568. cf = sheetCF.getConditionalFormattingAt(3);
  569. assertEquals(1, cf.getFormattingRanges().length);
  570. assertEquals("F2:F17", cf.getFormattingRanges()[0].formatAsString());
  571. assertColorScale(cf, "F8696B", "FFEB84", "63BE7B");
  572. // Colours Blue->White->Red - Column G
  573. cf = sheetCF.getConditionalFormattingAt(4);
  574. assertEquals(1, cf.getFormattingRanges().length);
  575. assertEquals("G2:G17", cf.getFormattingRanges()[0].formatAsString());
  576. assertColorScale(cf, "5A8AC6", "FCFCFF", "F8696B");
  577. // Icons : Default - Column H, percentage thresholds
  578. cf = sheetCF.getConditionalFormattingAt(5);
  579. assertEquals(1, cf.getFormattingRanges().length);
  580. assertEquals("H2:H17", cf.getFormattingRanges()[0].formatAsString());
  581. assertIconSetPercentages(cf, IconSet.GYR_3_TRAFFIC_LIGHTS, 0d, 33d, 67d);
  582. // Icons : 3 signs - Column I
  583. cf = sheetCF.getConditionalFormattingAt(6);
  584. assertEquals(1, cf.getFormattingRanges().length);
  585. assertEquals("I2:I17", cf.getFormattingRanges()[0].formatAsString());
  586. assertIconSetPercentages(cf, IconSet.GYR_3_SHAPES, 0d, 33d, 67d);
  587. // Icons : 3 traffic lights 2 - Column J
  588. cf = sheetCF.getConditionalFormattingAt(7);
  589. assertEquals(1, cf.getFormattingRanges().length);
  590. assertEquals("J2:J17", cf.getFormattingRanges()[0].formatAsString());
  591. assertIconSetPercentages(cf, IconSet.GYR_3_TRAFFIC_LIGHTS_BOX, 0d, 33d, 67d);
  592. // Icons : 4 traffic lights - Column K
  593. cf = sheetCF.getConditionalFormattingAt(8);
  594. assertEquals(1, cf.getFormattingRanges().length);
  595. assertEquals("K2:K17", cf.getFormattingRanges()[0].formatAsString());
  596. assertIconSetPercentages(cf, IconSet.GYRB_4_TRAFFIC_LIGHTS, 0d, 25d, 50d, 75d);
  597. // Icons : 3 symbols with backgrounds - Column L
  598. cf = sheetCF.getConditionalFormattingAt(9);
  599. assertEquals(1, cf.getFormattingRanges().length);
  600. assertEquals("L2:L17", cf.getFormattingRanges()[0].formatAsString());
  601. assertIconSetPercentages(cf, IconSet.GYR_3_SYMBOLS_CIRCLE, 0d, 33d, 67d);
  602. // Icons : 3 flags - Column M2 Only
  603. cf = sheetCF.getConditionalFormattingAt(10);
  604. assertEquals(1, cf.getFormattingRanges().length);
  605. assertEquals("M2", cf.getFormattingRanges()[0].formatAsString());
  606. assertIconSetPercentages(cf, IconSet.GYR_3_FLAGS, 0d, 33d, 67d);
  607. // Icons : 3 flags - Column M (all)
  608. cf = sheetCF.getConditionalFormattingAt(11);
  609. assertEquals(1, cf.getFormattingRanges().length);
  610. assertEquals("M2:M17", cf.getFormattingRanges()[0].formatAsString());
  611. assertIconSetPercentages(cf, IconSet.GYR_3_FLAGS, 0d, 33d, 67d);
  612. // Icons : 3 symbols 2 (no background) - Column N
  613. cf = sheetCF.getConditionalFormattingAt(12);
  614. assertEquals(1, cf.getFormattingRanges().length);
  615. assertEquals("N2:N17", cf.getFormattingRanges()[0].formatAsString());
  616. assertIconSetPercentages(cf, IconSet.GYR_3_SYMBOLS, 0d, 33d, 67d);
  617. // Icons : 3 arrows - Column O
  618. cf = sheetCF.getConditionalFormattingAt(13);
  619. assertEquals(1, cf.getFormattingRanges().length);
  620. assertEquals("O2:O17", cf.getFormattingRanges()[0].formatAsString());
  621. assertIconSetPercentages(cf, IconSet.GYR_3_ARROW, 0d, 33d, 67d);
  622. // Icons : 5 arrows grey - Column P
  623. cf = sheetCF.getConditionalFormattingAt(14);
  624. assertEquals(1, cf.getFormattingRanges().length);
  625. assertEquals("P2:P17", cf.getFormattingRanges()[0].formatAsString());
  626. assertIconSetPercentages(cf, IconSet.GREY_5_ARROWS, 0d, 20d, 40d, 60d, 80d);
  627. // Icons : 3 stars (ext) - Column Q
  628. // TODO Support EXT formattings
  629. // Icons : 4 ratings - Column R
  630. cf = sheetCF.getConditionalFormattingAt(15);
  631. assertEquals(1, cf.getFormattingRanges().length);
  632. assertEquals("R2:R17", cf.getFormattingRanges()[0].formatAsString());
  633. assertIconSetPercentages(cf, IconSet.RATINGS_4, 0d, 25d, 50d, 75d);
  634. // Icons : 5 ratings - Column S
  635. cf = sheetCF.getConditionalFormattingAt(16);
  636. assertEquals(1, cf.getFormattingRanges().length);
  637. assertEquals("S2:S17", cf.getFormattingRanges()[0].formatAsString());
  638. assertIconSetPercentages(cf, IconSet.RATINGS_5, 0d, 20d, 40d, 60d, 80d);
  639. // Custom Icon+Format - Column T
  640. cf = sheetCF.getConditionalFormattingAt(17);
  641. assertEquals(1, cf.getFormattingRanges().length);
  642. assertEquals("T2:T17", cf.getFormattingRanges()[0].formatAsString());
  643. // TODO Support IconSet + Other CFs with 2 rules
  644. // assertEquals(2, cf.getNumberOfRules());
  645. // cr = cf.getRule(0);
  646. // assertIconSetPercentages(cr, IconSet.GYR_3_TRAFFIC_LIGHTS_BOX, 0d, 33d, 67d);
  647. // cr = cf.getRule(1);
  648. // assertEquals(ConditionType.FORMULA, cr.getConditionTypeType());
  649. // assertEquals(ComparisonOperator.NO_COMPARISON, cr.getComparisonOperation());
  650. // // TODO Why aren't these two the same between formats?
  651. // if (cr instanceof HSSFConditionalFormattingRule) {
  652. // assertEquals("MOD(ROW($T1),2)=1", cr.getFormula1());
  653. // } else {
  654. // assertEquals("MOD(ROW($T2),2)=1", cr.getFormula1());
  655. // }
  656. // assertEquals(null, cr.getFormula2());
  657. // Mixed icons - Column U
  658. // TODO Support EXT formattings
  659. wb.close();
  660. }
  661. private void assertDataBar(ConditionalFormatting cf, String color) {
  662. assertEquals(1, cf.getNumberOfRules());
  663. ConditionalFormattingRule cr = cf.getRule(0);
  664. assertDataBar(cr, color);
  665. }
  666. private void assertDataBar(ConditionalFormattingRule cr, String color) {
  667. assertEquals(ConditionType.DATA_BAR, cr.getConditionTypeType());
  668. assertEquals(ComparisonOperator.NO_COMPARISON, cr.getComparisonOperation());
  669. assertEquals(null, cr.getFormula1());
  670. assertEquals(null, cr.getFormula2());
  671. DataBarFormatting databar = cr.getDataBarFormatting();
  672. assertNotNull(databar);
  673. assertEquals(false, databar.isIconOnly());
  674. assertEquals(true, databar.isLeftToRight());
  675. assertEquals(0, databar.getWidthMin());
  676. assertEquals(100, databar.getWidthMax());
  677. assertColour(color, databar.getColor());
  678. ConditionalFormattingThreshold th;
  679. th = databar.getMinThreshold();
  680. assertEquals(RangeType.MIN, th.getRangeType());
  681. assertEquals(null, th.getValue());
  682. assertEquals(null, th.getFormula());
  683. th = databar.getMaxThreshold();
  684. assertEquals(RangeType.MAX, th.getRangeType());
  685. assertEquals(null, th.getValue());
  686. assertEquals(null, th.getFormula());
  687. }
  688. private void assertIconSetPercentages(ConditionalFormatting cf, IconSet iconset, Double...vals) {
  689. assertEquals(1, cf.getNumberOfRules());
  690. ConditionalFormattingRule cr = cf.getRule(0);
  691. assertIconSetPercentages(cr, iconset, vals);
  692. }
  693. private void assertIconSetPercentages(ConditionalFormattingRule cr, IconSet iconset, Double...vals) {
  694. assertEquals(ConditionType.ICON_SET, cr.getConditionTypeType());
  695. assertEquals(ComparisonOperator.NO_COMPARISON, cr.getComparisonOperation());
  696. assertEquals(null, cr.getFormula1());
  697. assertEquals(null, cr.getFormula2());
  698. IconMultiStateFormatting icon = cr.getMultiStateFormatting();
  699. assertNotNull(icon);
  700. assertEquals(iconset, icon.getIconSet());
  701. assertEquals(false, icon.isIconOnly());
  702. assertEquals(false, icon.isReversed());
  703. assertNotNull(icon.getThresholds());
  704. assertEquals(vals.length, icon.getThresholds().length);
  705. for (int i=0; i<vals.length; i++) {
  706. Double v = vals[i];
  707. ConditionalFormattingThreshold th = icon.getThresholds()[i];
  708. assertEquals(RangeType.PERCENT, th.getRangeType());
  709. assertEquals(v, th.getValue());
  710. assertEquals(null, th.getFormula());
  711. }
  712. }
  713. private void assertColorScale(ConditionalFormatting cf, String... colors) {
  714. assertEquals(1, cf.getNumberOfRules());
  715. ConditionalFormattingRule cr = cf.getRule(0);
  716. assertColorScale(cr, colors);
  717. }
  718. private void assertColorScale(ConditionalFormattingRule cr, String... colors) {
  719. assertEquals(ConditionType.COLOR_SCALE, cr.getConditionTypeType());
  720. assertEquals(ComparisonOperator.NO_COMPARISON, cr.getComparisonOperation());
  721. assertEquals(null, cr.getFormula1());
  722. assertEquals(null, cr.getFormula2());
  723. ColorScaleFormatting color = cr.getColorScaleFormatting();
  724. assertNotNull(color);
  725. assertNotNull(color.getColors());
  726. assertNotNull(color.getThresholds());
  727. assertEquals(colors.length, color.getNumControlPoints());
  728. assertEquals(colors.length, color.getColors().length);
  729. assertEquals(colors.length, color.getThresholds().length);
  730. // Thresholds should be Min / (evenly spaced) / Max
  731. int steps = 100 / (colors.length-1);
  732. for (int i=0; i<colors.length; i++) {
  733. ConditionalFormattingThreshold th = color.getThresholds()[i];
  734. if (i == 0) {
  735. assertEquals(RangeType.MIN, th.getRangeType());
  736. } else if (i == colors.length-1) {
  737. assertEquals(RangeType.MAX, th.getRangeType());
  738. } else {
  739. assertEquals(RangeType.PERCENTILE, th.getRangeType());
  740. assertEquals(steps*i, th.getValue().intValue());
  741. }
  742. assertEquals(null, th.getFormula());
  743. }
  744. // Colors should match
  745. for (int i=0; i<colors.length; i++) {
  746. assertColour(colors[i], color.getColors()[i]);
  747. }
  748. }
  749. @Test
  750. public void testCreateFontFormatting() throws IOException {
  751. Workbook workbook = _testDataProvider.createWorkbook();
  752. Sheet sheet = workbook.createSheet();
  753. SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
  754. ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(ComparisonOperator.EQUAL, "7");
  755. FontFormatting fontFmt = rule1.createFontFormatting();
  756. assertFalse(fontFmt.isItalic());
  757. assertFalse(fontFmt.isBold());
  758. fontFmt.setFontStyle(true, true);
  759. assertTrue(fontFmt.isItalic());
  760. assertTrue(fontFmt.isBold());
  761. assertEquals(-1, fontFmt.getFontHeight()); // not modified
  762. fontFmt.setFontHeight(200);
  763. assertEquals(200, fontFmt.getFontHeight());
  764. fontFmt.setFontHeight(100);
  765. assertEquals(100, fontFmt.getFontHeight());
  766. assertEquals(FontFormatting.SS_NONE, fontFmt.getEscapementType());
  767. fontFmt.setEscapementType(FontFormatting.SS_SUB);
  768. assertEquals(FontFormatting.SS_SUB, fontFmt.getEscapementType());
  769. fontFmt.setEscapementType(FontFormatting.SS_NONE);
  770. assertEquals(FontFormatting.SS_NONE, fontFmt.getEscapementType());
  771. fontFmt.setEscapementType(FontFormatting.SS_SUPER);
  772. assertEquals(FontFormatting.SS_SUPER, fontFmt.getEscapementType());
  773. assertEquals(FontFormatting.U_NONE, fontFmt.getUnderlineType());
  774. fontFmt.setUnderlineType(FontFormatting.U_SINGLE);
  775. assertEquals(FontFormatting.U_SINGLE, fontFmt.getUnderlineType());
  776. fontFmt.setUnderlineType(FontFormatting.U_NONE);
  777. assertEquals(FontFormatting.U_NONE, fontFmt.getUnderlineType());
  778. fontFmt.setUnderlineType(FontFormatting.U_DOUBLE);
  779. assertEquals(FontFormatting.U_DOUBLE, fontFmt.getUnderlineType());
  780. assertEquals(-1, fontFmt.getFontColorIndex());
  781. fontFmt.setFontColorIndex(IndexedColors.RED.index);
  782. assertEquals(IndexedColors.RED.index, fontFmt.getFontColorIndex());
  783. fontFmt.setFontColorIndex(IndexedColors.AUTOMATIC.index);
  784. assertEquals(IndexedColors.AUTOMATIC.index, fontFmt.getFontColorIndex());
  785. fontFmt.setFontColorIndex(IndexedColors.BLUE.index);
  786. assertEquals(IndexedColors.BLUE.index, fontFmt.getFontColorIndex());
  787. ConditionalFormattingRule [] cfRules = { rule1 };
  788. CellRangeAddress [] regions = { CellRangeAddress.valueOf("A1:A5") };
  789. sheetCF.addConditionalFormatting(regions, cfRules);
  790. // Verification
  791. ConditionalFormatting cf = sheetCF.getConditionalFormattingAt(0);
  792. assertNotNull(cf);
  793. assertEquals(1, cf.getNumberOfRules());
  794. FontFormatting r1fp = cf.getRule(0).getFontFormatting();
  795. assertNotNull(r1fp);
  796. assertTrue(r1fp.isItalic());
  797. assertTrue(r1fp.isBold());
  798. assertEquals(FontFormatting.SS_SUPER, r1fp.getEscapementType());
  799. assertEquals(FontFormatting.U_DOUBLE, r1fp.getUnderlineType());
  800. assertEquals(IndexedColors.BLUE.index, r1fp.getFontColorIndex());
  801. workbook.close();
  802. }
  803. @Test
  804. public void testCreatePatternFormatting() throws IOException {
  805. Workbook workbook = _testDataProvider.createWorkbook();
  806. Sheet sheet = workbook.createSheet();
  807. SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
  808. ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(ComparisonOperator.EQUAL, "7");
  809. PatternFormatting patternFmt = rule1.createPatternFormatting();
  810. assertEquals(0, patternFmt.getFillBackgroundColor());
  811. patternFmt.setFillBackgroundColor(IndexedColors.RED.index);
  812. assertEquals(IndexedColors.RED.index, patternFmt.getFillBackgroundColor());
  813. assertEquals(0, patternFmt.getFillForegroundColor());
  814. patternFmt.setFillForegroundColor(IndexedColors.BLUE.index);
  815. assertEquals(IndexedColors.BLUE.index, patternFmt.getFillForegroundColor());
  816. assertEquals(PatternFormatting.NO_FILL, patternFmt.getFillPattern());
  817. patternFmt.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
  818. assertEquals(PatternFormatting.SOLID_FOREGROUND, patternFmt.getFillPattern());
  819. patternFmt.setFillPattern(PatternFormatting.NO_FILL);
  820. assertEquals(PatternFormatting.NO_FILL, patternFmt.getFillPattern());
  821. patternFmt.setFillPattern(PatternFormatting.BRICKS);
  822. assertEquals(PatternFormatting.BRICKS, patternFmt.getFillPattern());
  823. ConditionalFormattingRule [] cfRules = { rule1 };
  824. CellRangeAddress [] regions = { CellRangeAddress.valueOf("A1:A5") };
  825. sheetCF.addConditionalFormatting(regions, cfRules);
  826. // Verification
  827. ConditionalFormatting cf = sheetCF.getConditionalFormattingAt(0);
  828. assertNotNull(cf);
  829. assertEquals(1, cf.getNumberOfRules());
  830. PatternFormatting r1fp = cf.getRule(0).getPatternFormatting();
  831. assertNotNull(r1fp);
  832. assertEquals(IndexedColors.RED.index, r1fp.getFillBackgroundColor());
  833. assertEquals(IndexedColors.BLUE.index, r1fp.getFillForegroundColor());
  834. assertEquals(PatternFormatting.BRICKS, r1fp.getFillPattern());
  835. workbook.close();
  836. }
  837. @Test
  838. public void testAllCreateBorderFormatting() throws IOException {
  839. // Make sure it is possible to create a conditional formatting rule
  840. // with every type of Border Style
  841. Workbook workbook = _testDataProvider.createWorkbook();
  842. Sheet sheet = workbook.createSheet();
  843. SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
  844. ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(ComparisonOperator.EQUAL, "7");
  845. BorderFormatting borderFmt = rule1.createBorderFormatting();
  846. for (BorderStyle border : BorderStyle.values()) {
  847. borderFmt.setBorderTop(border);
  848. assertEquals(border, borderFmt.getBorderTop());
  849. borderFmt.setBorderBottom(border);
  850. assertEquals(border, borderFmt.getBorderBottom());
  851. borderFmt.setBorderLeft(border);
  852. assertEquals(border, borderFmt.getBorderLeft());
  853. borderFmt.setBorderRight(border);
  854. assertEquals(border, borderFmt.getBorderRight());
  855. borderFmt.setBorderDiagonal(border);
  856. assertEquals(border, borderFmt.getBorderDiagonal());
  857. }
  858. workbook.close();
  859. }
  860. @Test
  861. public void testCreateBorderFormatting() throws IOException {
  862. Workbook workbook = _testDataProvider.createWorkbook();
  863. Sheet sheet = workbook.createSheet();
  864. SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
  865. ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(ComparisonOperator.EQUAL, "7");
  866. BorderFormatting borderFmt = rule1.createBorderFormatting();
  867. assertEquals(BorderStyle.NONE, borderFmt.getBorderBottom());
  868. borderFmt.setBorderBottom(BorderStyle.DOTTED);
  869. assertEquals(BorderStyle.DOTTED, borderFmt.getBorderBottom());
  870. borderFmt.setBorderBottom(BorderStyle.NONE);
  871. assertEquals(BorderStyle.NONE, borderFmt.getBorderBottom());
  872. borderFmt.setBorderBottom(BorderStyle.THICK);
  873. assertEquals(BorderStyle.THICK, borderFmt.getBorderBottom());
  874. assertEquals(BorderStyle.NONE, borderFmt.getBorderTop());
  875. borderFmt.setBorderTop(BorderStyle.DOTTED);
  876. assertEquals(BorderStyle.DOTTED, borderFmt.getBorderTop());
  877. borderFmt.setBorderTop(BorderStyle.NONE);
  878. assertEquals(BorderStyle.NONE, borderFmt.getBorderTop());
  879. borderFmt.setBorderTop(BorderStyle.THICK);
  880. assertEquals(BorderStyle.THICK, borderFmt.getBorderTop());
  881. assertEquals(BorderStyle.NONE, borderFmt.getBorderLeft());
  882. borderFmt.setBorderLeft(BorderStyle.DOTTED);
  883. assertEquals(BorderStyle.DOTTED, borderFmt.getBorderLeft());
  884. borderFmt.setBorderLeft(BorderStyle.NONE);
  885. assertEquals(BorderStyle.NONE, borderFmt.getBorderLeft());
  886. borderFmt.setBorderLeft(BorderStyle.THIN);
  887. assertEquals(BorderStyle.THIN, borderFmt.getBorderLeft());
  888. assertEquals(BorderStyle.NONE, borderFmt.getBorderRight());
  889. borderFmt.setBorderRight(BorderStyle.DOTTED);
  890. assertEquals(BorderStyle.DOTTED, borderFmt.getBorderRight());
  891. borderFmt.setBorderRight(BorderStyle.NONE);
  892. assertEquals(BorderStyle.NONE, borderFmt.getBorderRight());
  893. borderFmt.setBorderRight(BorderStyle.HAIR);
  894. assertEquals(BorderStyle.HAIR, borderFmt.getBorderRight());
  895. ConditionalFormattingRule [] cfRules = { rule1 };
  896. CellRangeAddress [] regions = { CellRangeAddress.valueOf("A1:A5") };
  897. sheetCF.addConditionalFormatting(regions, cfRules);
  898. // Verification
  899. ConditionalFormatting cf = sheetCF.getConditionalFormattingAt(0);
  900. assertNotNull(cf);
  901. assertEquals(1, cf.getNumberOfRules());
  902. BorderFormatting r1fp = cf.getRule(0).getBorderFormatting();
  903. assertNotNull(r1fp);
  904. assertEquals(BorderStyle.THICK, r1fp.getBorderBottom());
  905. assertEquals(BorderStyle.THICK, r1fp.getBorderTop());
  906. assertEquals(BorderStyle.THIN, r1fp.getBorderLeft());
  907. assertEquals(BorderStyle.HAIR, r1fp.getBorderRight());
  908. workbook.close();
  909. }
  910. @Test
  911. public void testCreateIconFormatting() throws IOException {
  912. Workbook wb1 = _testDataProvider.createWorkbook();
  913. Sheet sheet = wb1.createSheet();
  914. SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
  915. ConditionalFormattingRule rule1 =
  916. sheetCF.createConditionalFormattingRule(IconSet.GYRB_4_TRAFFIC_LIGHTS);
  917. IconMultiStateFormatting iconFmt = rule1.getMultiStateFormatting();
  918. assertEquals(IconSet.GYRB_4_TRAFFIC_LIGHTS, iconFmt.getIconSet());
  919. assertEquals(4, iconFmt.getThresholds().length);
  920. assertEquals(false, iconFmt.isIconOnly());
  921. assertEquals(false, iconFmt.isReversed());
  922. iconFmt.setIconOnly(true);
  923. iconFmt.getThresholds()[0].setRangeType(RangeType.MIN);
  924. iconFmt.getThresholds()[1].setRangeType(RangeType.NUMBER);
  925. iconFmt.getThresholds()[1].setValue(10d);
  926. iconFmt.getThresholds()[2].setRangeType(RangeType.PERCENT);
  927. iconFmt.getThresholds()[2].setValue(75d);
  928. iconFmt.getThresholds()[3].setRangeType(RangeType.MAX);
  929. CellRangeAddress [] regions = { CellRangeAddress.valueOf("A1:A5") };
  930. sheetCF.addConditionalFormatting(regions, rule1);
  931. // Save, re-load and re-check
  932. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  933. wb1.close();
  934. sheet = wb2.getSheetAt(0);
  935. sheetCF = sheet.getSheetConditionalFormatting();
  936. assertEquals(1, sheetCF.getNumConditionalFormattings());
  937. ConditionalFormatting cf = sheetCF.getConditionalFormattingAt(0);
  938. assertEquals(1, cf.getNumberOfRules());
  939. rule1 = cf.getRule(0);
  940. assertEquals(ConditionType.ICON_SET, rule1.getConditionTypeType());
  941. iconFmt = rule1.getMultiStateFormatting();
  942. assertEquals(IconSet.GYRB_4_TRAFFIC_LIGHTS, iconFmt.getIconSet());
  943. assertEquals(4, iconFmt.getThresholds().length);
  944. assertEquals(true, iconFmt.isIconOnly());
  945. assertEquals(false, iconFmt.isReversed());
  946. assertEquals(RangeType.MIN, iconFmt.getThresholds()[0].getRangeType());
  947. assertEquals(RangeType.NUMBER, iconFmt.getThresholds()[1].getRangeType());
  948. assertEquals(RangeType.PERCENT,iconFmt.getThresholds()[2].getRangeType());
  949. assertEquals(RangeType.MAX, iconFmt.getThresholds()[3].getRangeType());
  950. assertEquals(null, iconFmt.getThresholds()[0].getValue());
  951. assertEquals(10d, iconFmt.getThresholds()[1].getValue(), 0);
  952. assertEquals(75d, iconFmt.getThresholds()[2].getValue(), 0);
  953. assertEquals(null, iconFmt.getThresholds()[3].getValue());
  954. wb2.close();
  955. }
  956. @Test
  957. public void testCreateColorScaleFormatting() throws IOException {
  958. Workbook wb1 = _testDataProvider.createWorkbook();
  959. Sheet sheet = wb1.createSheet();
  960. SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
  961. ConditionalFormattingRule rule1 =
  962. sheetCF.createConditionalFormattingColorScaleRule();
  963. ColorScaleFormatting clrFmt = rule1.getColorScaleFormatting();
  964. assertEquals(3, clrFmt.getNumControlPoints());
  965. assertEquals(3, clrFmt.getColors().length);
  966. assertEquals(3, clrFmt.getThresholds().length);
  967. clrFmt.getThresholds()[0].setRangeType(RangeType.MIN);
  968. clrFmt.getThresholds()[1].setRangeType(RangeType.NUMBER);
  969. clrFmt.getThresholds()[1].setValue(10d);
  970. clrFmt.getThresholds()[2].setRangeType(RangeType.MAX);
  971. CellRangeAddress [] regions = { CellRangeAddress.valueOf("A1:A5") };
  972. sheetCF.addConditionalFormatting(regions, rule1);
  973. // Save, re-load and re-check
  974. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  975. wb1.close();
  976. sheet = wb2.getSheetAt(0);
  977. sheetCF = sheet.getSheetConditionalFormatting();
  978. assertEquals(1, sheetCF.getNumConditionalFormattings());
  979. ConditionalFormatting cf = sheetCF.getConditionalFormattingAt(0);
  980. assertEquals(1, cf.getNumberOfRules());
  981. rule1 = cf.getRule(0);
  982. clrFmt = rule1.getColorScaleFormatting();
  983. assertEquals(ConditionType.COLOR_SCALE, rule1.getConditionTypeType());
  984. assertEquals(3, clrFmt.getNumControlPoints());
  985. assertEquals(3, clrFmt.getColors().length);
  986. assertEquals(3, clrFmt.getThresholds().length);
  987. assertEquals(RangeType.MIN, clrFmt.getThresholds()[0].getRangeType());
  988. assertEquals(RangeType.NUMBER, clrFmt.getThresholds()[1].getRangeType());
  989. assertEquals(RangeType.MAX, clrFmt.getThresholds()[2].getRangeType());
  990. assertEquals(null, clrFmt.getThresholds()[0].getValue());
  991. assertEquals(10d, clrFmt.getThresholds()[1].getValue(), 0);
  992. assertEquals(null, clrFmt.getThresholds()[2].getValue());
  993. wb2.close();
  994. }
  995. @Test
  996. public void testCreateDataBarFormatting() throws IOException {
  997. Workbook wb1 = _testDataProvider.createWorkbook();
  998. Sheet sheet = wb1.createSheet();
  999. String colorHex = "FFFFEB84";
  1000. ExtendedColor color = wb1.getCreationHelper().createExtendedColor();
  1001. color.setARGBHex(colorHex);
  1002. SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
  1003. ConditionalFormattingRule rule1 =
  1004. sheetCF.createConditionalFormattingRule(color);
  1005. DataBarFormatting dbFmt = rule1.getDataBarFormatting();
  1006. assertEquals(false, dbFmt.isIconOnly());
  1007. assertEquals(true, dbFmt.isLeftToRight());
  1008. assertEquals(0, dbFmt.getWidthMin());
  1009. assertEquals(100, dbFmt.getWidthMax());
  1010. assertColour(colorHex, dbFmt.getColor());
  1011. dbFmt.getMinThreshold().setRangeType(RangeType.MIN);
  1012. dbFmt.getMaxThreshold().setRangeType(RangeType.MAX);
  1013. CellRangeAddress [] regions = { CellRangeAddress.valueOf("A1:A5") };
  1014. sheetCF.addConditionalFormatting(regions, rule1);
  1015. // Save, re-load and re-check
  1016. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  1017. wb1.close();
  1018. sheet = wb2.getSheetAt(0);
  1019. sheetCF = sheet.getSheetConditionalFormatting();
  1020. assertEquals(1, sheetCF.getNumConditionalFormattings());
  1021. ConditionalFormatting cf = sheetCF.getConditionalFormattingAt(0);
  1022. assertEquals(1, cf.getNumberOfRules());
  1023. rule1 = cf.getRule(0);
  1024. dbFmt = rule1.getDataBarFormatting();
  1025. assertEquals(ConditionType.DATA_BAR, rule1.getConditionTypeType());
  1026. assertEquals(false, dbFmt.isIconOnly());
  1027. assertEquals(true, dbFmt.isLeftToRight());
  1028. assertEquals(0, dbFmt.getWidthMin());
  1029. assertEquals(100, dbFmt.getWidthMax());
  1030. assertColour(colorHex, dbFmt.getColor());
  1031. assertEquals(RangeType.MIN, dbFmt.getMinThreshold().getRangeType());
  1032. assertEquals(RangeType.MAX, dbFmt.getMaxThreshold().getRangeType());
  1033. assertEquals(null, dbFmt.getMinThreshold().getValue());
  1034. assertEquals(null, dbFmt.getMaxThreshold().getValue());
  1035. wb2.close();
  1036. }
  1037. @Test
  1038. public void testBug55380() throws IOException {
  1039. Workbook wb = _testDataProvider.createWorkbook();
  1040. Sheet sheet = wb.createSheet();
  1041. CellRangeAddress[] ranges = new CellRangeAddress[] {
  1042. CellRangeAddress.valueOf("C9:D30"), CellRangeAddress.valueOf("C7:C31")
  1043. };
  1044. ConditionalFormattingRule rule = sheet.getSheetConditionalFormatting().createConditionalFormattingRule("$A$1>0");
  1045. sheet.getSheetConditionalFormatting().addConditionalFormatting(ranges, rule);
  1046. wb.close();
  1047. }
  1048. }