1

I have a 72 sheet Google spreadsheet and am trying to reset each sheet so that it shows A1 in the upper left when you click on its tab. That is, if a sheet is scrolled downward so that you can't see A1, I want it to scroll back so that you can.

I've tried the following google scripts, but nothing does the job. I got the 4th one (reset4) from here but that didn't work either.

function reset1() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var allSheets = ss.getSheets();
  for (var i = 1; i < allSheets.length; i++) {
    allSheets[i].setActiveSelection("A1");
  }
}

function reset2() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var allSheets = ss.getSheets();
  for (var i = 1; i < allSheets.length; i++) {
    allSheets[i].setActiveRange(allSheets[i].getRange("A1"));
  }
}

function reset3() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var allSheets = ss.getSheets();
  for (var i = 1; i < allSheets.length; i++) {
    allSheets[i].setCurrentCell(allSheets[i].getRange("A1").getCell(1, 1));
  }
}

// ***** CORRECTION: 
// ***** reset4() later found to work. See accepted answer comments for details

function reset4() {
  SpreadsheetApp.getActive().getSheets().forEach(function (s) {
    s.setActiveSelection("A1");
  });
  SpreadsheetApp.flush(); // may still need to refresh page afterward
}

function reset5(){
  var ss = SpreadsheetApp.getActive();
  var sheets = ss.getSheets();
  sheets.forEach(function(sh){
    sh.activate();
    ss.getActiveSheet().getRange("A1").activate();
  });
}

I added the 5th one (reset5) based on the code supplied in the answer by Rafa Guillermo, but that didn't work either. Though the answer by Rafa Guillermo works, I didn't mean for sheets to be reset EVERY time its tab is clicked, but only after the desired function is run.

The animated .gif below shows a test spreadsheet having the last function (reset5) and 3 sheets with may rows and columns. At the beginning of the video, cell A1 (with a red background color) is at the top left corner of each sheet. Then I scroll down and over and select other cells in each sheet so that A1 is no longer seen. After running the function, Sheet3 does have A1 positioned at the top left, as desired. But, the problem is that Sheet1 and Sheet2 do not.

enter image description here

Tony M
  • 1,694
  • 2
  • 17
  • 33
  • About `I got the 4th one (reset4) from here but that didn't work either.`, I think that in your script of `reset4`, when the function is run, the cell "A1" of each sheet is activated and the each tab is opened in the order. For this situation, can I ask you about the difference from `am trying to reset each sheet so that it shows A1 in the upper left when you click on its tab.`? – Tanaike May 01 '20 at 11:45
  • @Tanaike I have added a sentence to the first paragraph to make what I mean (hopefully) clearer. For example, I have many sheets which are scrolled down so you cannot see A1 when you click on their tab. I want all those to be scrolled back up so that when I click on the tab I do see the top, A1. – Tony M May 01 '20 at 12:36
  • Thank you for replying. When `setActiveSelection("A1")` is run, the cell "A1" is selected and focused. About `scroll back`, this is different from the result you expect. Is my understanding correct? – Tanaike May 01 '20 at 12:42
  • Thank you for replying. I deeply apologize for my poor English skill. About `Is my understanding correct?`, I asked whether I could correctly understand about your question. – Tanaike May 01 '20 at 12:48

1 Answers1

2

Answer:

You need to use SpreadsheetApp.flush() after you set the active range of each sheet.

NB: This answer was edited after more information was provided, the original answer is below the break.

More Information:

If I understand your situation correctly, you wish to have a function which:

  • Does not run on a trigger.
  • When run, resets the active cell of all sheets in the Spreadsheet it is attached to to cell A1.

With these parameters, I believe that the function reset4() you provided in your question does in fact work. Now, Google Sheets does have some strange behaviour with bound script files and sometimes the script tab will 'lose' it's bound status to the Spreadsheet if both are open and left unattended for long periods of time; this can be fixed by reloading the Spreadsheet (which, from a UI perspective, will also reset the active cell in all your sheets to A1, but we shall put that aside for now).

Code:

As a modification to the modification of the script in my previous answer, you can run your reset5() function but add a SpreadsheetApp.flush() after the activate() call:

function reset() {
  var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
  var range;
  sheets.forEach(function(sh) {
    range = sh.getRange("A1");
    range.activate();
    SpreadsheetApp.flush();
  });
}

References:


Previous Answer:

You can use the onSelectionChange(e) trigger in Apps Script to change the active cell of the current Sheet to A1 when the tab is changed.

More Information:

As per the Simple Triggers documentation:

The onSelectionChange(e) trigger runs automatically when a user changes the selection in a spreadsheet. Most onSelectionChange(e) triggers use the information in the event object to respond appropriately.

Code Example:

You can store the current active sheet in a PropertiesService property and compare it to the new active Sheet when the active selection is changed. From here you can then change the active cell to A1 of that sheet:

function onSelectionChange(e) {
  var lastSheet = PropertiesService.getUserProperties().getProperty("LastActiveSheet");
  if (e.source.getActiveSheet().getName() == lastSheet) {
    return;
  }
  else {
    PropertiesService.getUserProperties().setProperty("LastActiveSheet", e.source.getActiveSheet().getName())
    SpreadsheetApp.getActiveSheet().getRange("A1").activate();
  }
}

References:

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Rafa Guillermo
  • 14,474
  • 3
  • 18
  • 54
  • Thank you for some cool code @Rafa Guillermo, but it's not what I wanted. My phrase "when you click on its tab" was not meant to imply the triggering of an event, but rather that all sheets would be reset to A1 when a function like one of the 4 I showed was run. Only after "when you click on its tab" you would see the result from running the function. Based on your code I added another function (reset5) to try to do what I want and yet it did not work either. Your code works but I don't want sheets reset EVERY time its tab is clicked, but only after the desired function is run – Tony M May 08 '20 at 15:49
  • I don't think I understand your situation. All the functions you have in your question reset the active cell to A1 when they run. At what point exactly do you want the function to run, and what exactly do you what it to do? Please be as concise as possible. – Rafa Guillermo May 08 '20 at 17:24
  • I have now added an animated .gif to my post @Rafa Guillermo. This test spreadsheet has 3 sheets and the code for 'reset5'. What I want to happen occurs for Sheet3 but not Sheet1 or Sheet2 – Tony M May 08 '20 at 20:43
  • `reset4` absolutely does work for me, if I understand what you are describing. I have updated my answer with a new modified version of `reset5` though, which I believe should work. – Rafa Guillermo May 10 '20 at 20:11
  • Thank you, @Rafa Guillermo for your complete answer. I have learned a lot. I confirm that it now works with both 'reset4' and the answer you provided. Perhaps reset4 did not originally work for me because of the 'strange behavior' you mentioned because my 72 tab spreadsheet was indeed 'open and left unattended for long periods of time'. – Tony M May 11 '20 at 09:16