From your following reply,
My spreadsheet consists of 4 tabs , i would like for the script to be applied to every tab whenever i select it. This now works if i select the tab and refresh it (i guess because it's an onOpen() function). I would like it to work without refreshing it, just by selecting the tab.
I believe your goal is as follows.
- When you active a tab on Spreadsheet, you want to activate the cell of the last row of column "A".
In this case, I remembered this thread. I thought that this method might be able to be used for your situation. When this is reflected in your script, how about the following sample script?
Sample script:
Please copy and paste the following script to the script editor of Spreadsheet and save the script. And, in order to initialize the active sheet, please reopen Google Spreadsheet. By this, onOpen
is run and the initial sheet name is set to Properties Service.
function onOpen(e) {
const prop = PropertiesService.getScriptProperties();
const sheet = e.range.getSheet();
const sheetName = sheet.getSheetName();
prop.setProperty("previousSheet", sheetName);
yourScript(sheet);
}
function onSelectionChange(e) {
const prop = PropertiesService.getScriptProperties();
const previousSheet = prop.getProperty("previousSheet");
const sheet = e.range.getSheet();
const sheetName = sheet.getSheetName();
if (sheetName != previousSheet) {
yourScript(sheet);
}
prop.setProperty("previousSheet", sheetName);
}
// --- This is your script.
function yourScript(sheet) {
var lastrow = sheet.getLastRow();
var range = sheet.getRange(lastrow, 1);
sheet.setActiveRange(range);
}
- When you select another tab,
onSelectionChange
is automatically run. And, it checks whether the tab is changed. When the tab is changed, the function yourScript
is run. By this, the last row of column "A" is activated.
Testing:
This script is used, and the following situation is obtained.

In this sample, the active cell is on cell "A1" in the 1st sheet. But, in the actual script, when Spreadsheet is opened, the last row of column "A" is activated.
References: