How to make all sheets of a Google spreadsheet show A1 in upper left?

后端 未结 1 345
半阙折子戏
半阙折子戏 2020-12-18 16:44

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

相关标签:
1条回答
  • 2020-12-18 17:30

    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();
      });
    }
    

    I hope this is helpful to you!

    References:

    • Class Range - Method activate() | Apps Script

    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();
      }
    }
    

    I hope this is helpful to you!

    References:

    • Simple Triggers | Apps Script | Google Developers
    0 讨论(0)
提交回复
热议问题