Google Sheets – Set Selection Across All Sheets

google sheetsgoogle-apps-script

I have a spreadsheet with multiple sheets. I want them all to scroll in sync. For example if I scroll to J18 in Sheet1, all others sheets will have the cursor at J18 and same row/column at the top left.

I know how to write (basic) scripts – but could not figure this one out. Any ideas?

Question on Google+ community: https://plus.google.com/113862354064807861446/posts/D7b919WFFjY

Best Answer

The closest thing you can have is the following script.

Code

function onOpen() {
  SpreadsheetApp.getUi().createMenu("Harmonize")
    .addItem("Go !!", "mySheets").addToUi();
}

function mySheets() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  // retrieve number of sheets
  var nshs = ss.getNumSheets();

  // find first sheet
  for(var i=0; i<nshs; i++) {
    var sh = ss.getSheets()[i];
    if(sh.getIndex() == 1) {
      var aCell = ss.getActiveCell().getA1Notation();
      var index = i;
    }
  }

  // set active range throughout the sheets
  for(var j=0; j<nshs; j++) {
    if(ss.getSheets()[j].getIndex() != 1) {
      ss.getSheets()[j].setActiveSelection(aCell).activate();
      Utilities.sleep(200);
    }
  }

  // return to first sheet
  ss.getSheets()[index].setActiveSelection(aCell).activate();   
}

Explained

Upon opening, a menu item is created. Selecting this item, will execute the mySheet function. This will find the first sheet (doesn't need to be getSheets[0]) and its index plus the A1Notation(). After that, it will set the active selection of each sheet according to the first sheet.

Example

I've created an example sheet for you: Set Selection throughout all sheets