Tuesday 28 March 2023

Switch view to a specific Google Sheet

The following Google Apps Script code is designed to switch the active sheet that the user is currently looking at to that of another within the same Google Sheet file. This is a snippet of code that could be placed at the end of a function when archiving data from one sheet to another for instance, and you want the destination to be shown to the user.

Apps Script Code to switch the active view for a user
Apps Script Code to switch the active view for a user


The Code

We start by getting the active spreadsheet, which then allows us to drill down and get a specific range within the Google Sheet that we want the cursor to switch focus to. Finally, we set this as the current cell:

  // get spreadsheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  // get cell range in destination sheet that the view will be switched to
  var range = ss.getRange('Sheet2!A1');

  // activate cell range so cursor moves to that position and changes the current view
  range.activateAsCurrentCell();



Download

Switch view to a specific Google Sheet download (please use 'File' > 'Make a copy' for your own version).


No comments:

Post a Comment