Home > Enterprise >  Stumped. In need of help to apply two scripts to a single Google Sheets doc that has multiple sheets
Stumped. In need of help to apply two scripts to a single Google Sheets doc that has multiple sheets

Time:09-25

I am very new to scripting and have searched your tremendous forums but just cannot answer my own question. I was able to find script that work for each sheet in a separate file, but can I have 2 scripts applied to a single file, but 2 sheets respectively? Essentially what I am after is this:

I am a pilot and I keep track of my logbook and flight times in a Google Sheets file. I have used this means for years but recently tried to apply some scripts to get to the first empty cell in column A as that is where I start with the date for each entry. The main reason for this is my sheet is now in excess of 3000 rows of entries. The first sheet in my file where I record my flights is titled "LOGBOOK". I have found a script to apply onOpen that will place my cursor to the first empty row of column A each time I open. Below is the script that I use. It works but takes approx 10 seconds to complete. If there is a faster means, I am open to suggestions. I only want to know the first empty cell in column A. This is what I use currently:

function onOpen() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var values = sheet.getRange("A:A").getValues();
  var maxIndex = values.reduce(function(maxIndex, row, index) {
    return row[0] === "" ? maxIndex : index;
  }, 0);
  sheet.setActiveRange(sheet.getRange(maxIndex   2, 1));
}

Screen shot of Sheet 1, "LOGBOOK"

The second sheet in this file is titled "FLIGHT DUTY" In this sheet I want the same function to happen onOpen. I want the cursor to be placed in the first empty cell of column A as well. Again, I start column A with a date input for the given flight duty period. If I break this sheet out to a stand along Google Sheets file, this script works:

function onOpen(){
  var sheet = SpreadsheetApp.getActiveSheet();
  //Find the last cell with data in that specific column (A in this case)
  var lastCell = sheet.getRange("A1").getNextDataCell(SpreadsheetApp.Direction.DOWN);
  //Activate the next cell
  lastCell.offset(1, 0).activate();
} 

Screen shot of sheet 2 "Flight Duty"

If someone much more knowledgable than me can help, I would appreciate any and all support. In my current Google Sheets file, I want Sheet 1 "LOGBOOK" to onOpen, place my cursor to the first empty cell in Column A. At the same time, onOpen, I want Sheet 2 "FLIGHT DUTY" to place my cursor in the first empty cell of Column A of that sheet when I select that tab.

Below is a screen shot of the SHEET names at the bottom. The only sheets that I need a script for are 1 LOGBOOK and 2 FLIGHT DUTY.

Screen shot of Sheet Names

Many thanks in advance!

CodePudding user response:

The spreadsheet (usually) opens on the first tab. You can therefore keep the first onOpen script as it is. For the second tab, you can activate the script when you select this tab ...

function onSelectionChange(event){
  var sh = event.source.getActiveSheet();
  if (sh.getName() != 'FLIGHT DUTY') {return}
  // here your script
}

be careful, sometimes you mentioned FLIGHT DUTY and sometimes Flight Duty

CodePudding user response:

I tested the below code. The last row will be activated in as many sheets as you have. Whichever sheet you open, you will always land at the bottom row. Takes one second to run.

function onOpen() {
  var ss = SpreadsheetApp.getActive();
  var tabs= ss.getSheets();
  for (i=0; i<tabs.length;i  ){
    var lr = tabs[i].getLastRow();
    tabs[i].getRange("A" (lr 1)).activate();
  }
}
  • Related