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.