How to create a spreadsheet script that moves to the next sheet tab every minute?

jamc41

I have a spreadsheet displaying information on two monitors. The file has anywhere from 5 to 30 tabs. I have been trying to get the following script with a time-driven trigger to move from one sheet tab to the next every minute, however I have two problems:

1) - I need the loop to skip four tabs (they have fixed names) and I currently can't figure out a workable solution, and
2) - the time driven trigger of 1 minute is not doing anything, not working.

Here are two scripts I've been testing and tweeking to see if I find the one that works:

Script 1:

function MoveNext() {
  var spreadsheet = SpreadsheetApp.getActive();
  var nextSheetIndex = spreadsheet.getActiveSheet().getIndex() + 1;

  if (nextSheetIndex > spreadsheet.getSheets().length) { nextSheetIndex = 1; }
  spreadsheet.setActiveSheet(spreadsheet.getSheets()[nextSheetIndex - 1],true);

And Script 2: this one aims at skipping the four tabs I don't want to loop:

var ss = SpreadsheetApp.getActive();
var sheets = ss.getSheets();

for (i = 0; i < sheets.length; i++) {
  switch (sheets[i].getSheetName()) {
    case "T1":
    case "T0":
    case "Summary Panel":
    case "Flight Info":
    case "Template":
      break;
    default:


      var nextSheetIndex = ss.getActiveSheet().getIndex() + 1;
      if (nextSheetIndex > ss.getSheets().length) {
        nextSheetIndex = 1;
      }
      ss.setActiveSheet(ss.getSheets()[nextSheetIndex - 1], true);
PeterT

You'll have to add your timer code yourself, but here is an example of how to move to the next "approved" sheet.

function MoveNext() {
  var sheetsToSkip = ["Sheet3", "Sheet4", "Sheet7"];
  var thisBook = SpreadsheetApp.getActive();
  var sheetCount = thisBook.getNumSheets();
  var thisSheet = SpreadsheetApp.getActiveSheet();
  var thisSheetName = thisSheet.getName();
  var thisSheetIndex = thisSheet.getIndex() - 1;  //subtract one to get the array index
  var allSheets = thisBook.getSheets();

  var i = thisSheetIndex;
  var notDone = true;
  while (notDone) {
    if (i == (sheetCount-1)) {
      i = 0;
    } else {
      i++;
    }
    Logger.log('next sheet index is ' + i);
    var nextName = allSheets[i].getName();
    if (sheetsToSkip.indexOf(nextName) == -1) {
      var nextSheet = allSheets[i];
      Logger.log('next active sheet should be ' + nextSheet.getName());
      thisBook.setActiveSheet(nextSheet);
      notDone = false;
    }
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How To Use Google Sheets API v4 To Create New Sheet or Tab in Spreadsheet with PHP

Script for getting the tab/sheet name not working in google spreadsheet

How to create random number every minute

How do I make this function in Google sheet run "every minute"?

How to upload a CSV file to a new tab(sheet) in the google spreadsheet

How to show confirm modal before user moves to next tab?

How to send inputs from google spreadsheet sidebar into sheet script function?

How to copy spreadsheet's sheet into another spreadsheet without creating a new sheet copy when using google sheet script?

Class ClockTriggerBuilder: Specific Date and Time (only once) Trigger in Google Sheet Script keeps repeatedly executing every minute

how to run this script in cron.d every minute?

MS Excel - create a screenshot of every sheet (using script?)

crontab execute a script every minute

I would like to create an Excel Spreadsheet where every sheet is a list. Any advice on how I can do this. Do I need to make a list of lists?

Pressing tab moves to one after the next control

How to create a formula for every row in a column in Google SpreadSheet?

Google Spreadsheet: How can I leave the Script Editor open in another tab after refreshing a spreadsheet

How would I create a Bash script that moves files?

How to create a new sheet in excel with powershell script

Google App Script Copy a single sheet and create a new spreadsheet in specific folder

How can I move a range from one Google sheet to the next blank row in a different sheet/tab

How to append to a Sheet within a Spreadsheet

Google sheet script tab color

Copy sheet to another spreadsheet [Google Apps Script]

Google App Script Rename Spreadsheet & Sheet

How to access the cell next to the selected cell in Google sheet using script?

How to create a table that adds a row for every sheet created?

How to create dynamic Tab menu in java script?

How can I create spreadsheet with included gs script by API?

How to Create a Spreadsheet in a particular folder via App Script