title: Google Sheets App Script to change sheet and keep same active cell
body: <p>I'm writing a script for Google Sheets in App Script. The spreadsheet contains two sheets and each has a button on it. Clicking the button on the first sheet takes you to the second sheet. Clicking the button on the second takes you back to the first.</p>
  <p>It sounds simple, but I'm unable to restore the cursor position when I go back to each sheet. It always changes to A1 instead. Here is my code.</p>
  <pre><code>function changesheet2() {
    var spreadsheet = SpreadsheetApp.getActive();
    spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Sheet2'), true);
  };
  function changesheet1() {
    var spreadsheet = SpreadsheetApp.getActive();
    spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Sheet1'), true);
  };
  </code></pre>
  <p>According to the <a href="https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app#setActiveSheet(Sheet,Boolean)" rel="nofollow noreferrer">documentation</a>, calling <code>setActiveSheet</code> with parameter <code>true</code> should restore the previous active selection and cursor position. This seems to work OK if you do everything within a single function. It doesn't work when there are two separate functions as here.</p>
  <p>It's presumably something about context and who can see what data. I am running both scripts from the same user context by getting the user to click a button in each sheet. How can I make it work?</p>