https://issues.apache.org/bugzilla/show_bug.cgi?id=47100

--- Comment #3 from Carl Pritchett <[email protected]> 2011-03-01 
16:58:12 EST ---
Thanks for the explanation Yegor,

As a workaround, I made methods to update related named ranges when a sheet
name changes - could something similar be added to XSSFWorkbook? May not be the
most efficient or the best use of the POI APIs...

public void setSheetName(Workbook wb, Sheet sheet, String name)
{
    String newName = WorkbookUtil.createSafeSheetName(name);
    String oldName = sheet.getSheetName();
    wb.setSheetName(wb.getSheetIndex(sheet), newName);

    if (wb instanceof XSSFWorkbook)
    {
        updateNamedRangesWithSheetName(wb, sheet, oldName);
    } 
}

public void updateNamedRangesWithSheetName(Workbook wb, Sheet sheet, String
oldName)
{
    int numNames = wb.getNumberOfNames();     
    for (int i = 0; i < numNames; i++)
    {
        Name namedRange = wb.getNameAt(i);
        if (namedRange == null || namedRange.isDeleted() ||
!oldName.equals(namedRange.getSheetName())) continue;

        AreaReference areaRef = new
AreaReference(namedRange.getRefersToFormula());

        CellReference firstCell = areaRef.getFirstCell();
        CellReference lastCell = areaRef.getLastCell();

        updateNamedRange(sheet, namedRange, firstCell.getRow(),
firstCell.getCol(), lastCell.getRow(), lastCell.getCol());
    }
}

private void updateNamedRange(Sheet sheet, Name namedRange, int firstRow, int
firstCol, int lastRow, int lastCol)
{
    CellReference firstCellRef = new CellReference(firstRow, firstCol, true,
true);
    CellReference lastCellRef = new CellReference(lastRow, lastCol, true,
true);

    AreaReference ref = new AreaReference(firstCellRef, lastCellRef);

    String formula = "'" + sheet.getSheetName() + "'!" + ref.formatAsString();
    namedRange.setRefersToFormula(formula);
}

-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to