Tom
Tom

Reputation: 7

how to find next blank cell in a specific range using apps script?

This function will activate the next blank cell in column F which is part of what I want. How can I modify this code to check a specific range. The range I want it check is F12:F34 only and ignore the rest of the column. If i change the range in the "values" var to F12:F34 it activates a random cell not close to what I want. Can someone assist me with this code?

function jump() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var values = sheet.getRange("F:F").getValues();
  var maxIndex = values.reduce(function(maxIndex, row, index) {
    return row[0] === "" ? maxIndex : index;
  }, 0);
  sheet.setActiveRange(sheet.getRange(maxIndex + 2, 6));
}

Upvotes: 0

Views: 588

Answers (1)

Nico
Nico

Reputation: 316

It's not activating a random cell, it's activating the cell that is offset 11 rows from the first empty cell, because the index of the first row in values is 0, not 11. Change the last line to

sheet.setActiveRange(sheet.getRange(maxIndex + 13, 6));

and it should work. 13 is the offset of the values range + 2.

Upvotes: 1

Related Questions