import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship;
+import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.usermodel.Hyperlink;
+import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.Internal;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHyperlink;
public void setCellReference(String ref) {
_ctHyperlink.setRef(ref);
}
+
@Internal
public void setCellReference(CellReference ref) {
setCellReference(ref.formatAsString());
}
- private CellReference buildCellReference() {
+ private CellReference buildFirstCellReference() {
+ return buildCellReference(false);
+ }
+
+ private CellReference buildLastCellReference() {
+ return buildCellReference(true);
+ }
+
+ private CellReference buildCellReference(boolean lastCell) {
String ref = _ctHyperlink.getRef();
if (ref == null) {
ref = "A1";
}
+ if (ref.contains(":")) {
+ AreaReference area = new AreaReference(ref, SpreadsheetVersion.EXCEL2007);
+ return lastCell ? area.getLastCell() : area.getFirstCell();
+ }
return new CellReference(ref);
}
-
/**
* Return the column of the first cell that contains the hyperlink
*
*/
@Override
public int getFirstColumn() {
- return buildCellReference().getCol();
+ return buildFirstCellReference().getCol();
}
*/
@Override
public int getLastColumn() {
- return buildCellReference().getCol();
+ return buildLastCellReference().getCol();
}
/**
*/
@Override
public int getFirstRow() {
- return buildCellReference().getRow();
+ return buildFirstCellReference().getRow();
}
*/
@Override
public int getLastRow() {
- return buildCellReference().getRow();
+ return buildLastCellReference().getRow();
}
/**
*/
@Override
public void setFirstColumn(int col) {
- setCellReference(new CellReference( getFirstRow(), col ));
+ String firstCellRef = CellReference.convertNumToColString(col) + (getFirstRow() + 1);
+ setCellRange(firstCellRef + ":" + buildLastCellReference().formatAsString());
}
/**
* Set the column of the last cell that contains the hyperlink.
- * For XSSF, a Hyperlink may only reference one cell
*
* @param col the 0-based column of the last cell that contains the hyperlink
*/
@Override
public void setLastColumn(int col) {
- setFirstColumn(col);
+ String lastCellRef = CellReference.convertNumToColString(col) + (getLastRow() + 1);
+ setCellRange(buildFirstCellReference().formatAsString() + ":" + lastCellRef);
}
/**
*/
@Override
public void setFirstRow(int row) {
- setCellReference(new CellReference( row, getFirstColumn() ));
+ String firstCellRef = CellReference.convertNumToColString(getFirstColumn()) + (row + 1);
+ setCellRange(firstCellRef + ":" + buildLastCellReference().formatAsString());
}
/**
* Set the row of the last cell that contains the hyperlink.
- * For XSSF, a Hyperlink may only reference one cell
*
* @param row the 0-based row of the last cell that contains the hyperlink
*/
@Override
public void setLastRow(int row) {
- setFirstRow(row);
+ String lastCellRef = CellReference.convertNumToColString(getLastColumn()) + (row + 1);
+ setCellRange(buildFirstCellReference().formatAsString() + ":" + lastCellRef);
+ }
+
+ private void setCellRange(String range) {
+ AreaReference ref = new AreaReference(range, SpreadsheetVersion.EXCEL2007);
+ if(ref.isSingleCell()) {
+ setCellReference(ref.getFirstCell());
+ } else {
+ setCellReference(ref.formatAsString());
+ }
}
/**
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.formula.FormulaShifter;
import org.apache.poi.ss.formula.SheetNameFormatter;
-import org.apache.poi.ss.usermodel.Cell;
-import org.apache.poi.ss.usermodel.CellCopyPolicy;
-import org.apache.poi.ss.usermodel.CellRange;
-import org.apache.poi.ss.usermodel.CellStyle;
-import org.apache.poi.ss.usermodel.CellType;
-import org.apache.poi.ss.usermodel.DataValidation;
-import org.apache.poi.ss.usermodel.DataValidationHelper;
-import org.apache.poi.ss.usermodel.Font;
-import org.apache.poi.ss.usermodel.Footer;
-import org.apache.poi.ss.usermodel.FormulaEvaluator;
-import org.apache.poi.ss.usermodel.Header;
-import org.apache.poi.ss.usermodel.IgnoredErrorType;
-import org.apache.poi.ss.usermodel.Name;
-import org.apache.poi.ss.usermodel.Row;
-import org.apache.poi.ss.usermodel.Sheet;
-import org.apache.poi.ss.usermodel.Table;
-import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.ss.util.CellRangeAddress;
*/
@Override
public XSSFHyperlink getHyperlink(CellAddress addr) {
- String ref = addr.formatAsString();
- for(XSSFHyperlink hyperlink : hyperlinks) {
- if(hyperlink.getCellRef().equals(ref)) {
+ for (XSSFHyperlink hyperlink : getHyperlinkList()) {
+ if (addr.getRow() >= hyperlink.getFirstRow() && addr.getRow() <= hyperlink.getLastRow()
+ && addr.getColumn() >= hyperlink.getFirstColumn() && addr.getColumn() <= hyperlink.getLastColumn()) {
return hyperlink;
}
}
}
}
+
+ @Test
+ public void readSharedHyperlink() throws IOException {
+ try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("sharedhyperlink.xlsx")) {
+ XSSFSheet sheet = wb.getSheetAt(0);
+
+ XSSFHyperlink hyperlink3 = sheet.getHyperlink(new CellAddress("A3"));
+ XSSFHyperlink hyperlink4 = sheet.getHyperlink(new CellAddress("A4"));
+ XSSFHyperlink hyperlink5 = sheet.getHyperlink(new CellAddress("A5"));
+ assertNotNull(hyperlink3, "hyperlink found?");
+ assertEquals(hyperlink3, hyperlink4);
+ assertEquals(hyperlink3, hyperlink5);
+
+ assertEquals("A3:A5", hyperlink3.getCellRef());
+ assertEquals(0, hyperlink3.getFirstColumn());
+ assertEquals(0, hyperlink3.getLastColumn());
+ assertEquals(2, hyperlink3.getFirstRow());
+ assertEquals(4, hyperlink3.getLastRow());
+ }
+ }
+
@Test
void testCreate() throws Exception {
XSSFWorkbook workbook = new XSSFWorkbook();
}
@Test
- void test() throws IOException {
+ void testCellStyle() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
wb.close();
wbBack.close();
}
+
+ @Test
+ void testChangeReference() throws IOException {
+ try (XSSFWorkbook wb = new XSSFWorkbook()) {
+ XSSFHyperlink hyperlink = new XSSFHyperlink(HyperlinkType.URL);
+ hyperlink.setCellReference("B2");
+ assertEquals(1, hyperlink.getFirstRow());
+ assertEquals(1, hyperlink.getLastRow());
+ assertEquals(1, hyperlink.getFirstColumn());
+ assertEquals(1, hyperlink.getLastColumn());
+ hyperlink.setFirstRow(0);
+ assertEquals("B1:B2", hyperlink.getCellRef());
+ assertEquals(0, hyperlink.getFirstRow());
+ assertEquals(1, hyperlink.getLastRow());
+ assertEquals(1, hyperlink.getFirstColumn());
+ assertEquals(1, hyperlink.getLastColumn());
+ hyperlink.setLastRow(2);
+ assertEquals("B1:B3", hyperlink.getCellRef());
+ assertEquals(0, hyperlink.getFirstRow());
+ assertEquals(2, hyperlink.getLastRow());
+ assertEquals(1, hyperlink.getFirstColumn());
+ assertEquals(1, hyperlink.getLastColumn());
+ hyperlink.setFirstColumn(0);
+ assertEquals("A1:B3", hyperlink.getCellRef());
+ assertEquals(0, hyperlink.getFirstRow());
+ assertEquals(2, hyperlink.getLastRow());
+ assertEquals(0, hyperlink.getFirstColumn());
+ assertEquals(1, hyperlink.getLastColumn());
+ hyperlink.setLastColumn(2);
+ assertEquals("A1:C3", hyperlink.getCellRef());
+ assertEquals(0, hyperlink.getFirstRow());
+ assertEquals(2, hyperlink.getLastRow());
+ assertEquals(0, hyperlink.getFirstColumn());
+ assertEquals(2, hyperlink.getLastColumn());
+ hyperlink.setFirstColumn(2);
+ hyperlink.setFirstRow(2);
+ assertEquals("C3", hyperlink.getCellRef());
+ assertEquals(2, hyperlink.getFirstRow());
+ assertEquals(2, hyperlink.getLastRow());
+ assertEquals(2, hyperlink.getFirstColumn());
+ assertEquals(2, hyperlink.getLastColumn());
+ }
+ }
}