简体   繁体   中英

Add 1 to each cell in every row of two columns until a blank cell is reached - Google Spreadsheet

All I'm looking to do is add 1 to a range of cells until it reaches a blank cell. I'm also minus 1 from a another range of cells until it reaches a blank cell. I've currently codded each cell individually but the number of cells with data in changes so isn't really practical.

Here is an extract of code I have written can anyone help me?

function Calculations() {
  //Week Number & Week Pay
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet()
  s=SpreadsheetApp.setActiveSheet(spreadsheet.getSheetByName("Money")

  var currVal=s.getRange("C2").getValue()
  var plusVal= currVal +1
  s.getRange("C2") .setValue(plusVal)
  var currVal=s.getRange("B2").getValue()
  var minusVal= currVal -1
  s.getRange("B2") .setValue(minusVal)

  var currVal=s.getRange("C3").getValue()
  var plusVal= currVal +1
  s.getRange("C3") .setValue(plusVal)
  var currVal=s.getRange("B3").getValue()
  var minusVal= currVal -1
  s.getRange("B3") .setValue(minusVal)

  var currVal=s.getRange("C4").getValue()
  var plusVal= currVal +1
  s.getRange("C4") .setValue(plusVal)
  var currVal=s.getRange("B4").getValue()
  var minusVal= currVal -1
  s.getRange("B4") .setValue(minusVal)
}

Instead of using A1 or R1C1 notation, I find it easier to just use numbers when getting the cell location (range).

Increment the row number at the end of the loop. The loop will iterate a maximum of the number of rows with data, and it will break if it finds an empty cell.

function Calculations() {
try{
  //Week Number & Week Pay
  var currVal,i,L,lastRow,minusVal,plusVal,ss,sh,theColumn,theRng,theRow;

  ss = SpreadsheetApp.getActiveSpreadsheet();//Get the active spreadsheet
  sh = ss.getSheetByName("Money");//Get a sheet tab by name
  lastRow = sh.getLastRow();//Get the number of the last row with data

  L = lastRow;
  theRow = 2;//Start in row 2

  for (i=0;i<L;i++) {//Loop as many times as there is number of rows with data
    theColumn = 3;//Column C

    theRng = sh.getRange(theRow,theColumn);//Get the range which is one cell

    currVal = theRng.getValue();//Get the value in one cell
    //Logger.log('currVal: ' + currVal);

    if (!currVal) {//If there is a blank cell then
      break;//quit the loop
    }

    plusVal= currVal +1;
    theRng.setValue(plusVal);

    theColumn = 2;//Column B

    theRng = sh.getRange(theRow,theColumn);

    currVal = theRng.getValue();
    minusVal = currVal - 1;
    theRng.setValue(minusVal);

    if (!currVal) {//If there is a blank cell then
      break;//quit the loop
    }

    theRow = theRow + 1;//Increment to the next row
  }

}catch(e) {
  Logger.log('message: ' + e.message);
  Logger.log('stack: ' + e.stack);
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM