How to loop through each cell in column?

Viewed 281

The script simply adds 1 to the cell in a specific column, but I'd like it to +1 to every cell in a specific column with a set delay.

This is what I've gathered so far, but need that extra step.

function plus() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var yesCount = ss.getRange("B4");
    var yesAdd = yesCount.getValue();
    if(yesAdd != ""){
        yesCount.setValue(yesAdd+1);
        Utilities.sleep(1000);
  }
}

Basically B4 should be the entire B column and I'd like to step through each (non-empty) cell wait for one or two second and move on to the next cell.

1 Answers

Note that the spreadsheet may not be updated immediately every time you change the value.

function plus() {
  const row = 4;
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName('Sheet Name');
  const rows = sheet.getLastRow() - row + 1;
  const range = sheet.getRange(row, 2, rows, 1);
  const values = range.getValues().flat();
  const matches = {};
  values.forEach((value, i) => {
    if (value !== '') {
      sheet.getRange(row + i, 2).setValue(value + 1);
      Utilities.sleep(1000);
    }
  });
}
Related