Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

BaseTestConditionalFormatting.java 65KB

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