Google-sheets – Move cursor to cell in all sheets

google sheetsgoogle-apps-scriptgoogle-apps-script-triggers

I have the following script:

function setActiveCells() {

  var wb = SpreadsheetApp.getActiveSpreadsheet();

  var ws_names = wb.getRangeByName('an_sheets').getValues();
  var ws_cells = wb.getRangeByName('an_sheetsLastCells').getValues();
  var ws_count = ws_cells.length;

  for (var i=0;i<ws_count;i++) {
    var ws_cell_ref = ws_cells[i][0];
    if (ws_cell_ref.length > 0) {
      var ws_name = ws_names[i][0];
      var ws = wb.getSheetByName(ws_name);
      ws.setActiveRange(ws.getRange(ws_cell_ref));
      SpreadsheetApp.flush();
    }
  }

  var ws = wb.getSheetByName('Summary');
  ws.setActiveRange(ws.getRange(ws_cells[0][0]));

}
/* */

I am attempting to set the cursor to the indicated cell in each sheet. It does set the cursor in the correct cell on the first sheet, but the selected cells remain unchanged on the remaining sheets.

On further research I've found that adding SpreadsheetApp.flush(); after the ws.setActiveRange(…) (see code above) solves part of the issue. It does actually set the cursor in the correct cell, but it does not scroll to that cell.

I am trying to both select the cell and ensure that it is in the current viewport. Any assistance is appreciated.

Best Answer

The Apps Script Documentation says: Sets the specified range as the active range in the active sheet, with the top left cell in the range as the current cell.

Reference