r/GoogleAppsScript • u/Gavinaldi • May 17 '20
Resolved How to active sheet?!?!?
I'm trying to figure out I don't know what or how.
Can anyone help me?
I am trying to do nothing. Just put text in an Sheet range.
I can't even figure out how to get the activeSheet.
Please kill me!
function learnBasics() {
var app = SpreadsheetApp;
var ss = app.getActiveSpreadsheet();
var activeSheet = ss.getActiveSheet();
// activeSheet.getRange("D2:F9").setValue("OMG"); //This worked fine so I moved on
// activeSheet.getRange(6, 2).setValue("Serious Stuff"); // This worked fine, moved on
//activeSheet.getRange(2, 2, 3, 3).setValue("Serious Stuff") //Chose Sheet2 expecting it to become the new ss but it writes to Sheet1
logger.log(ss) //me in absolute despair trying to see if I can figure the GD active sheet.
}
(a) How do I find out what apps script is going to treat as the activeSheet and (b) do I have any say over it?!?!? Can I specify which sheet I want to be the sheet that something happens to?!?!?
2
u/gh5000 May 17 '20
So active sheet is the sheet that you currently have open and a cell selected.
//activeSheet.getRange(2, 2, 3, 3).setValue("Serious Stuff") //Chose Sheet2 expecting it to become the new ss but it writes to Sheet1
When you say chose sheet2 did you think that (2,2,3,3) somehow selected sheet two? Or did you select the sheet in the browser?
What I would do is
var sheet1 = ss.getSheetByName('Sheet1')
Then sheet1.getRange( etc
And similar for sheet 2 that way you know you are getting the right sheet. Active sheet is mostly useful for custom functions.
1
u/Gavinaldi May 17 '20
Thanks for the reply and the tips!
I meant chose in the Goog Sheets document. On the spreadsheet itself, I switched to the sheet2 because that's what the tutorial said. It told me that if I chose the second sheet, then ss would then become sheet2, and the could would write to sheet2 after saving and running the script again.
2
u/MikeAdrianOne May 18 '20
You can use the .setActiveSheet method to control which sheet is active from your script.
Depending on your use case, I recommend using .getSheetByName instead of .getActiveSheet. This way you can really target where your script works.
2
u/Gavinaldi May 18 '20
Thanks!
I'll look into figuring how to use these functions. Maybe figure out the type of argument? Parameter? date type? to put into .setActiveSheet(Here?); .getSheetByName('andHere?');
2
u/MikeAdrianOne May 18 '20
Here's the documentation for the .setActiveSheet method:
https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#setactivesheetsheet
Basically, it needs the sheet as the parameter/argument.
You can get that by using the .getSheetByName method which accepts a string as the parameter/argument.
Here's an example:
function testSetActiveSheet() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet2 = ss.getSheetByName('Sheet2');
ss.setActiveSheet(sheet2);
}You should see the sheet switch to the active sheet on Google Sheets.
2
u/Gavinaldi May 18 '20
Thanks for the reply!
I'm not trying to be a wiseguy or anything, but are people really supposed to be able to read and understand those?
When I look at the docs, I feel like I might as well be trying to read what a cat walking all over a keyboard is typing.
2
u/MikeAdrianOne May 18 '20
Hahaha! Docs can be confusing at times but once you get a hang of it, it really helps.
You can really do a lot of stuff using Apps Script like automations and even a WebApp.
3
u/lordph8 May 17 '20 edited May 17 '20
Type in this:
var ss = SpreadsheetApp. Including the period.
It will prompt you with a dropdown list with all the options. You want getActive() in this case you could also select openByUrl("URL") or openById("ID").
By typing var you are setting a variable, that variable will = that snippet of code. So unless you redefine ss it will connect to your spreadsheet. From here you want to define your sheets within your spreadsheet. You can say something like this:
var activeSheet = ss.getActiveSheet();
(This is the same as saying var activeSheet = SpreadsheetApp.getActive().getActiveSheet())
You can also define by name:
var log = ss.getSheetByName("Log");
Note: when you press period at the end, and you don't get any options, you probably did something wrong.
From there it is pretty easy.
log.getRange("A1").setValue("Hello Word");