r/GoogleAppsScript • • Mar 01 '22

Question Experienced Excel VBA programmer trying to make the move to Google Sheets/Apps Script

I have a lot of experience working in Excel/VBA but am looking to make the move to Apps Script to automate Google Sheets.

In Excel VBA I can set/get various properties of a workbook/worksheet/range without having to first activate the object. For example, I can write

Workbooks("Book1.xlsx").Worksheets("Sheet1").Range(Cells(2,1)).Value = 1

to set the value of cell A2 to 1 without having to first select cell A2. Similarly, I can read the value from cell A2 and set it to a variable without having to select the cell

dim myVariable = Workbooks("Book1.xlsx").Worksheets("Sheet1").Range(Cells(2,1)).Value

Is this possible in the Google Sheets? Looking at the documentation it looks like the methods all involve setActiveWorksheet and setActiveRange. Is there a way to reference a workbook/worksheet/range without having to select it?

Thank you for any help you can provide. My apologies if I'm overlooking something obvious.

6 Upvotes

9 comments sorted by

2

u/ResponseIntel Mar 01 '22

Below is a way to do this in GAS there are other ways like using the active sheet or the sheet ID instead of the sheet name. You can also call on sheet/ranges in other documents if you want to.

//This is the equivalent to your line in GAS
var myVariable = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange(2,1).getValue()
//I prefer to name the sheet and call upon it when I need it multiple times in the same code.
//name the sheet
var mySheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
// get the variable on this sheet
var myVariable = mySheet.getRange(2,1).getValue();
//gets an array of variables on this sheet
var myOtherVariables = mySheet.getRange(1,1,5,2).getValues()
//Sets a value on this sheet
mySheet.getRange(1,1).setValue("This is the first cell")

1

u/ShaneFerguson Mar 02 '22

Excellent. Those is exactly what I was looking for. Thank you.

1

u/ResponseIntel Mar 02 '22

No problem. Sorry that didn't look as clear as it should be but if you copied it and pasted it in the script editor, hopefully the commented out lines were separate from the code.

2

u/_Kaimbe Mar 01 '22

SpreadsheetApp.getActive() returns the Spreadsheet object (workbook) that the script is bound to, if created from a Spreadsheet through the menu (or probably other ways that I'm not familiar with and aren't important right now).

Only SpreadsheetApp.getActiveSheet() and Sheet.getActiveRange() really depend on selection and are more useful for thing like custom functions that you want to act on the selection.

You can open Spreadsheet that are out of the current scope with SpreadsheetApp.open(fileObject), SpreadsheetApp.openById()/.openByUrl()

let ss = SpreadsheetApp.getActive(); is something you'll see a lot on stackoverflow and here. It's just assigning the Spreadsheet object to a variable so that you can do stuff like let sheet = ss.getSheetByName('Sheet1') and then let range = sheet.getRange("A2"). You can do that all in one line (SpreadsheetApp.getActive().getSheetByName('Sheet1').getRange('A2').setValue(1)) but expanding it out makes it much easier to troubleshoot and expand upon generally.

Last thing, GAS is 99% Javascript with google APIs and classes. Searching for a Javascript problem on stackoverflow will give you 100x more results than a GAS one, if it's a general syntax or other problem that doesn't necessarily have to do with GAS.

3

u/ShaneFerguson Mar 02 '22

This is perfect. Exactly the info I was looking for. Thank you.

2

u/arnoldsomen Mar 01 '22

getActiveSpreadsheet, getSheetByName?

0

u/[deleted] Mar 02 '22

[deleted]

1

u/ShaneFerguson Mar 02 '22

Thanks, I'll take a look

0

u/[deleted] Mar 01 '22

[removed] — view removed comment

1

u/ShaneFerguson Mar 01 '22

Thanks for the response. When you're back in front of a computer can you please provide the equivalent GAS subtract for the two lines of sample code in my post?

Thx