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 downw
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.
If I understand your situation correctly, you wish to have a function which:
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).
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();
});
}
I hope this is helpful to you!
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.
As per the Simple Triggers documentation:
The
onSelectionChange(e)trigger runs automatically when a user changes the selection in a spreadsheet. MostonSelectionChange(e)triggers use the information in the event object to respond appropriately.
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();
}
}
I hope this is helpful to you!