Move Sheet Tab to the Far Left Using Script

2019-07-02 11:47发布

问题:

Is it possible to have a small script that will re-arrange the order of the sheet tabs moving the currently selected sheet tab to the far left (the beginning) of all the tabs in a Google Spreadsheet?

回答1:

Yes and No. It can be done by sweet name, but not by the current sheet you are looking at. Below is some code I wrote that moves a sweet to the first position every day.

function tab() { 

  var reportDate = new Date();
  var tabName = Utilities.formatDate(reportDate, 'MST', 'yyyy-MM-dd').toString(); 
  var tab = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(tabName);
  SpreadsheetApp.setActiveSheet(tab);
  SpreadsheetApp.getActiveSpreadsheet().moveActiveSheet(1);

} 


回答2:

function BacktotheFront() {
   SpreadsheetApp.getActiveSpreadsheet().moveActiveSheet(1);
}


回答3:

This adds two menu items in a menu called "Move Tabs" which will move the current tab to either the far left or far right.

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Move Tabs')
    .addItem('Move Current Sheet To Far Left', 'moveCurrentToFarLeft')
    .addItem('Move Current Sheet To Far Right', 'moveCurrentToFarRight')
    .addToUi();
}

function moveCurrentToFarRight() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = spreadsheet.getSheets();
  spreadsheet.moveActiveSheet(sheets.length);
}

function moveCurrentToFarLeft() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  spreadsheet.moveActiveSheet(1);
}