Question

I want to focus the last row on a specific sheet (or if possible on every one of them) so here is my function triggered by onOpen on the sheet:

function FocusLastRows() {
var spsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spsheet.getSheetByName("Usuelle_2013");
var rowInt = sheet.getLastRow();
var colInt = sheet.getLastColumn();
var range = sheet.getRange(rowInt, colInt)
sheet.setActiveRange(range)
}

This event works but if my first loaded sheet is different from the one I want it to focus on it just teleports to the other sheet. Best will be to set an .onOpen on the spreadsheet.

Is that possible?

Was it helpful?

Solution

I found a good alternative for it. It's custom menu with function link to it.

    //Pierre-luc Des. Script

//Build your menu 
function setMenu() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var menubuttons = [ {name: "Dernière ligne usuelle", functionName:  "GOTOLastRow}];
    ss.addMenu("Outils", menubuttons);
} 

function GOTOLastRow() { 
var spsheet = SpreadsheetApp.getActiveSpreadsheet();
var currentTime = new Date();
var sheet = spsheet.getSheetByName("Usuelle_" + currentTime.getFullYear());
var rowInt = sheet.getLastRow();
var colInt = sheet.getLastColumn();
var range = sheet.getRange(rowInt, colInt);
sheet.setActiveRange(range);

}

Now i will have a clickable menu "Outils" which will let me do the function.

OTHER TIPS

This is rather inelegant, but perhaps try just checking whether the activated sheet is the target sheet? Something like this:

function FocusLastRows() {
var spsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spsheet.getSheetByName("Usuelle_2013");
  if (sheet == spsheet) {
    var rowInt = sheet.getLastRow();
    var colInt = sheet.getLastColumn();
    var range = sheet.getRange(rowInt, colInt);
    sheet.setActiveRange(range);
  }
}

The Google Apps Script documentation seems to indicate that you'll need custom libraries to get any other trigger events than the ones provided, and I'm not sure if it's even in-principle possible to trigger only when a certain specific sheet is activated.

I figure checking the sheet with if comparisons is the next-simplest solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top