View | Details | Raw Unified | Return to bug 34023
Collapse All | Expand All

(-)C:/workspace/poi/org/apache/poi/hssf/usermodel/HSSFSheet.java (+46 lines)
Lines 36-41 Link Here
36
import java.util.Iterator;
36
import java.util.Iterator;
37
import java.util.List;
37
import java.util.List;
38
import java.util.TreeMap;
38
import java.util.TreeMap;
39
import java.util.regex.Matcher;
40
import java.util.regex.Pattern;
39
41
40
/**
42
/**
41
 * High level representation of a worksheet.
43
 * High level representation of a worksheet.
Lines 972-977 Link Here
972
974
973
	}
975
	}
974
976
977
	protected void shiftFormulas(int startRow, int endRow, int n){
978
		Iterator itRow = this.rowIterator();
979
		while(itRow.hasNext()){
980
			HSSFRow row = (HSSFRow)itRow.next();
981
			Iterator itCells = row.cellIterator();
982
			while(itCells.hasNext()){
983
				HSSFCell cellSource = (HSSFCell)itCells.next();
984
				if (cellSource.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
985
					cellSource.setCellFormula(cellSource.getCellFormula());
986
//					logger.info("Formula cell found: '"
987
//							+ cellSource.getCellFormula() + "'");
988
					Pattern p = Pattern
989
							.compile("(\\${0,1}[a-zA-Z]{1,2})(\\${0,1}\\d{1,})");
990
					String sOrigFormula = cellSource.getCellFormula();
991
					Matcher m = p.matcher(sOrigFormula);
992
					StringBuffer sbNewFormula = new StringBuffer();
993
					int iStart = 0;
994
					while (m.find()) {
995
						sbNewFormula.append(sOrigFormula.substring(iStart, m.start()));
996
//						logger.info("matched pattern: " + m.group());
997
//						for (int i = 1; i <= m.groupCount(); i++) {
998
//							logger.info("submatch(" + i + "): " + m.group(i));
999
//						}
1000
						// m.group(2) is the row portion of the cell reference
1001
						sbNewFormula.append(m.group(1));
1002
						int iRow = Integer.parseInt(m.group(2));
1003
						if(iRow>=startRow && iRow<=endRow){
1004
							// replace references to iRow with iRow + n
1005
							sbNewFormula.append(iRow + n);
1006
						} else {
1007
							sbNewFormula.append(iRow);
1008
						}
1009
						iStart = m.end();
1010
					}
1011
					sbNewFormula.append(sOrigFormula.substring(iStart));
1012
					System.out.println("orig Formula: " + sOrigFormula);
1013
					System.out.println("new Formula: " + sbNewFormula.toString());
1014
					cellSource.setCellFormula(sbNewFormula.toString());
1015
				}
1016
			}
1017
		}
1018
	}
1019
	
975
    /**
1020
    /**
976
     * Shifts rows between startRow and endRow n number of rows.
1021
     * Shifts rows between startRow and endRow n number of rows.
977
     * If you use a negative number, it will shift rows up.
1022
     * If you use a negative number, it will shift rows up.
Lines 1024-1029 Link Here
1024
1069
1025
        shiftMerged(startRow, endRow, n, true);
1070
        shiftMerged(startRow, endRow, n, true);
1026
        sheet.shiftRowBreaks(startRow, endRow, n);
1071
        sheet.shiftRowBreaks(startRow, endRow, n);
1072
        shiftFormulas(startRow, endRow, n);
1027
			
1073
			
1028
        for ( int rowNum = s; rowNum >= startRow && rowNum <= endRow && rowNum >= 0 && rowNum < 65536; rowNum += inc )
1074
        for ( int rowNum = s; rowNum >= startRow && rowNum <= endRow && rowNum >= 0 && rowNum < 65536; rowNum += inc )
1029
        {
1075
        {

Return to bug 34023