Volcanis
Volcanis

Reputation: 3

Use google apps script to increment a column on Google Sheets by 1?

Not sure how to go about incrementing the number values on my google spreadsheet by 1 with a button, the script I'm using only increments the first cell and copies that down. How can I make the script increment the whole column and it's individual cells by 1?

function increment(){
  var cell = SpreadsheetApp.getActiveSheet().getRange("C2:C86");
  var cellValue = cell.getValue();
  cell.setValue(cellValue + 1);

}

Upvotes: 0

Views: 2153

Answers (1)

Tanaike
Tanaike

Reputation: 201573

  • You want to add 1 to the cells of "C2:C86".
  • You want to achieve this using Google Apps Script.

If my understanding is correct, how about this modification? Please think of this as just one of several answers.

Modified script:

function increment(){
  var cell = SpreadsheetApp.getActiveSheet().getRange("C2:C86");
  var cellValues = cell.getValues().map(function(row) {return [row[0] + 1]}); // Modified
  cell.setValues(cellValues); // Modified
}

Flow:

  1. Retrieve the values from the cells of "C2:C86".
  2. Add 1 for the value of each cell.
  3. Put the values to the range of "C2:C86".

References:

If I misunderstood your question and this was not the result you want, I apologize.

Upvotes: 1

Related Questions