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.

TestXSSFConditionalFormatting.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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.xssf.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.assertTrue;
  25. import java.io.IOException;
  26. import org.apache.poi.ss.usermodel.BaseTestConditionalFormatting;
  27. import org.apache.poi.ss.usermodel.Color;
  28. import org.apache.poi.ss.usermodel.ConditionalFormatting;
  29. import org.apache.poi.ss.usermodel.ConditionalFormattingRule;
  30. import org.apache.poi.ss.usermodel.ConditionalFormattingThreshold;
  31. import org.apache.poi.ss.usermodel.ExtendedColor;
  32. import org.apache.poi.ss.usermodel.FontFormatting;
  33. import org.apache.poi.ss.usermodel.PatternFormatting;
  34. import org.apache.poi.ss.usermodel.Sheet;
  35. import org.apache.poi.ss.usermodel.SheetConditionalFormatting;
  36. import org.apache.poi.ss.usermodel.Workbook;
  37. import org.apache.poi.ss.util.CellRangeAddress;
  38. import org.apache.poi.xssf.XSSFITestDataProvider;
  39. import org.junit.jupiter.api.Test;
  40. /**
  41. * XSSF-specific Conditional Formatting tests
  42. */
  43. class TestXSSFConditionalFormatting extends BaseTestConditionalFormatting {
  44. public TestXSSFConditionalFormatting(){
  45. super(XSSFITestDataProvider.instance);
  46. }
  47. //https://docs.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.databar?view=openxml-2.8.1
  48. //defaults are 10% and 90%
  49. @Override
  50. protected int defaultDataBarMinLength() { return 10; }
  51. @Override
  52. protected int defaultDataBarMaxLength() { return 90; }
  53. @Override
  54. protected void assertColor(String hexExpected, Color actual) {
  55. assertNotNull(actual, "Color must be given");
  56. XSSFColor color = (XSSFColor)actual;
  57. if (hexExpected.length() == 8) {
  58. assertEquals(hexExpected, color.getARGBHex());
  59. } else {
  60. assertEquals(hexExpected, color.getARGBHex().substring(2));
  61. }
  62. }
  63. @Test
  64. void testRead() throws IOException {
  65. testRead("WithConditionalFormatting.xlsx");
  66. }
  67. @Test
  68. void testReadOffice2007() throws IOException {
  69. testReadOffice2007("NewStyleConditionalFormattings.xlsx");
  70. }
  71. private static final java.awt.Color PEAK_ORANGE = new java.awt.Color(255, 239, 221);
  72. @Test
  73. void testFontFormattingColor() {
  74. XSSFWorkbook wb = XSSFITestDataProvider.instance.createWorkbook();
  75. final Sheet sheet = wb.createSheet();
  76. final SheetConditionalFormatting formatting = sheet.getSheetConditionalFormatting();
  77. // the conditional formatting is not automatically added when it is created...
  78. assertEquals(0, formatting.getNumConditionalFormattings());
  79. ConditionalFormattingRule formattingRule = formatting.createConditionalFormattingRule("A1");
  80. assertEquals(0, formatting.getNumConditionalFormattings());
  81. // adding the formatting makes it available
  82. int idx = formatting.addConditionalFormatting(new CellRangeAddress[] {}, formattingRule);
  83. // verify that it can be accessed now
  84. assertEquals(0, idx);
  85. assertEquals(1, formatting.getNumConditionalFormattings());
  86. assertEquals(1, formatting.getConditionalFormattingAt(idx).getNumberOfRules());
  87. // this is confusing: the rule is not connected to the sheet, changes are not applied
  88. // so we need to use setRule() explicitly!
  89. FontFormatting fontFmt = formattingRule.createFontFormatting();
  90. assertNotNull(formattingRule.getFontFormatting());
  91. assertEquals(1, formatting.getConditionalFormattingAt(idx).getNumberOfRules());
  92. formatting.getConditionalFormattingAt(idx).setRule(0, formattingRule);
  93. assertNotNull(formatting.getConditionalFormattingAt(idx).getRule(0).getFontFormatting());
  94. fontFmt.setFontStyle(true, false);
  95. assertEquals(-1, fontFmt.getFontColorIndex());
  96. //fontFmt.setFontColorIndex((short)11);
  97. final ExtendedColor extendedColor = new XSSFColor(PEAK_ORANGE, wb.getStylesSource().getIndexedColors());
  98. fontFmt.setFontColor(extendedColor);
  99. PatternFormatting patternFmt = formattingRule.createPatternFormatting();
  100. assertNotNull(patternFmt);
  101. patternFmt.setFillBackgroundColor(extendedColor);
  102. assertEquals(1, formatting.getConditionalFormattingAt(0).getNumberOfRules());
  103. assertNotNull(formatting.getConditionalFormattingAt(0).getRule(0).getFontFormatting());
  104. assertNotNull(formatting.getConditionalFormattingAt(0).getRule(0).getFontFormatting().getFontColor());
  105. assertNotNull(formatting.getConditionalFormattingAt(0).getRule(0).getPatternFormatting().getFillBackgroundColorColor());
  106. checkFontFormattingColorWriteOutAndReadBack(wb, extendedColor);
  107. }
  108. private void checkFontFormattingColorWriteOutAndReadBack(Workbook wb, ExtendedColor extendedColor) {
  109. Workbook wbBack = XSSFITestDataProvider.instance.writeOutAndReadBack(wb);
  110. assertNotNull(wbBack);
  111. assertEquals(1, wbBack.getSheetAt(0).getSheetConditionalFormatting().getNumConditionalFormattings());
  112. final ConditionalFormatting formattingBack = wbBack.getSheetAt(0).getSheetConditionalFormatting().getConditionalFormattingAt(0);
  113. assertEquals(1, wbBack.getSheetAt(0).getSheetConditionalFormatting().getConditionalFormattingAt(0).getNumberOfRules());
  114. final ConditionalFormattingRule ruleBack = formattingBack.getRule(0);
  115. final FontFormatting fontFormattingBack = ruleBack.getFontFormatting();
  116. assertNotNull(formattingBack);
  117. assertNotNull(fontFormattingBack.getFontColor());
  118. assertEquals(extendedColor, fontFormattingBack.getFontColor());
  119. assertEquals(extendedColor, ruleBack.getPatternFormatting().getFillBackgroundColorColor());
  120. }
  121. @Override
  122. protected boolean applyLimitOf3() {
  123. return false;
  124. }
  125. @Override
  126. protected void checkThreshold(ConditionalFormattingThreshold threshold) {
  127. assertNull(threshold.getValue());
  128. assertNull(threshold.getFormula());
  129. assertTrue(threshold instanceof XSSFConditionalFormattingThreshold,
  130. "threshold is a XSSFConditionalFormattingThreshold?");
  131. XSSFConditionalFormattingThreshold xssfThreshold = (XSSFConditionalFormattingThreshold)threshold;
  132. assertTrue(xssfThreshold.isGte(), "gte defaults to true?");
  133. xssfThreshold.setGte(false);
  134. assertFalse(xssfThreshold.isGte(), "gte changed to false?");
  135. xssfThreshold.setGte(true);
  136. assertTrue(xssfThreshold.isGte(), "gte changed to true?");
  137. }
  138. }