if (colNum > maxColNum)
maxColNum = colNum;
}
if (cells != null) {
final Cell cell = row.getCell(srcCol);
if (cell != null) {
cells.put(Integer.valueOf(rowNum), cell);
}
}
shiftCells(row, startCol, endCol, n, clearRest);
}
}
if (endCol < 0) {
endCol = maxColNum;
}
if (n > 0) {
if (startCol > endCol) { //nothing to do
return Collections.emptyList();
}
} else {
if ((startCol + n) > endCol) { //nothing to do
return Collections.emptyList();
}
}
final int maxrow = SpreadsheetVersion.EXCEL2007.getLastRowIndex();
final int maxcol = SpreadsheetVersion.EXCEL2007.getLastColumnIndex();
final List<CellRangeAddress[]> shiftedRanges = BookHelper.shiftMergedRegion(this, tRow, startCol, bRow, endCol, n, true);
final boolean wholeColumn = tRow == 0 && bRow == maxrow;
if (wholeColumn) {
//TODO handle the page breaks
//?
}
// Move comments from the source column to the
// destination column. Note that comments can
// exist for cells which are null
if (moveComments) {
final CommentsTable sheetComments = getCommentsTable(false);
if(sheetComments != null){
//TODO shift Note's anchor in the associated /xl/drawing/vmlDrawings#.vml
CTCommentList lst = sheetComments.getCTComments().getCommentList();
for (final Iterator<CTComment> it = lst.getCommentList().iterator(); it.hasNext();) {
CTComment comment = it.next();
CellReference ref = new CellReference(comment.getRef());
final int colnum = ref.getCol();
final int rownum = ref.getRow();
if(startCol <= colnum && colnum <= endCol && tRow <= rownum && rownum <= bRow){
int newColNum = colnum + n;
if (newColNum < 0 || newColNum > maxcol) { //out of bound, shall remove it
it.remove();
} else {
ref = new CellReference(ref.getRow(), newColNum);
comment.setRef(ref.formatAsString());
}
}
}
}
}
// Fix up column width if required
int s, inc;
if (n < 0) {
s = startCol;
inc = 1;
} else {
s = endCol;
inc = -1;
}
if (wholeColumn && (copyColWidth || resetOriginalColWidth)) {
final int defaultColumnWidth = getDefaultColumnWidth();
for ( int colNum = s; colNum >= startCol && colNum <= endCol && colNum >= 0 && colNum <= maxcol; colNum += inc ) {
final int newColNum = colNum + n;
if (copyColWidth) {
setColumnWidth(newColNum, getColumnWidth(colNum));
}
if (resetOriginalColWidth) {
setColumnWidth(colNum, defaultColumnWidth);
}
}
}
//handle inserted columns
if (srcCol >= 0) {
final int col2 = Math.min(startCol + n - 1, maxcol);
if (wholeColumn) {
for (int col = startCol; col <= col2 ; ++col) {
//copy the column width
setColumnWidth(col, colWidth);
if (colStyle != null) {
setDefaultColumnStyle(col, BookHelper.copyFromStyleExceptBorder(getBook(), colStyle));
}
}
}
if (cells != null) {
for (Entry<Integer, Cell> cellEntry : cells.entrySet()) {
final XSSFRow row = getRow(cellEntry.getKey().intValue());
final Cell srcCell = cellEntry.getValue();
final CellStyle srcStyle = srcCell.getCellStyle();
for (int col = startCol; col <= col2; ++col) {
Cell dstCell = row.getCell(col);
if (dstCell == null) {
dstCell = row.createCell(col);
}
dstCell.setCellStyle(BookHelper.copyFromStyleExceptBorder(getBook(), srcStyle));
}
}
}
}