2009-05-18 15:11:45 -04:00
|
|
|
/* ====================================================================
|
|
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
contributor license agreements. See the NOTICE file distributed with
|
|
|
|
this work for additional information regarding copyright ownership.
|
|
|
|
The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
(the "License"); you may not use this file except in compliance with
|
|
|
|
the License. You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
==================================================================== */
|
|
|
|
|
|
|
|
package org.apache.poi.ss.usermodel;
|
|
|
|
|
2014-01-10 18:14:03 -05:00
|
|
|
import static org.junit.Assert.assertEquals;
|
2015-05-19 09:13:09 -04:00
|
|
|
import static org.junit.Assert.assertNotNull;
|
|
|
|
import static org.junit.Assert.assertNull;
|
2014-01-10 18:14:03 -05:00
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
import static org.junit.Assert.fail;
|
2009-12-25 21:17:34 -05:00
|
|
|
|
2015-07-22 08:43:48 -04:00
|
|
|
import java.awt.font.FontRenderContext;
|
|
|
|
import java.awt.font.TextAttribute;
|
|
|
|
import java.awt.font.TextLayout;
|
2015-05-19 09:13:09 -04:00
|
|
|
import java.io.IOException;
|
2015-07-22 08:43:48 -04:00
|
|
|
import java.text.AttributedString;
|
2014-10-20 19:22:02 -04:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
2011-06-25 08:19:49 -04:00
|
|
|
import org.apache.poi.hssf.util.PaneInformation;
|
2009-05-18 15:11:45 -04:00
|
|
|
import org.apache.poi.ss.ITestDataProvider;
|
|
|
|
import org.apache.poi.ss.SpreadsheetVersion;
|
|
|
|
import org.apache.poi.ss.util.CellRangeAddress;
|
2015-07-16 05:06:47 -04:00
|
|
|
import org.apache.poi.ss.util.SheetUtil;
|
|
|
|
import org.junit.Assume;
|
2015-07-13 08:17:52 -04:00
|
|
|
import org.junit.Ignore;
|
2014-01-10 18:14:03 -05:00
|
|
|
import org.junit.Test;
|
2009-05-18 15:11:45 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A base class for bugzilla issues that can be described in terms of common ss interfaces.
|
|
|
|
*
|
|
|
|
* @author Yegor Kozlov
|
|
|
|
*/
|
2014-01-10 18:14:03 -05:00
|
|
|
public abstract class BaseTestBugzillaIssues {
|
2009-05-18 15:11:45 -04:00
|
|
|
|
2009-12-25 21:17:34 -05:00
|
|
|
private final ITestDataProvider _testDataProvider;
|
|
|
|
|
|
|
|
protected BaseTestBugzillaIssues(ITestDataProvider testDataProvider) {
|
|
|
|
_testDataProvider = testDataProvider;
|
|
|
|
}
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-04-18 19:44:08 -04:00
|
|
|
public static void assertAlmostEquals(double expected, double actual, float factor) {
|
2014-04-18 16:46:57 -04:00
|
|
|
double diff = Math.abs(expected - actual);
|
2014-04-18 19:44:08 -04:00
|
|
|
double fuzz = expected * factor;
|
2014-04-18 16:46:57 -04:00
|
|
|
if (diff > fuzz)
|
|
|
|
fail(actual + " not within " + fuzz + " of " + expected);
|
|
|
|
}
|
2009-05-18 15:11:45 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test writing a hyperlink
|
|
|
|
* Open resulting sheet in Excel and check that A1 contains a hyperlink
|
|
|
|
*
|
|
|
|
* Also tests bug 15353 (problems with hyperlinks to Google)
|
|
|
|
*/
|
2014-01-10 18:14:03 -05:00
|
|
|
@Test
|
2015-10-12 16:43:42 -04:00
|
|
|
public final void bug23094() throws IOException {
|
2015-09-20 08:02:40 -04:00
|
|
|
Workbook wb1 = _testDataProvider.createWorkbook();
|
|
|
|
Sheet s = wb1.createSheet();
|
2009-05-18 15:11:45 -04:00
|
|
|
Row r = s.createRow(0);
|
|
|
|
r.createCell(0).setCellFormula("HYPERLINK(\"http://jakarta.apache.org\",\"Jakarta\")");
|
|
|
|
r.createCell(1).setCellFormula("HYPERLINK(\"http://google.com\",\"Google\")");
|
|
|
|
|
2015-09-20 08:02:40 -04:00
|
|
|
Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
|
|
|
|
wb1.close();
|
|
|
|
r = wb2.getSheetAt(0).getRow(0);
|
2009-05-18 15:11:45 -04:00
|
|
|
|
|
|
|
Cell cell_0 = r.getCell(0);
|
|
|
|
assertEquals("HYPERLINK(\"http://jakarta.apache.org\",\"Jakarta\")", cell_0.getCellFormula());
|
|
|
|
Cell cell_1 = r.getCell(1);
|
|
|
|
assertEquals("HYPERLINK(\"http://google.com\",\"Google\")", cell_1.getCellFormula());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
|
|
|
wb2.close();
|
2009-05-18 15:11:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test writing a file with large number of unique strings,
|
|
|
|
* open resulting file in Excel to check results!
|
|
|
|
* @param num the number of strings to generate
|
|
|
|
*/
|
2015-10-12 16:43:42 -04:00
|
|
|
public final void bug15375(int num) throws IOException {
|
2015-09-20 08:02:40 -04:00
|
|
|
Workbook wb1 = _testDataProvider.createWorkbook();
|
|
|
|
Sheet sheet = wb1.createSheet();
|
|
|
|
CreationHelper factory = wb1.getCreationHelper();
|
2009-05-18 15:11:45 -04:00
|
|
|
|
|
|
|
String tmp1 = null;
|
|
|
|
String tmp2 = null;
|
|
|
|
String tmp3 = null;
|
|
|
|
|
|
|
|
for (int i = 0; i < num; i++) {
|
|
|
|
tmp1 = "Test1" + i;
|
|
|
|
tmp2 = "Test2" + i;
|
|
|
|
tmp3 = "Test3" + i;
|
|
|
|
|
|
|
|
Row row = sheet.createRow(i);
|
|
|
|
|
|
|
|
Cell cell = row.createCell(0);
|
|
|
|
cell.setCellValue(factory.createRichTextString(tmp1));
|
|
|
|
cell = row.createCell(1);
|
|
|
|
cell.setCellValue(factory.createRichTextString(tmp2));
|
|
|
|
cell = row.createCell(2);
|
|
|
|
cell.setCellValue(factory.createRichTextString(tmp3));
|
|
|
|
}
|
2015-09-20 08:02:40 -04:00
|
|
|
Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
|
|
|
|
wb1.close();
|
|
|
|
|
|
|
|
sheet = wb2.getSheetAt(0);
|
2009-05-18 15:11:45 -04:00
|
|
|
for (int i = 0; i < num; i++) {
|
|
|
|
tmp1 = "Test1" + i;
|
|
|
|
tmp2 = "Test2" + i;
|
|
|
|
tmp3 = "Test3" + i;
|
|
|
|
|
|
|
|
Row row = sheet.getRow(i);
|
|
|
|
|
|
|
|
assertEquals(tmp1, row.getCell(0).getStringCellValue());
|
|
|
|
assertEquals(tmp2, row.getCell(1).getStringCellValue());
|
|
|
|
assertEquals(tmp3, row.getCell(2).getStringCellValue());
|
|
|
|
}
|
2015-09-20 08:02:40 -04:00
|
|
|
wb2.close();
|
2009-05-18 15:11:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merged regions were being removed from the parent in cloned sheets
|
|
|
|
*/
|
2014-01-10 18:14:03 -05:00
|
|
|
@Test
|
2015-10-12 16:43:42 -04:00
|
|
|
public void bug22720() throws IOException {
|
2015-09-20 08:02:40 -04:00
|
|
|
Workbook wb = _testDataProvider.createWorkbook();
|
|
|
|
wb.createSheet("TEST");
|
|
|
|
Sheet template = wb.getSheetAt(0);
|
2009-05-18 15:11:45 -04:00
|
|
|
|
|
|
|
template.addMergedRegion(new CellRangeAddress(0, 1, 0, 2));
|
2015-10-31 06:22:19 -04:00
|
|
|
template.addMergedRegion(new CellRangeAddress(2, 3, 0, 2));
|
2009-05-18 15:11:45 -04:00
|
|
|
|
2015-09-20 08:02:40 -04:00
|
|
|
Sheet clone = wb.cloneSheet(0);
|
2009-05-18 15:11:45 -04:00
|
|
|
int originalMerged = template.getNumMergedRegions();
|
|
|
|
assertEquals("2 merged regions", 2, originalMerged);
|
|
|
|
|
|
|
|
//remove merged regions from clone
|
|
|
|
for (int i=template.getNumMergedRegions()-1; i>=0; i--) {
|
|
|
|
clone.removeMergedRegion(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals("Original Sheet's Merged Regions were removed", originalMerged, template.getNumMergedRegions());
|
|
|
|
//check if template's merged regions are OK
|
|
|
|
if (template.getNumMergedRegions()>0) {
|
|
|
|
// fetch the first merged region...EXCEPTION OCCURS HERE
|
|
|
|
template.getMergedRegion(0);
|
|
|
|
}
|
|
|
|
//make sure we dont exception
|
2015-09-20 08:02:40 -04:00
|
|
|
wb.close();
|
2009-05-18 15:11:45 -04:00
|
|
|
}
|
|
|
|
|
2014-01-10 18:14:03 -05:00
|
|
|
@Test
|
2015-10-12 16:43:42 -04:00
|
|
|
public final void bug28031() throws IOException {
|
2015-09-20 08:02:40 -04:00
|
|
|
Workbook wb1 = _testDataProvider.createWorkbook();
|
|
|
|
Sheet sheet = wb1.createSheet();
|
|
|
|
wb1.setSheetName(0, "Sheet1");
|
2009-05-18 15:11:45 -04:00
|
|
|
|
|
|
|
Row row = sheet.createRow(0);
|
|
|
|
Cell cell = row.createCell(0);
|
|
|
|
String formulaText =
|
|
|
|
"IF(ROUND(A2*B2*C2,2)>ROUND(B2*D2,2),ROUND(A2*B2*C2,2),ROUND(B2*D2,2))";
|
|
|
|
cell.setCellFormula(formulaText);
|
|
|
|
|
|
|
|
assertEquals(formulaText, cell.getCellFormula());
|
2015-09-20 08:02:40 -04:00
|
|
|
Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
|
|
|
|
wb1.close();
|
|
|
|
|
|
|
|
cell = wb2.getSheetAt(0).getRow(0).getCell(0);
|
2009-05-18 15:11:45 -04:00
|
|
|
assertEquals("IF(ROUND(A2*B2*C2,2)>ROUND(B2*D2,2),ROUND(A2*B2*C2,2),ROUND(B2*D2,2))", cell.getCellFormula());
|
2015-09-20 08:02:40 -04:00
|
|
|
wb2.close();
|
2009-05-18 15:11:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bug 21334: "File error: data may have been lost" with a file
|
|
|
|
* that contains macros and this formula:
|
|
|
|
* {=SUM(IF(FREQUENCY(IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),""),IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),""))>0,1))}
|
|
|
|
*/
|
2014-01-10 18:14:03 -05:00
|
|
|
@Test
|
2015-10-12 16:43:42 -04:00
|
|
|
public final void bug21334() throws IOException {
|
2015-09-20 08:02:40 -04:00
|
|
|
Workbook wb1 = _testDataProvider.createWorkbook();
|
|
|
|
Sheet sh = wb1.createSheet();
|
2009-05-18 15:11:45 -04:00
|
|
|
Cell cell = sh.createRow(0).createCell(0);
|
|
|
|
String formula = "SUM(IF(FREQUENCY(IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),\"\"),IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),\"\"))>0,1))";
|
|
|
|
cell.setCellFormula(formula);
|
|
|
|
|
2015-09-20 08:02:40 -04:00
|
|
|
Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
|
|
|
|
wb1.close();
|
|
|
|
Cell cell_sv = wb2.getSheetAt(0).getRow(0).getCell(0);
|
2009-05-18 15:11:45 -04:00
|
|
|
assertEquals(formula, cell_sv.getCellFormula());
|
2015-09-20 08:02:40 -04:00
|
|
|
wb2.close();
|
2009-05-18 15:11:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** another test for the number of unique strings issue
|
|
|
|
*test opening the resulting file in Excel*/
|
2014-01-10 18:14:03 -05:00
|
|
|
@Test
|
2015-10-12 16:43:42 -04:00
|
|
|
public final void bug22568() throws IOException {
|
2009-05-18 15:11:45 -04:00
|
|
|
int r=2000;int c=3;
|
|
|
|
|
2015-09-20 08:02:40 -04:00
|
|
|
Workbook wb1 = _testDataProvider.createWorkbook();
|
|
|
|
Sheet sheet = wb1.createSheet("ExcelTest") ;
|
2009-05-18 15:11:45 -04:00
|
|
|
|
|
|
|
int col_cnt=0, rw_cnt=0 ;
|
|
|
|
|
|
|
|
col_cnt = c;
|
|
|
|
rw_cnt = r;
|
|
|
|
|
|
|
|
Row rw ;
|
|
|
|
rw = sheet.createRow(0) ;
|
|
|
|
//Header row
|
|
|
|
for(int j=0; j<col_cnt; j++){
|
|
|
|
Cell cell = rw.createCell(j) ;
|
|
|
|
cell.setCellValue("Col " + (j+1));
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i=1; i<rw_cnt; i++){
|
|
|
|
rw = sheet.createRow(i) ;
|
|
|
|
for(int j=0; j<col_cnt; j++){
|
|
|
|
Cell cell = rw.createCell(j) ;
|
|
|
|
cell.setCellValue("Row:" + (i+1) + ",Column:" + (j+1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sheet.setDefaultColumnWidth(18) ;
|
|
|
|
|
2015-09-20 08:02:40 -04:00
|
|
|
Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
|
|
|
|
wb1.close();
|
|
|
|
sheet = wb2.getSheetAt(0);
|
2009-05-18 15:11:45 -04:00
|
|
|
rw = sheet.getRow(0);
|
|
|
|
//Header row
|
|
|
|
for(int j=0; j<col_cnt; j++){
|
|
|
|
Cell cell = rw.getCell(j) ;
|
|
|
|
assertEquals("Col " + (j+1), cell.getStringCellValue());
|
|
|
|
}
|
|
|
|
for(int i=1; i<rw_cnt; i++){
|
|
|
|
rw = sheet.getRow(i) ;
|
|
|
|
for(int j=0; j<col_cnt; j++){
|
|
|
|
Cell cell = rw.getCell(j) ;
|
|
|
|
assertEquals("Row:" + (i+1) + ",Column:" + (j+1), cell.getStringCellValue());
|
|
|
|
}
|
|
|
|
}
|
2015-09-20 08:02:40 -04:00
|
|
|
wb2.close();
|
2009-05-18 15:11:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bug 42448: Can't parse SUMPRODUCT(A!C7:A!C67, B8:B68) / B69
|
|
|
|
*/
|
2014-01-10 18:14:03 -05:00
|
|
|
@Test
|
2015-10-12 16:43:42 -04:00
|
|
|
public final void bug42448() throws IOException {
|
2009-12-25 21:17:34 -05:00
|
|
|
Workbook wb = _testDataProvider.createWorkbook();
|
2009-05-18 15:11:45 -04:00
|
|
|
Cell cell = wb.createSheet().createRow(0).createCell(0);
|
|
|
|
cell.setCellFormula("SUMPRODUCT(A!C7:A!C67, B8:B68) / B69");
|
|
|
|
assertTrue("no errors parsing formula", true);
|
2015-09-20 08:02:40 -04:00
|
|
|
wb.close();
|
2009-05-18 15:11:45 -04:00
|
|
|
}
|
|
|
|
|
2014-01-10 18:14:03 -05:00
|
|
|
@Test
|
2015-10-12 16:43:42 -04:00
|
|
|
public void bug18800() throws IOException {
|
2015-09-20 08:02:40 -04:00
|
|
|
Workbook wb1 = _testDataProvider.createWorkbook();
|
|
|
|
wb1.createSheet("TEST");
|
|
|
|
Sheet sheet = wb1.cloneSheet(0);
|
|
|
|
wb1.setSheetName(1,"CLONE");
|
2009-05-18 15:11:45 -04:00
|
|
|
sheet.createRow(0).createCell(0).setCellValue("Test");
|
|
|
|
|
2015-09-20 08:02:40 -04:00
|
|
|
Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
|
|
|
|
wb1.close();
|
|
|
|
sheet = wb2.getSheet("CLONE");
|
2009-05-18 15:11:45 -04:00
|
|
|
Row row = sheet.getRow(0);
|
|
|
|
Cell cell = row.getCell(0);
|
|
|
|
assertEquals("Test", cell.getRichStringCellValue().getString());
|
2015-09-20 08:02:40 -04:00
|
|
|
wb2.close();
|
2009-05-18 15:11:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void addNewSheetWithCellsA1toD4(Workbook book, int sheet) {
|
|
|
|
|
|
|
|
Sheet sht = book .createSheet("s" + sheet);
|
|
|
|
for (int r=0; r < 4; r++) {
|
|
|
|
|
|
|
|
Row row = sht.createRow (r);
|
|
|
|
for (int c=0; c < 4; c++) {
|
|
|
|
|
|
|
|
Cell cel = row.createCell(c);
|
|
|
|
cel.setCellValue(sheet*100 + r*10 + c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-10 18:14:03 -05:00
|
|
|
@Test
|
2015-10-12 16:43:42 -04:00
|
|
|
public void bug43093() throws IOException {
|
2009-12-25 21:17:34 -05:00
|
|
|
Workbook xlw = _testDataProvider.createWorkbook();
|
2009-05-18 15:11:45 -04:00
|
|
|
|
|
|
|
addNewSheetWithCellsA1toD4(xlw, 1);
|
|
|
|
addNewSheetWithCellsA1toD4(xlw, 2);
|
|
|
|
addNewSheetWithCellsA1toD4(xlw, 3);
|
|
|
|
addNewSheetWithCellsA1toD4(xlw, 4);
|
|
|
|
|
|
|
|
Sheet s2 = xlw.getSheet("s2");
|
|
|
|
Row s2r3 = s2.getRow(3);
|
|
|
|
Cell s2E4 = s2r3.createCell(4);
|
|
|
|
s2E4.setCellFormula("SUM(s3!B2:C3)");
|
|
|
|
|
|
|
|
FormulaEvaluator eva = xlw.getCreationHelper().createFormulaEvaluator();
|
|
|
|
double d = eva.evaluate(s2E4).getNumberValue();
|
|
|
|
|
|
|
|
assertEquals(d, (311+312+321+322), 0.0000001);
|
2015-09-20 08:02:40 -04:00
|
|
|
|
|
|
|
xlw.close();
|
2009-05-18 15:11:45 -04:00
|
|
|
}
|
|
|
|
|
2014-01-10 18:14:03 -05:00
|
|
|
@Test
|
2015-10-12 16:43:42 -04:00
|
|
|
public void bug46729_testMaxFunctionArguments() throws IOException {
|
2009-05-18 15:11:45 -04:00
|
|
|
String[] func = {"COUNT", "AVERAGE", "MAX", "MIN", "OR", "SUBTOTAL", "SKEW"};
|
|
|
|
|
2009-12-25 21:17:34 -05:00
|
|
|
SpreadsheetVersion ssVersion = _testDataProvider.getSpreadsheetVersion();
|
|
|
|
Workbook wb = _testDataProvider.createWorkbook();
|
2009-05-18 15:11:45 -04:00
|
|
|
Cell cell = wb.createSheet().createRow(0).createCell(0);
|
|
|
|
|
|
|
|
String fmla;
|
|
|
|
for (String name : func) {
|
|
|
|
|
|
|
|
fmla = createFunction(name, 5);
|
|
|
|
cell.setCellFormula(fmla);
|
|
|
|
|
|
|
|
fmla = createFunction(name, ssVersion.getMaxFunctionArgs());
|
|
|
|
cell.setCellFormula(fmla);
|
|
|
|
|
|
|
|
try {
|
|
|
|
fmla = createFunction(name, ssVersion.getMaxFunctionArgs() + 1);
|
|
|
|
cell.setCellFormula(fmla);
|
|
|
|
fail("Expected FormulaParseException");
|
|
|
|
} catch (RuntimeException e){
|
|
|
|
assertTrue(e.getMessage().startsWith("Too many arguments to function '"+name+"'"));
|
|
|
|
}
|
|
|
|
}
|
2015-09-20 08:02:40 -04:00
|
|
|
wb.close();
|
2009-05-18 15:11:45 -04:00
|
|
|
}
|
|
|
|
|
2009-12-25 21:17:34 -05:00
|
|
|
private static String createFunction(String name, int maxArgs){
|
2009-05-18 15:11:45 -04:00
|
|
|
StringBuffer fmla = new StringBuffer();
|
|
|
|
fmla.append(name);
|
|
|
|
fmla.append("(");
|
|
|
|
for(int i=0; i < maxArgs; i++){
|
|
|
|
if(i > 0) fmla.append(',');
|
|
|
|
fmla.append("A1");
|
|
|
|
}
|
|
|
|
fmla.append(")");
|
|
|
|
return fmla.toString();
|
|
|
|
}
|
2011-06-16 04:57:55 -04:00
|
|
|
|
2014-01-10 18:14:03 -05:00
|
|
|
@Test
|
2015-10-12 16:43:42 -04:00
|
|
|
public final void bug50681_testAutoSize() throws IOException {
|
2011-06-16 04:57:55 -04:00
|
|
|
Workbook wb = _testDataProvider.createWorkbook();
|
2014-12-11 20:43:45 -05:00
|
|
|
BaseTestSheetAutosizeColumn.fixFonts(wb);
|
2011-06-16 04:57:55 -04:00
|
|
|
Sheet sheet = wb.createSheet("Sheet1");
|
|
|
|
Row row = sheet.createRow(0);
|
|
|
|
Cell cell0 = row.createCell(0);
|
|
|
|
|
|
|
|
String longValue = "www.hostname.com, www.hostname.com, " +
|
|
|
|
"www.hostname.com, www.hostname.com, www.hostname.com, " +
|
|
|
|
"www.hostname.com, www.hostname.com, www.hostname.com, " +
|
|
|
|
"www.hostname.com, www.hostname.com, www.hostname.com, " +
|
|
|
|
"www.hostname.com, www.hostname.com, www.hostname.com, " +
|
|
|
|
"www.hostname.com, www.hostname.com, www.hostname.com, www.hostname.com";
|
|
|
|
|
|
|
|
cell0.setCellValue(longValue);
|
|
|
|
|
2015-07-16 05:06:47 -04:00
|
|
|
// autoSize will fail if required fonts are not installed, skip this test then
|
|
|
|
Font font = wb.getFontAt(cell0.getCellStyle().getFontIndex());
|
2015-09-20 08:02:40 -04:00
|
|
|
Assume.assumeTrue("Cannot verify auoSizeColumn() because the necessary Fonts are not installed on this machine: " + font,
|
2015-07-16 05:06:47 -04:00
|
|
|
SheetUtil.canComputeColumnWidht(font));
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2015-07-17 04:42:10 -04:00
|
|
|
assertEquals("Expecting no indentation in this test",
|
|
|
|
0, cell0.getCellStyle().getIndention());
|
2015-09-20 08:02:40 -04:00
|
|
|
assertEquals("Expecting no rotation in this test",
|
2015-07-22 08:43:48 -04:00
|
|
|
0, cell0.getCellStyle().getRotation());
|
2015-07-17 04:42:10 -04:00
|
|
|
|
2015-07-22 16:30:24 -04:00
|
|
|
// check computing size up to a large size
|
2015-07-27 12:08:08 -04:00
|
|
|
// StringBuilder b = new StringBuilder();
|
|
|
|
// for(int i = 0;i < longValue.length()*5;i++) {
|
|
|
|
// b.append("w");
|
|
|
|
// assertTrue("Had zero length starting at length " + i, computeCellWidthFixed(font, b.toString()) > 0);
|
|
|
|
// }
|
2015-07-22 08:43:48 -04:00
|
|
|
double widthManual = computeCellWidthManually(cell0, font);
|
|
|
|
double widthBeforeCell = SheetUtil.getCellWidth(cell0, 8, null, false);
|
|
|
|
double widthBeforeCol = SheetUtil.getColumnWidth(sheet, 0, false);
|
|
|
|
|
2015-09-20 08:02:40 -04:00
|
|
|
String info = widthManual + "/" + widthBeforeCell + "/" + widthBeforeCol + "/" +
|
|
|
|
SheetUtil.canComputeColumnWidht(font) + "/" + computeCellWidthFixed(font, "1") + "/" + computeCellWidthFixed(font, "w") + "/" +
|
2015-07-27 12:08:08 -04:00
|
|
|
computeCellWidthFixed(font, "1w") + "/" + computeCellWidthFixed(font, "0000") + "/" + computeCellWidthFixed(font, longValue);
|
|
|
|
assertTrue("Expected to have cell width > 0 when computing manually, but had " + info, widthManual > 0);
|
|
|
|
assertTrue("Expected to have cell width > 0 BEFORE auto-size, but had " + info, widthBeforeCell > 0);
|
|
|
|
assertTrue("Expected to have column width > 0 BEFORE auto-size, but had " + info, widthBeforeCol > 0);
|
2015-07-17 04:42:10 -04:00
|
|
|
|
|
|
|
sheet.autoSizeColumn(0);
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2015-07-22 08:43:48 -04:00
|
|
|
double width = SheetUtil.getColumnWidth(sheet, 0, false);
|
2015-07-17 04:42:10 -04:00
|
|
|
assertTrue("Expected to have column width > 0 AFTER auto-size, but had " + width, width > 0);
|
|
|
|
width = SheetUtil.getCellWidth(cell0, 8, null, false);
|
|
|
|
assertTrue("Expected to have cell width > 0 AFTER auto-size, but had " + width, width > 0);
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2011-06-16 04:57:55 -04:00
|
|
|
assertEquals(255*256, sheet.getColumnWidth(0)); // maximum column width is 255 characters
|
|
|
|
sheet.setColumnWidth(0, sheet.getColumnWidth(0)); // Bug 506819 reports exception at this point
|
2015-09-20 08:02:40 -04:00
|
|
|
wb.close();
|
2011-06-16 04:57:55 -04:00
|
|
|
}
|
2011-06-25 08:19:49 -04:00
|
|
|
|
2015-07-22 08:43:48 -04:00
|
|
|
private double computeCellWidthManually(Cell cell0, Font font) {
|
2015-09-20 08:02:40 -04:00
|
|
|
final FontRenderContext fontRenderContext = new FontRenderContext(null, true, true);
|
2015-07-22 08:43:48 -04:00
|
|
|
RichTextString rt = cell0.getRichStringCellValue();
|
|
|
|
String[] lines = rt.getString().split("\\n");
|
|
|
|
assertEquals(1, lines.length);
|
|
|
|
String txt = lines[0] + "0";
|
|
|
|
|
|
|
|
AttributedString str = new AttributedString(txt);
|
|
|
|
copyAttributes(font, str, 0, txt.length());
|
|
|
|
|
|
|
|
if (rt.numFormattingRuns() > 0) {
|
|
|
|
// TODO: support rich text fragments
|
|
|
|
}
|
|
|
|
|
|
|
|
TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext);
|
2015-07-22 10:03:11 -04:00
|
|
|
return ((layout.getBounds().getWidth() / 1) / 8);
|
|
|
|
}
|
|
|
|
|
2015-07-22 16:30:24 -04:00
|
|
|
private double computeCellWidthFixed(Font font, String txt) {
|
2015-09-20 08:02:40 -04:00
|
|
|
final FontRenderContext fontRenderContext = new FontRenderContext(null, true, true);
|
2015-07-22 10:03:11 -04:00
|
|
|
AttributedString str = new AttributedString(txt);
|
|
|
|
copyAttributes(font, str, 0, txt.length());
|
|
|
|
|
|
|
|
TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext);
|
2015-07-22 11:26:41 -04:00
|
|
|
return layout.getBounds().getWidth();
|
2015-07-22 08:43:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void copyAttributes(Font font, AttributedString str, int startIdx, int endIdx) {
|
|
|
|
str.addAttribute(TextAttribute.FAMILY, font.getFontName(), startIdx, endIdx);
|
|
|
|
str.addAttribute(TextAttribute.SIZE, (float)font.getFontHeightInPoints());
|
|
|
|
if (font.getBoldweight() == Font.BOLDWEIGHT_BOLD) str.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD, startIdx, endIdx);
|
|
|
|
if (font.getItalic() ) str.addAttribute(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE, startIdx, endIdx);
|
|
|
|
if (font.getUnderline() == Font.U_SINGLE ) str.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, startIdx, endIdx);
|
|
|
|
}
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2011-06-25 08:19:49 -04:00
|
|
|
/**
|
|
|
|
* CreateFreezePane column/row order check
|
|
|
|
*/
|
2014-01-10 18:14:03 -05:00
|
|
|
@Test
|
2015-10-12 16:43:42 -04:00
|
|
|
public void bug49381() throws IOException {
|
2011-06-25 08:19:49 -04:00
|
|
|
Workbook wb = _testDataProvider.createWorkbook();
|
|
|
|
int colSplit = 1;
|
|
|
|
int rowSplit = 2;
|
|
|
|
int leftmostColumn = 3;
|
|
|
|
int topRow = 4;
|
|
|
|
|
|
|
|
Sheet s = wb.createSheet();
|
|
|
|
|
|
|
|
// Populate
|
|
|
|
for(int rn=0; rn<= topRow; rn++) {
|
|
|
|
Row r = s.createRow(rn);
|
|
|
|
for(int cn=0; cn<leftmostColumn; cn++) {
|
|
|
|
Cell c = r.createCell(cn, Cell.CELL_TYPE_NUMERIC);
|
|
|
|
c.setCellValue(100*rn + cn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the Freeze Pane
|
|
|
|
s.createFreezePane(colSplit, rowSplit, leftmostColumn, topRow);
|
|
|
|
PaneInformation paneInfo = s.getPaneInformation();
|
|
|
|
|
|
|
|
// Check it
|
|
|
|
assertEquals(colSplit, paneInfo.getVerticalSplitPosition());
|
|
|
|
assertEquals(rowSplit, paneInfo.getHorizontalSplitPosition());
|
|
|
|
assertEquals(leftmostColumn, paneInfo.getVerticalSplitLeftColumn());
|
|
|
|
assertEquals(topRow, paneInfo.getHorizontalSplitTopRow());
|
|
|
|
|
|
|
|
|
|
|
|
// Now a row only freezepane
|
|
|
|
s.createFreezePane(0, 3);
|
|
|
|
paneInfo = s.getPaneInformation();
|
|
|
|
|
|
|
|
assertEquals(0, paneInfo.getVerticalSplitPosition());
|
|
|
|
assertEquals(3, paneInfo.getHorizontalSplitPosition());
|
|
|
|
assertEquals(0, paneInfo.getVerticalSplitLeftColumn());
|
|
|
|
assertEquals(3, paneInfo.getHorizontalSplitTopRow());
|
|
|
|
|
|
|
|
// Now a column only freezepane
|
|
|
|
s.createFreezePane(4, 0);
|
|
|
|
paneInfo = s.getPaneInformation();
|
|
|
|
|
|
|
|
assertEquals(4, paneInfo.getVerticalSplitPosition());
|
|
|
|
assertEquals(0, paneInfo.getHorizontalSplitPosition());
|
|
|
|
assertEquals(4 , paneInfo.getVerticalSplitLeftColumn());
|
|
|
|
assertEquals(0, paneInfo.getHorizontalSplitTopRow());
|
2015-09-20 08:02:40 -04:00
|
|
|
wb.close();
|
2011-06-25 08:19:49 -04:00
|
|
|
}
|
|
|
|
|
2015-09-20 08:02:40 -04:00
|
|
|
/**
|
2014-10-18 16:10:31 -04:00
|
|
|
* Test hyperlinks
|
|
|
|
* open resulting file in excel, and check that there is a link to Google
|
|
|
|
*/
|
|
|
|
@Test
|
2015-10-12 16:43:42 -04:00
|
|
|
public void bug15353() throws IOException {
|
2014-10-18 16:10:31 -04:00
|
|
|
String hyperlinkF = "HYPERLINK(\"http://google.com\",\"Google\")";
|
2015-09-20 08:02:40 -04:00
|
|
|
|
|
|
|
Workbook wb1 = _testDataProvider.createWorkbook();
|
|
|
|
Sheet sheet = wb1.createSheet("My sheet");
|
2014-10-18 16:10:31 -04:00
|
|
|
|
2015-07-13 08:17:52 -04:00
|
|
|
Row row = sheet.createRow(0);
|
|
|
|
Cell cell = row.createCell(0);
|
2014-10-18 16:10:31 -04:00
|
|
|
cell.setCellFormula(hyperlinkF);
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:10:31 -04:00
|
|
|
assertEquals(hyperlinkF, cell.getCellFormula());
|
|
|
|
|
2015-09-20 08:02:40 -04:00
|
|
|
Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
|
|
|
|
wb1.close();
|
|
|
|
sheet = wb2.getSheet("My Sheet");
|
2015-07-13 08:17:52 -04:00
|
|
|
row = sheet.getRow(0);
|
|
|
|
cell = row.getCell(0);
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:10:31 -04:00
|
|
|
assertEquals(hyperlinkF, cell.getCellFormula());
|
2015-09-20 08:02:40 -04:00
|
|
|
wb2.close();
|
2014-10-18 16:10:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-20 08:02:40 -04:00
|
|
|
* HLookup and VLookup with optional arguments
|
2014-10-18 16:10:31 -04:00
|
|
|
*/
|
|
|
|
@Test
|
2015-10-12 16:43:42 -04:00
|
|
|
public void bug51024() throws IOException {
|
2014-10-18 16:10:31 -04:00
|
|
|
Workbook wb = _testDataProvider.createWorkbook();
|
|
|
|
Sheet s = wb.createSheet();
|
|
|
|
Row r1 = s.createRow(0);
|
|
|
|
Row r2 = s.createRow(1);
|
|
|
|
|
|
|
|
r1.createCell(0).setCellValue("v A1");
|
|
|
|
r2.createCell(0).setCellValue("v A2");
|
|
|
|
r1.createCell(1).setCellValue("v B1");
|
|
|
|
|
|
|
|
Cell c = r1.createCell(4);
|
|
|
|
|
|
|
|
FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
|
|
|
|
|
|
|
|
c.setCellFormula("VLOOKUP(\"v A1\", A1:B2, 1)");
|
|
|
|
assertEquals("v A1", eval.evaluate(c).getStringValue());
|
|
|
|
|
|
|
|
c.setCellFormula("VLOOKUP(\"v A1\", A1:B2, 1, 1)");
|
|
|
|
assertEquals("v A1", eval.evaluate(c).getStringValue());
|
|
|
|
|
|
|
|
c.setCellFormula("VLOOKUP(\"v A1\", A1:B2, 1, )");
|
|
|
|
assertEquals("v A1", eval.evaluate(c).getStringValue());
|
|
|
|
|
|
|
|
|
|
|
|
c.setCellFormula("HLOOKUP(\"v A1\", A1:B2, 1)");
|
|
|
|
assertEquals("v A1", eval.evaluate(c).getStringValue());
|
|
|
|
|
|
|
|
c.setCellFormula("HLOOKUP(\"v A1\", A1:B2, 1, 1)");
|
|
|
|
assertEquals("v A1", eval.evaluate(c).getStringValue());
|
|
|
|
|
|
|
|
c.setCellFormula("HLOOKUP(\"v A1\", A1:B2, 1, )");
|
|
|
|
assertEquals("v A1", eval.evaluate(c).getStringValue());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
|
|
|
wb.close();
|
2014-10-18 16:10:31 -04:00
|
|
|
}
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:10:31 -04:00
|
|
|
@Test
|
2015-10-12 16:43:42 -04:00
|
|
|
public void stackoverflow23114397() throws IOException {
|
2014-10-18 16:10:31 -04:00
|
|
|
Workbook wb = _testDataProvider.createWorkbook();
|
|
|
|
DataFormat format = wb.getCreationHelper().createDataFormat();
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:10:31 -04:00
|
|
|
// How close the sizing should be, given that not all
|
|
|
|
// systems will have quite the same fonts on them
|
|
|
|
float fontAccuracy = 0.22f;
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:10:31 -04:00
|
|
|
// x%
|
|
|
|
CellStyle iPercent = wb.createCellStyle();
|
|
|
|
iPercent.setDataFormat(format.getFormat("0%"));
|
|
|
|
// x.x%
|
|
|
|
CellStyle d1Percent = wb.createCellStyle();
|
|
|
|
d1Percent.setDataFormat(format.getFormat("0.0%"));
|
|
|
|
// x.xx%
|
|
|
|
CellStyle d2Percent = wb.createCellStyle();
|
|
|
|
d2Percent.setDataFormat(format.getFormat("0.00%"));
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:10:31 -04:00
|
|
|
Sheet s = wb.createSheet();
|
|
|
|
Row r1 = s.createRow(0);
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:10:31 -04:00
|
|
|
for (int i=0; i<3; i++) {
|
|
|
|
r1.createCell(i, Cell.CELL_TYPE_NUMERIC).setCellValue(0);
|
|
|
|
}
|
|
|
|
for (int i=3; i<6; i++) {
|
|
|
|
r1.createCell(i, Cell.CELL_TYPE_NUMERIC).setCellValue(1);
|
|
|
|
}
|
|
|
|
for (int i=6; i<9; i++) {
|
|
|
|
r1.createCell(i, Cell.CELL_TYPE_NUMERIC).setCellValue(0.12345);
|
|
|
|
}
|
|
|
|
for (int i=9; i<12; i++) {
|
|
|
|
r1.createCell(i, Cell.CELL_TYPE_NUMERIC).setCellValue(1.2345);
|
|
|
|
}
|
|
|
|
for (int i=0; i<12; i+=3) {
|
|
|
|
r1.getCell(i+0).setCellStyle(iPercent);
|
|
|
|
r1.getCell(i+1).setCellStyle(d1Percent);
|
|
|
|
r1.getCell(i+2).setCellStyle(d2Percent);
|
|
|
|
}
|
|
|
|
for (int i=0; i<12; i++) {
|
|
|
|
s.autoSizeColumn(i);
|
|
|
|
}
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:10:31 -04:00
|
|
|
// Check the 0(.00)% ones
|
|
|
|
assertAlmostEquals(980, s.getColumnWidth(0), fontAccuracy);
|
|
|
|
assertAlmostEquals(1400, s.getColumnWidth(1), fontAccuracy);
|
|
|
|
assertAlmostEquals(1700, s.getColumnWidth(2), fontAccuracy);
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:10:31 -04:00
|
|
|
// Check the 100(.00)% ones
|
|
|
|
assertAlmostEquals(1500, s.getColumnWidth(3), fontAccuracy);
|
|
|
|
assertAlmostEquals(1950, s.getColumnWidth(4), fontAccuracy);
|
|
|
|
assertAlmostEquals(2225, s.getColumnWidth(5), fontAccuracy);
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:10:31 -04:00
|
|
|
// Check the 12(.34)% ones
|
|
|
|
assertAlmostEquals(1225, s.getColumnWidth(6), fontAccuracy);
|
|
|
|
assertAlmostEquals(1650, s.getColumnWidth(7), fontAccuracy);
|
|
|
|
assertAlmostEquals(1950, s.getColumnWidth(8), fontAccuracy);
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:10:31 -04:00
|
|
|
// Check the 123(.45)% ones
|
|
|
|
assertAlmostEquals(1500, s.getColumnWidth(9), fontAccuracy);
|
|
|
|
assertAlmostEquals(1950, s.getColumnWidth(10), fontAccuracy);
|
|
|
|
assertAlmostEquals(2225, s.getColumnWidth(11), fontAccuracy);
|
2015-09-20 08:02:40 -04:00
|
|
|
|
|
|
|
wb.close();
|
2014-10-18 16:10:31 -04:00
|
|
|
}
|
2015-06-17 10:53:38 -04:00
|
|
|
|
2014-10-18 16:37:56 -04:00
|
|
|
/**
|
2015-09-20 08:02:40 -04:00
|
|
|
* =ISNUMBER(SEARCH("AM",A1)) evaluation
|
2014-10-18 16:37:56 -04:00
|
|
|
*/
|
2014-10-18 16:53:50 -04:00
|
|
|
@Test
|
2015-10-12 16:43:42 -04:00
|
|
|
public void stackoverflow26437323() throws IOException {
|
2014-10-18 16:37:56 -04:00
|
|
|
Workbook wb = _testDataProvider.createWorkbook();
|
|
|
|
Sheet s = wb.createSheet();
|
|
|
|
Row r1 = s.createRow(0);
|
|
|
|
Row r2 = s.createRow(1);
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:37:56 -04:00
|
|
|
// A1 is a number
|
|
|
|
r1.createCell(0).setCellValue(1.1);
|
|
|
|
// B1 is a string, with the wanted text in it
|
|
|
|
r1.createCell(1).setCellValue("This is text with AM in it");
|
|
|
|
// C1 is a string, with different text
|
|
|
|
r1.createCell(2).setCellValue("This some other text");
|
|
|
|
// D1 is a blank cell
|
|
|
|
r1.createCell(3, Cell.CELL_TYPE_BLANK);
|
|
|
|
// E1 is null
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:37:56 -04:00
|
|
|
// A2 will hold our test formulas
|
|
|
|
Cell cf = r2.createCell(0, Cell.CELL_TYPE_FORMULA);
|
2015-09-20 08:02:40 -04:00
|
|
|
|
|
|
|
|
2014-10-18 16:37:56 -04:00
|
|
|
// First up, check that TRUE and ISLOGICAL both behave
|
|
|
|
cf.setCellFormula("TRUE()");
|
|
|
|
cf = evaluateCell(wb, cf);
|
|
|
|
assertEquals(true, cf.getBooleanCellValue());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:37:56 -04:00
|
|
|
cf.setCellFormula("ISLOGICAL(TRUE())");
|
|
|
|
cf = evaluateCell(wb, cf);
|
|
|
|
assertEquals(true, cf.getBooleanCellValue());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:37:56 -04:00
|
|
|
cf.setCellFormula("ISLOGICAL(4)");
|
|
|
|
cf = evaluateCell(wb, cf);
|
|
|
|
assertEquals(false, cf.getBooleanCellValue());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
|
|
|
|
2014-10-18 16:37:56 -04:00
|
|
|
// Now, check ISNUMBER / ISTEXT / ISNONTEXT
|
2014-10-18 16:53:50 -04:00
|
|
|
cf.setCellFormula("ISNUMBER(A1)");
|
|
|
|
cf = evaluateCell(wb, cf);
|
|
|
|
assertEquals(true, cf.getBooleanCellValue());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:53:50 -04:00
|
|
|
cf.setCellFormula("ISNUMBER(B1)");
|
|
|
|
cf = evaluateCell(wb, cf);
|
|
|
|
assertEquals(false, cf.getBooleanCellValue());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:53:50 -04:00
|
|
|
cf.setCellFormula("ISNUMBER(C1)");
|
|
|
|
cf = evaluateCell(wb, cf);
|
|
|
|
assertEquals(false, cf.getBooleanCellValue());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:53:50 -04:00
|
|
|
cf.setCellFormula("ISNUMBER(D1)");
|
|
|
|
cf = evaluateCell(wb, cf);
|
|
|
|
assertEquals(false, cf.getBooleanCellValue());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:53:50 -04:00
|
|
|
cf.setCellFormula("ISNUMBER(E1)");
|
|
|
|
cf = evaluateCell(wb, cf);
|
|
|
|
assertEquals(false, cf.getBooleanCellValue());
|
|
|
|
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:53:50 -04:00
|
|
|
cf.setCellFormula("ISTEXT(A1)");
|
|
|
|
cf = evaluateCell(wb, cf);
|
|
|
|
assertEquals(false, cf.getBooleanCellValue());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:53:50 -04:00
|
|
|
cf.setCellFormula("ISTEXT(B1)");
|
|
|
|
cf = evaluateCell(wb, cf);
|
|
|
|
assertEquals(true, cf.getBooleanCellValue());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:53:50 -04:00
|
|
|
cf.setCellFormula("ISTEXT(C1)");
|
|
|
|
cf = evaluateCell(wb, cf);
|
|
|
|
assertEquals(true, cf.getBooleanCellValue());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:53:50 -04:00
|
|
|
cf.setCellFormula("ISTEXT(D1)");
|
|
|
|
cf = evaluateCell(wb, cf);
|
|
|
|
assertEquals(false, cf.getBooleanCellValue());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:53:50 -04:00
|
|
|
cf.setCellFormula("ISTEXT(E1)");
|
|
|
|
cf = evaluateCell(wb, cf);
|
|
|
|
assertEquals(false, cf.getBooleanCellValue());
|
|
|
|
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:53:50 -04:00
|
|
|
cf.setCellFormula("ISNONTEXT(A1)");
|
|
|
|
cf = evaluateCell(wb, cf);
|
|
|
|
assertEquals(true, cf.getBooleanCellValue());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:53:50 -04:00
|
|
|
cf.setCellFormula("ISNONTEXT(B1)");
|
|
|
|
cf = evaluateCell(wb, cf);
|
|
|
|
assertEquals(false, cf.getBooleanCellValue());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:53:50 -04:00
|
|
|
cf.setCellFormula("ISNONTEXT(C1)");
|
|
|
|
cf = evaluateCell(wb, cf);
|
|
|
|
assertEquals(false, cf.getBooleanCellValue());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:53:50 -04:00
|
|
|
cf.setCellFormula("ISNONTEXT(D1)");
|
|
|
|
cf = evaluateCell(wb, cf);
|
|
|
|
assertEquals(true, cf.getBooleanCellValue());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:53:50 -04:00
|
|
|
cf.setCellFormula("ISNONTEXT(E1)");
|
|
|
|
cf = evaluateCell(wb, cf);
|
|
|
|
assertEquals(true, cf.getBooleanCellValue()); // Blank and Null the same
|
|
|
|
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:37:56 -04:00
|
|
|
// Next up, SEARCH on its own
|
2014-10-18 16:53:50 -04:00
|
|
|
cf.setCellFormula("SEARCH(\"am\", A1)");
|
|
|
|
cf = evaluateCell(wb, cf);
|
2015-05-19 09:13:09 -04:00
|
|
|
assertEquals(FormulaError.VALUE.getCode(), cf.getErrorCellValue());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:53:50 -04:00
|
|
|
cf.setCellFormula("SEARCH(\"am\", B1)");
|
|
|
|
cf = evaluateCell(wb, cf);
|
|
|
|
assertEquals(19, (int)cf.getNumericCellValue());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:53:50 -04:00
|
|
|
cf.setCellFormula("SEARCH(\"am\", C1)");
|
|
|
|
cf = evaluateCell(wb, cf);
|
2015-05-19 09:13:09 -04:00
|
|
|
assertEquals(FormulaError.VALUE.getCode(), cf.getErrorCellValue());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:53:50 -04:00
|
|
|
cf.setCellFormula("SEARCH(\"am\", D1)");
|
|
|
|
cf = evaluateCell(wb, cf);
|
2015-05-19 09:13:09 -04:00
|
|
|
assertEquals(FormulaError.VALUE.getCode(), cf.getErrorCellValue());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
|
|
|
|
2014-10-18 16:37:56 -04:00
|
|
|
// Finally, bring it all together
|
2014-10-18 16:53:50 -04:00
|
|
|
cf.setCellFormula("ISNUMBER(SEARCH(\"am\", A1))");
|
|
|
|
cf = evaluateCell(wb, cf);
|
|
|
|
assertEquals(false, cf.getBooleanCellValue());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:53:50 -04:00
|
|
|
cf.setCellFormula("ISNUMBER(SEARCH(\"am\", B1))");
|
|
|
|
cf = evaluateCell(wb, cf);
|
|
|
|
assertEquals(true, cf.getBooleanCellValue());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:53:50 -04:00
|
|
|
cf.setCellFormula("ISNUMBER(SEARCH(\"am\", C1))");
|
|
|
|
cf = evaluateCell(wb, cf);
|
|
|
|
assertEquals(false, cf.getBooleanCellValue());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:53:50 -04:00
|
|
|
cf.setCellFormula("ISNUMBER(SEARCH(\"am\", D1))");
|
|
|
|
cf = evaluateCell(wb, cf);
|
|
|
|
assertEquals(false, cf.getBooleanCellValue());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:53:50 -04:00
|
|
|
cf.setCellFormula("ISNUMBER(SEARCH(\"am\", E1))");
|
|
|
|
cf = evaluateCell(wb, cf);
|
2015-09-20 08:02:40 -04:00
|
|
|
assertEquals(false, cf.getBooleanCellValue());
|
|
|
|
|
|
|
|
wb.close();
|
2014-10-18 16:37:56 -04:00
|
|
|
}
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-18 16:37:56 -04:00
|
|
|
private Cell evaluateCell(Workbook wb, Cell c) {
|
|
|
|
Sheet s = c.getSheet();
|
2014-10-18 16:53:50 -04:00
|
|
|
wb.getCreationHelper().createFormulaEvaluator().evaluateFormulaCell(c);
|
2014-10-18 16:37:56 -04:00
|
|
|
return s.getRow(c.getRowIndex()).getCell(c.getColumnIndex());
|
|
|
|
}
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-20 19:22:02 -04:00
|
|
|
/**
|
|
|
|
* Should be able to write then read formulas with references
|
|
|
|
* to cells in other files, eg '[refs/airport.xls]Sheet1'!$A$2
|
|
|
|
* or 'http://192.168.1.2/[blank.xls]Sheet1'!$A$1 .
|
|
|
|
* Additionally, if a reference to that file is provided, it should
|
|
|
|
* be possible to evaluate them too
|
|
|
|
* TODO Fix this to evaluate for XSSF
|
|
|
|
* TODO Fix this to work at all for HSSF
|
|
|
|
*/
|
2015-07-13 08:17:52 -04:00
|
|
|
@Ignore("Fix this to evaluate for XSSF, Fix this to work at all for HSSF")
|
|
|
|
@Test
|
2015-10-12 16:43:42 -04:00
|
|
|
public void bug46670() throws IOException {
|
2015-09-20 08:02:40 -04:00
|
|
|
Workbook wb1 = _testDataProvider.createWorkbook();
|
|
|
|
Sheet s = wb1.createSheet();
|
2014-10-20 19:22:02 -04:00
|
|
|
Row r1 = s.createRow(0);
|
2015-09-20 08:02:40 -04:00
|
|
|
|
|
|
|
|
2014-10-20 19:22:02 -04:00
|
|
|
// References to try
|
2015-08-19 09:36:08 -04:00
|
|
|
String ext = _testDataProvider.getStandardFileNameExtension();
|
2014-10-20 19:22:02 -04:00
|
|
|
String refLocal = "'[test."+ext+"]Sheet1'!$A$2";
|
|
|
|
String refHttp = "'[http://example.com/test."+ext+"]Sheet1'!$A$2";
|
|
|
|
String otherCellText = "In Another Workbook";
|
|
|
|
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-20 19:22:02 -04:00
|
|
|
// Create the references
|
|
|
|
Cell c1 = r1.createCell(0, Cell.CELL_TYPE_FORMULA);
|
|
|
|
c1.setCellFormula(refLocal);
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-20 19:22:02 -04:00
|
|
|
Cell c2 = r1.createCell(1, Cell.CELL_TYPE_FORMULA);
|
|
|
|
c2.setCellFormula(refHttp);
|
2015-09-20 08:02:40 -04:00
|
|
|
|
|
|
|
|
2014-10-20 19:22:02 -04:00
|
|
|
// Check they were set correctly
|
|
|
|
assertEquals(refLocal, c1.getCellFormula());
|
|
|
|
assertEquals(refHttp, c2.getCellFormula());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
|
|
|
|
2014-10-20 19:22:02 -04:00
|
|
|
// Reload, and ensure they were serialised and read correctly
|
2015-09-20 08:02:40 -04:00
|
|
|
Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
|
|
|
|
wb1.close();
|
|
|
|
s = wb2.getSheetAt(0);
|
2014-10-20 19:22:02 -04:00
|
|
|
r1 = s.getRow(0);
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-20 19:22:02 -04:00
|
|
|
c1 = r1.getCell(0);
|
|
|
|
c2 = r1.getCell(1);
|
|
|
|
assertEquals(refLocal, c1.getCellFormula());
|
|
|
|
assertEquals(refHttp, c2.getCellFormula());
|
|
|
|
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2015-07-13 08:17:52 -04:00
|
|
|
// Try to evaluate, without giving a way to get at the other file
|
2014-10-20 19:22:02 -04:00
|
|
|
try {
|
2015-09-20 08:02:40 -04:00
|
|
|
evaluateCell(wb2, c1);
|
2014-10-20 19:22:02 -04:00
|
|
|
fail("Shouldn't be able to evaluate without the other file");
|
|
|
|
} catch (Exception e) {}
|
|
|
|
try {
|
2015-09-20 08:02:40 -04:00
|
|
|
evaluateCell(wb2, c2);
|
2014-10-20 19:22:02 -04:00
|
|
|
fail("Shouldn't be able to evaluate without the other file");
|
|
|
|
} catch (Exception e) {}
|
2015-09-20 08:02:40 -04:00
|
|
|
|
|
|
|
|
2014-10-20 19:22:02 -04:00
|
|
|
// Set up references to the other file
|
2015-09-20 08:02:40 -04:00
|
|
|
Workbook wb3 = _testDataProvider.createWorkbook();
|
|
|
|
wb3.createSheet().createRow(1).createCell(0).setCellValue(otherCellText);
|
|
|
|
|
2014-10-20 19:22:02 -04:00
|
|
|
Map<String,FormulaEvaluator> evaluators = new HashMap<String, FormulaEvaluator>();
|
2015-09-20 08:02:40 -04:00
|
|
|
evaluators.put(refLocal, wb3.getCreationHelper().createFormulaEvaluator());
|
|
|
|
evaluators.put(refHttp, wb3.getCreationHelper().createFormulaEvaluator());
|
|
|
|
|
|
|
|
FormulaEvaluator evaluator = wb2.getCreationHelper().createFormulaEvaluator();
|
2014-10-20 19:22:02 -04:00
|
|
|
evaluator.setupReferencedWorkbooks(evaluators);
|
2015-09-20 08:02:40 -04:00
|
|
|
|
|
|
|
|
2014-10-20 19:22:02 -04:00
|
|
|
// Try to evaluate, with the other file
|
|
|
|
evaluator.evaluateFormulaCell(c1);
|
|
|
|
evaluator.evaluateFormulaCell(c2);
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2014-10-20 19:22:02 -04:00
|
|
|
assertEquals(otherCellText, c1.getStringCellValue());
|
|
|
|
assertEquals(otherCellText, c2.getStringCellValue());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
|
|
|
wb3.close();
|
|
|
|
wb2.close();
|
2014-10-20 19:22:02 -04:00
|
|
|
}
|
2015-05-19 09:13:09 -04:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void test56574OverwriteExistingRow() throws IOException {
|
|
|
|
Workbook wb = _testDataProvider.createWorkbook();
|
|
|
|
Sheet sheet = wb.createSheet();
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2015-05-19 09:13:09 -04:00
|
|
|
{ // create the Formula-Cell
|
|
|
|
Row row = sheet.createRow(0);
|
|
|
|
Cell cell = row.createCell(0);
|
|
|
|
cell.setCellFormula("A2");
|
|
|
|
}
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2015-05-19 09:13:09 -04:00
|
|
|
{ // check that it is there now
|
|
|
|
Row row = sheet.getRow(0);
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2015-05-19 09:13:09 -04:00
|
|
|
/* CTCell[] cArray = ((XSSFRow)row).getCTRow().getCArray();
|
|
|
|
assertEquals(1, cArray.length);*/
|
|
|
|
|
|
|
|
Cell cell = row.getCell(0);
|
|
|
|
assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType());
|
|
|
|
}
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2015-05-19 09:13:09 -04:00
|
|
|
{ // overwrite the row
|
|
|
|
Row row = sheet.createRow(0);
|
|
|
|
assertNotNull(row);
|
|
|
|
}
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2015-05-19 09:13:09 -04:00
|
|
|
{ // creating a row in place of another should remove the existing data,
|
|
|
|
// check that the cell is gone now
|
|
|
|
Row row = sheet.getRow(0);
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2015-05-19 09:13:09 -04:00
|
|
|
/*CTCell[] cArray = ((XSSFRow)row).getCTRow().getCArray();
|
|
|
|
assertEquals(0, cArray.length);*/
|
|
|
|
|
|
|
|
Cell cell = row.getCell(0);
|
|
|
|
assertNull(cell);
|
|
|
|
}
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2015-05-19 09:13:09 -04:00
|
|
|
// the calculation chain in XSSF is empty in a newly created workbook, so we cannot check if it is correctly updated
|
|
|
|
/*assertNull(((XSSFWorkbook)wb).getCalculationChain());
|
|
|
|
assertNotNull(((XSSFWorkbook)wb).getCalculationChain().getCTCalcChain());
|
|
|
|
assertNotNull(((XSSFWorkbook)wb).getCalculationChain().getCTCalcChain().getCArray());
|
|
|
|
assertEquals(0, ((XSSFWorkbook)wb).getCalculationChain().getCTCalcChain().getCArray().length);*/
|
|
|
|
|
|
|
|
wb.close();
|
|
|
|
}
|
2015-06-17 10:53:38 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* With HSSF, if you create a font, don't change it, and
|
2015-09-20 08:02:40 -04:00
|
|
|
* create a 2nd, you really do get two fonts that you
|
2015-06-17 10:53:38 -04:00
|
|
|
* can alter as and when you want.
|
|
|
|
* With XSSF, that wasn't the case, but this verfies
|
|
|
|
* that it now is again
|
|
|
|
*/
|
|
|
|
@Test
|
2015-10-12 16:43:42 -04:00
|
|
|
public void bug48718() throws IOException {
|
2015-06-17 10:53:38 -04:00
|
|
|
Workbook wb = _testDataProvider.createWorkbook();
|
|
|
|
int startingFonts = wb instanceof HSSFWorkbook ? 4 : 1;
|
|
|
|
|
|
|
|
assertEquals(startingFonts, wb.getNumberOfFonts());
|
|
|
|
|
|
|
|
// Get a font, and slightly change it
|
|
|
|
Font a = wb.createFont();
|
|
|
|
assertEquals(startingFonts+1, wb.getNumberOfFonts());
|
|
|
|
a.setFontHeightInPoints((short)23);
|
|
|
|
assertEquals(startingFonts+1, wb.getNumberOfFonts());
|
|
|
|
|
|
|
|
// Get two more, unchanged
|
|
|
|
/*Font b =*/ wb.createFont();
|
|
|
|
assertEquals(startingFonts+2, wb.getNumberOfFonts());
|
|
|
|
/*Font c =*/ wb.createFont();
|
|
|
|
assertEquals(startingFonts+3, wb.getNumberOfFonts());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
|
|
|
wb.close();
|
2015-06-17 10:53:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2015-10-12 16:43:42 -04:00
|
|
|
public void bug57430() throws IOException {
|
2015-06-17 10:53:38 -04:00
|
|
|
Workbook wb = _testDataProvider.createWorkbook();
|
2015-09-20 08:02:40 -04:00
|
|
|
wb.createSheet("Sheet1");
|
2015-06-17 10:53:38 -04:00
|
|
|
|
2015-09-20 08:02:40 -04:00
|
|
|
Name name1 = wb.createName();
|
|
|
|
name1.setNameName("FMLA");
|
|
|
|
name1.setRefersToFormula("Sheet1!$B$3");
|
|
|
|
wb.close();
|
2015-06-17 10:53:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void bug56981() throws IOException {
|
|
|
|
Workbook wb = _testDataProvider.createWorkbook();
|
|
|
|
CellStyle vertTop = wb.createCellStyle();
|
|
|
|
vertTop.setVerticalAlignment(CellStyle.VERTICAL_TOP);
|
|
|
|
CellStyle vertBottom = wb.createCellStyle();
|
|
|
|
vertBottom.setVerticalAlignment(CellStyle.VERTICAL_BOTTOM);
|
|
|
|
Sheet sheet = wb.createSheet("Sheet 1");
|
|
|
|
Row row = sheet.createRow(0);
|
|
|
|
Cell top = row.createCell(0);
|
|
|
|
Cell bottom = row.createCell(1);
|
|
|
|
top.setCellValue("Top");
|
|
|
|
top.setCellStyle(vertTop); // comment this out to get all bottom-aligned
|
|
|
|
// cells
|
|
|
|
bottom.setCellValue("Bottom");
|
|
|
|
bottom.setCellStyle(vertBottom);
|
|
|
|
row.setHeightInPoints(85.75f); // make it obvious
|
|
|
|
|
|
|
|
/*FileOutputStream out = new FileOutputStream("c:\\temp\\56981.xlsx");
|
|
|
|
try {
|
|
|
|
wb.write(out);
|
|
|
|
} finally {
|
|
|
|
out.close();
|
|
|
|
}*/
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2015-06-17 10:53:38 -04:00
|
|
|
wb.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void test57973() throws IOException {
|
|
|
|
Workbook wb = _testDataProvider.createWorkbook();
|
|
|
|
|
|
|
|
CreationHelper factory = wb.getCreationHelper();
|
|
|
|
|
|
|
|
Sheet sheet = wb.createSheet();
|
|
|
|
Drawing drawing = sheet.createDrawingPatriarch();
|
|
|
|
ClientAnchor anchor = factory.createClientAnchor();
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2015-06-17 10:53:38 -04:00
|
|
|
Cell cell0 = sheet.createRow(0).createCell(0);
|
|
|
|
cell0.setCellValue("Cell0");
|
|
|
|
|
|
|
|
Comment comment0 = drawing.createCellComment(anchor);
|
|
|
|
RichTextString str0 = factory.createRichTextString("Hello, World1!");
|
|
|
|
comment0.setString(str0);
|
|
|
|
comment0.setAuthor("Apache POI");
|
|
|
|
cell0.setCellComment(comment0);
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2015-06-17 10:53:38 -04:00
|
|
|
anchor = factory.createClientAnchor();
|
|
|
|
anchor.setCol1(1);
|
|
|
|
anchor.setCol2(1);
|
|
|
|
anchor.setRow1(1);
|
|
|
|
anchor.setRow2(1);
|
|
|
|
Cell cell1 = sheet.createRow(3).createCell(5);
|
|
|
|
cell1.setCellValue("F4");
|
|
|
|
Comment comment1 = drawing.createCellComment(anchor);
|
|
|
|
RichTextString str1 = factory.createRichTextString("Hello, World2!");
|
|
|
|
comment1.setString(str1);
|
|
|
|
comment1.setAuthor("Apache POI");
|
|
|
|
cell1.setCellComment(comment1);
|
|
|
|
|
|
|
|
Cell cell2 = sheet.createRow(2).createCell(2);
|
|
|
|
cell2.setCellValue("C3");
|
|
|
|
|
|
|
|
anchor = factory.createClientAnchor();
|
|
|
|
anchor.setCol1(2);
|
|
|
|
anchor.setCol2(2);
|
|
|
|
anchor.setRow1(2);
|
|
|
|
anchor.setRow2(2);
|
|
|
|
|
|
|
|
Comment comment2 = drawing.createCellComment(anchor);
|
|
|
|
RichTextString str2 = factory.createRichTextString("XSSF can set cell comments");
|
|
|
|
//apply custom font to the text in the comment
|
|
|
|
Font font = wb.createFont();
|
|
|
|
font.setFontName("Arial");
|
|
|
|
font.setFontHeightInPoints((short)14);
|
|
|
|
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
|
|
|
|
font.setColor(IndexedColors.RED.getIndex());
|
|
|
|
str2.applyFont(font);
|
|
|
|
|
|
|
|
comment2.setString(str2);
|
|
|
|
comment2.setAuthor("Apache POI");
|
|
|
|
comment2.setColumn(2);
|
|
|
|
comment2.setRow(2);
|
|
|
|
|
|
|
|
wb.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensures that XSSF and HSSF agree with each other,
|
|
|
|
* and with the docs on when fetching the wrong
|
|
|
|
* kind of value from a Formula cell
|
|
|
|
*/
|
|
|
|
@Test
|
2015-10-12 16:43:42 -04:00
|
|
|
public void bug47815() throws IOException {
|
2015-06-17 10:53:38 -04:00
|
|
|
Workbook wb = _testDataProvider.createWorkbook();
|
|
|
|
Sheet s = wb.createSheet();
|
|
|
|
Row r = s.createRow(0);
|
|
|
|
|
|
|
|
// Setup
|
|
|
|
Cell cn = r.createCell(0, Cell.CELL_TYPE_NUMERIC);
|
|
|
|
cn.setCellValue(1.2);
|
|
|
|
Cell cs = r.createCell(1, Cell.CELL_TYPE_STRING);
|
|
|
|
cs.setCellValue("Testing");
|
|
|
|
|
|
|
|
Cell cfn = r.createCell(2, Cell.CELL_TYPE_FORMULA);
|
2015-09-20 08:02:40 -04:00
|
|
|
cfn.setCellFormula("A1");
|
2015-06-17 10:53:38 -04:00
|
|
|
Cell cfs = r.createCell(3, Cell.CELL_TYPE_FORMULA);
|
|
|
|
cfs.setCellFormula("B1");
|
|
|
|
|
|
|
|
FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
|
|
|
|
assertEquals(Cell.CELL_TYPE_NUMERIC, fe.evaluate(cfn).getCellType());
|
|
|
|
assertEquals(Cell.CELL_TYPE_STRING, fe.evaluate(cfs).getCellType());
|
|
|
|
fe.evaluateFormulaCell(cfn);
|
|
|
|
fe.evaluateFormulaCell(cfs);
|
|
|
|
|
|
|
|
// Now test
|
|
|
|
assertEquals(Cell.CELL_TYPE_NUMERIC, cn.getCellType());
|
|
|
|
assertEquals(Cell.CELL_TYPE_STRING, cs.getCellType());
|
|
|
|
assertEquals(Cell.CELL_TYPE_FORMULA, cfn.getCellType());
|
|
|
|
assertEquals(Cell.CELL_TYPE_NUMERIC, cfn.getCachedFormulaResultType());
|
|
|
|
assertEquals(Cell.CELL_TYPE_FORMULA, cfs.getCellType());
|
|
|
|
assertEquals(Cell.CELL_TYPE_STRING, cfs.getCachedFormulaResultType());
|
|
|
|
|
|
|
|
// Different ways of retrieving
|
|
|
|
assertEquals(1.2, cn.getNumericCellValue(), 0);
|
|
|
|
try {
|
|
|
|
cn.getRichStringCellValue();
|
|
|
|
fail();
|
|
|
|
} catch(IllegalStateException e) {}
|
|
|
|
|
|
|
|
assertEquals("Testing", cs.getStringCellValue());
|
|
|
|
try {
|
|
|
|
cs.getNumericCellValue();
|
|
|
|
fail();
|
|
|
|
} catch(IllegalStateException e) {}
|
|
|
|
|
|
|
|
assertEquals(1.2, cfn.getNumericCellValue(), 0);
|
|
|
|
try {
|
|
|
|
cfn.getRichStringCellValue();
|
|
|
|
fail();
|
|
|
|
} catch(IllegalStateException e) {}
|
|
|
|
|
|
|
|
assertEquals("Testing", cfs.getStringCellValue());
|
|
|
|
try {
|
|
|
|
cfs.getNumericCellValue();
|
|
|
|
fail();
|
|
|
|
} catch(IllegalStateException e) {}
|
2015-09-20 08:02:40 -04:00
|
|
|
|
|
|
|
wb.close();
|
2015-06-17 10:53:38 -04:00
|
|
|
}
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2015-07-13 08:17:52 -04:00
|
|
|
@Test
|
2015-10-12 16:43:42 -04:00
|
|
|
public void test58113() throws IOException {
|
2015-07-13 08:17:52 -04:00
|
|
|
Workbook wb = _testDataProvider.createWorkbook();
|
|
|
|
Sheet sheet = wb.createSheet( "Test" );
|
|
|
|
|
|
|
|
Row row = sheet.createRow(0);
|
|
|
|
|
|
|
|
Cell cell = row.createCell(0);
|
2015-09-20 08:02:40 -04:00
|
|
|
// verify that null-values can be set, this was possible up to 3.11, but broken in 3.12
|
2015-07-13 08:17:52 -04:00
|
|
|
cell.setCellValue((String)null);
|
|
|
|
String value = cell.getStringCellValue();
|
|
|
|
assertTrue("HSSF will currently return empty string, XSSF/SXSSF will return null, but had: " + value,
|
|
|
|
value == null || value.length() == 0);
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2015-07-13 08:17:52 -04:00
|
|
|
cell = row.createCell(1);
|
2015-09-20 08:02:40 -04:00
|
|
|
// also verify that setting formulas to null works
|
2015-07-13 08:17:52 -04:00
|
|
|
cell.setCellType(Cell.CELL_TYPE_FORMULA);
|
|
|
|
cell.setCellValue((String)null);
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2015-07-13 08:17:52 -04:00
|
|
|
wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
|
|
|
|
|
|
|
|
value = cell.getStringCellValue();
|
|
|
|
assertTrue("HSSF will currently return empty string, XSSF/SXSSF will return null, but had: " + value,
|
|
|
|
value == null || value.length() == 0);
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2015-07-13 08:17:52 -04:00
|
|
|
// set some value
|
|
|
|
cell.setCellType(Cell.CELL_TYPE_STRING);
|
|
|
|
cell.setCellValue("somevalue");
|
|
|
|
|
|
|
|
value = cell.getStringCellValue();
|
|
|
|
assertTrue("can set value afterwards: " + value,
|
|
|
|
value.equals("somevalue"));
|
|
|
|
|
2015-09-20 08:02:40 -04:00
|
|
|
// verify that the null-value is actually set even if there was some value in the cell before
|
2015-07-13 08:17:52 -04:00
|
|
|
cell.setCellValue((String)null);
|
|
|
|
value = cell.getStringCellValue();
|
|
|
|
assertTrue("HSSF will currently return empty string, XSSF/SXSSF will return null, but had: " + value,
|
|
|
|
value == null || value.length() == 0);
|
2015-09-20 08:02:40 -04:00
|
|
|
|
|
|
|
wb.close();
|
2015-07-13 08:17:52 -04:00
|
|
|
}
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2015-07-31 19:53:54 -04:00
|
|
|
/**
|
|
|
|
* Formulas with Nested Ifs, or If with text functions like
|
|
|
|
* Mid in it, can give #VALUE in Excel
|
|
|
|
*/
|
|
|
|
@Test
|
2015-10-12 16:43:42 -04:00
|
|
|
public void bug55747() throws IOException {
|
2015-09-20 08:02:40 -04:00
|
|
|
Workbook wb1 = _testDataProvider.createWorkbook();
|
|
|
|
FormulaEvaluator ev = wb1.getCreationHelper().createFormulaEvaluator();
|
|
|
|
Sheet s = wb1.createSheet();
|
|
|
|
|
2015-07-31 19:53:54 -04:00
|
|
|
Row row = s.createRow(0);
|
|
|
|
row.createCell(0).setCellValue("abc");
|
|
|
|
row.createCell(1).setCellValue("");
|
|
|
|
row.createCell(2).setCellValue(3);
|
|
|
|
|
|
|
|
Cell cell = row.createCell(5);
|
|
|
|
cell.setCellFormula("IF(A1<>\"\",MID(A1,1,2),\" \")");
|
|
|
|
ev.evaluateAll();
|
|
|
|
assertEquals("ab", cell.getStringCellValue());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2015-07-31 19:53:54 -04:00
|
|
|
cell = row.createCell(6);
|
|
|
|
cell.setCellFormula("IF(B1<>\"\",MID(A1,1,2),\"empty\")");
|
|
|
|
ev.evaluateAll();
|
|
|
|
assertEquals("empty", cell.getStringCellValue());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2015-07-31 19:53:54 -04:00
|
|
|
cell = row.createCell(7);
|
|
|
|
cell.setCellFormula("IF(A1<>\"\",IF(C1<>\"\",MID(A1,1,2),\"c1\"),\"c2\")");
|
|
|
|
ev.evaluateAll();
|
|
|
|
assertEquals("ab", cell.getStringCellValue());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2015-07-31 20:01:56 -04:00
|
|
|
// Write it back out, and re-read
|
2015-09-20 08:02:40 -04:00
|
|
|
Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
|
|
|
|
wb1.close();
|
|
|
|
ev = wb2.getCreationHelper().createFormulaEvaluator();
|
|
|
|
s = wb2.getSheetAt(0);
|
2015-07-31 20:01:56 -04:00
|
|
|
row = s.getRow(0);
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2015-07-31 20:01:56 -04:00
|
|
|
// Check read ok, and re-evaluate fine
|
|
|
|
cell = row.getCell(5);
|
|
|
|
assertEquals("ab", cell.getStringCellValue());
|
|
|
|
ev.evaluateFormulaCell(cell);
|
|
|
|
assertEquals("ab", cell.getStringCellValue());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2015-07-31 20:01:56 -04:00
|
|
|
cell = row.getCell(6);
|
|
|
|
assertEquals("empty", cell.getStringCellValue());
|
|
|
|
ev.evaluateFormulaCell(cell);
|
|
|
|
assertEquals("empty", cell.getStringCellValue());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2015-07-31 20:01:56 -04:00
|
|
|
cell = row.getCell(7);
|
|
|
|
assertEquals("ab", cell.getStringCellValue());
|
|
|
|
ev.evaluateFormulaCell(cell);
|
|
|
|
assertEquals("ab", cell.getStringCellValue());
|
2015-09-20 08:02:40 -04:00
|
|
|
wb2.close();
|
2015-07-31 19:53:54 -04:00
|
|
|
}
|
2015-08-19 09:36:08 -04:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void bug58260() throws IOException {
|
|
|
|
//Create workbook and worksheet
|
|
|
|
Workbook wb = _testDataProvider.createWorkbook();
|
2015-08-24 04:12:01 -04:00
|
|
|
//Sheet worksheet = wb.createSheet("sample");
|
2015-08-19 09:36:08 -04:00
|
|
|
|
|
|
|
//Loop through and add all values from array list
|
|
|
|
// use a fixed seed to always produce the same file which makes comparing stuff easier
|
2015-08-24 04:12:01 -04:00
|
|
|
//Random rnd = new Random(4352345);
|
2015-08-19 09:36:08 -04:00
|
|
|
int maxStyles = (wb instanceof HSSFWorkbook) ? 4009 : 64000;
|
|
|
|
for(int i = 0;i < maxStyles;i++) {
|
|
|
|
//Create new row
|
2015-08-24 04:12:01 -04:00
|
|
|
//Row row = worksheet.createRow(i);
|
2015-08-19 09:36:08 -04:00
|
|
|
|
|
|
|
//Create cell style
|
2015-09-20 08:02:40 -04:00
|
|
|
CellStyle style = null;
|
2015-08-19 09:36:08 -04:00
|
|
|
try {
|
|
|
|
style = wb.createCellStyle();
|
|
|
|
} catch (IllegalStateException e) {
|
2015-09-20 08:02:40 -04:00
|
|
|
fail("Failed for row " + i);
|
2015-08-19 09:36:08 -04:00
|
|
|
}
|
|
|
|
style.setAlignment(CellStyle.ALIGN_RIGHT);
|
|
|
|
if((wb instanceof HSSFWorkbook)) {
|
|
|
|
// there are some predefined styles
|
|
|
|
assertEquals(i+21, style.getIndex());
|
|
|
|
} else {
|
|
|
|
// getIndex() returns short, which is not sufficient for > 32767
|
|
|
|
// we should really change the API to be "int" for getIndex() but
|
|
|
|
// that needs API changes
|
|
|
|
assertEquals(i+1, style.getIndex() & 0xffff);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Create cell
|
2015-08-24 04:12:01 -04:00
|
|
|
//Cell cell = row.createCell(0);
|
2015-08-19 09:36:08 -04:00
|
|
|
|
|
|
|
//Set cell style
|
2015-08-24 04:12:01 -04:00
|
|
|
//cell.setCellStyle(style);
|
2015-08-19 09:36:08 -04:00
|
|
|
|
|
|
|
//Set cell value
|
2015-08-24 04:12:01 -04:00
|
|
|
//cell.setCellValue("r" + rnd.nextInt());
|
2015-08-19 09:36:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// should fail if we try to add more now
|
|
|
|
try {
|
|
|
|
wb.createCellStyle();
|
|
|
|
fail("Should fail after " + maxStyles + " styles, but did not fail");
|
|
|
|
} catch (IllegalStateException e) {
|
|
|
|
// expected here
|
|
|
|
}
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2015-08-19 09:36:08 -04:00
|
|
|
/*//add column width for appearance sake
|
|
|
|
worksheet.setColumnWidth(0, 5000);
|
2015-09-20 08:02:40 -04:00
|
|
|
|
|
|
|
// Write the output to a file
|
2015-08-19 09:36:08 -04:00
|
|
|
System.out.println("Writing...");
|
|
|
|
OutputStream fileOut = new FileOutputStream("C:\\temp\\58260." + _testDataProvider.getStandardFileNameExtension());
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2015-08-19 09:36:08 -04:00
|
|
|
// the resulting file can be compressed nicely, so we need to disable the zip bomb detection here
|
|
|
|
double before = ZipSecureFile.getMinInflateRatio();
|
|
|
|
try {
|
|
|
|
ZipSecureFile.setMinInflateRatio(0.00001);
|
|
|
|
wb.write(fileOut);
|
2015-09-20 08:02:40 -04:00
|
|
|
} finally {
|
2015-08-19 09:36:08 -04:00
|
|
|
fileOut.close();
|
|
|
|
ZipSecureFile.setMinInflateRatio(before);
|
|
|
|
}*/
|
2015-09-20 08:02:40 -04:00
|
|
|
|
2015-08-19 09:36:08 -04:00
|
|
|
wb.close();
|
|
|
|
}
|
2015-09-29 08:26:08 -04:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void test50319() throws IOException {
|
|
|
|
Workbook wb = new HSSFWorkbook();
|
|
|
|
Sheet sheet = wb.createSheet("Test");
|
|
|
|
sheet.createRow(0);
|
|
|
|
sheet.groupRow(0, 0);
|
|
|
|
sheet.setRowGroupCollapsed(0, true);
|
|
|
|
|
|
|
|
sheet.groupColumn(0, 0);
|
|
|
|
sheet.setColumnGroupCollapsed(0, true);
|
|
|
|
|
|
|
|
wb.close();
|
|
|
|
}
|
2009-05-18 15:11:45 -04:00
|
|
|
}
|