Academy ↓
Hey there stranger!
Sign up to get access.
Scoping Functions in Apps Script
About this Tutorial
Sheet Resources
Video Transcript
0:18 for this and said it's not working How do I make it work? And also, what is like some things to know about this?
0:23 So, first thing, this is really simple. It's going to be some simple math Uh, but in your case, you may have uh a lot more complicated issues But this should help you figure it out as you do more complicated stuff So, we have a function here main math, which we want to do two things We want to do addition
0:43 and multiplication both Now, we have uh do addition, which is just taking one variable and adding it to the second variable.
0:51 Then we have do multiplication which is taking a variable and multiplying it by a second variable. So, this won't work, uh but, if you want to combine these two into one, we can add this with just copying the function name and putting it here, uh into this function and we can call this function from
1:09 anywhere in uh sheets. Meaning, you can create a new file, let's call this Utilities, and we can move these, these two functions, you can cut them and move them over to here and this function will still run.
1:27 It will still call, reference those functions. There is a serious issue though with these two functions and how they run and I want to show you uh what will happen.
1:38 So we're just going to save these, my function and this one. Uh these won't run. This will, this will give you an error.
1:46 Let's see what that error is. One thing to note is if you are trying to run multiple uh functions and you're trying to test it from here, you do need to select The specific function that you're trying to run so if you're here and do addition, even if I'm physically, like our cursor is here, I still need
2:04 to change it to do addition. That's one weird thing. Uhm, you just have to select that. So let's see what happens if we just run this.
2:11 And we're going to get an error I assume. Yeah. First variable is not defined. So this variable we have here, which we've named first variable, does not exist in this function.
2:23 And we can create, like, uh, what are called, uhm, global functions, but this first variable, we might need not know it yet.
2:41 We can say equals four. But we might not know it. So we might not want to do that. We might actually, in this math function, get that variable and then run it.
2:52 So I'll show you how to run that variable in here. What we need to do is reference this inside of these parameters.
3:01 We need first variable and second variable here. And then, in our do addition here, we need to define these as well.
3:11 Actually, we don't need to define them. We need to define that they're variables and get the variable here. So here we can do variable first variable.
3:20 Variable equals four. Variable second variable equals five. And now, if we do this, and actually, instead of return, er, including returning a result, I will also log this so we can see the result, uh, result.
3:40 There we go. So, if we just run do addition, you'll see the same error as before. Oh, actually, not even an error, we get nothing.
3:51 But now let's run main math, which will call these variables, which will actually get the variables here, and this could be through variable setting or through some other method.
4:00 action you're taking. Let's change this to main math and run, and let's see what we get. We get nine. So, we get the uh actual answer.
4:10 And now we can also take this do addition and get and do do multiplication. And we have to set this up the exact same way.
4:20 So, we need first variable, second variable. We need to keep our variables in check. Because in actuality we can probably rename these and I'll show you that.
4:31 Let's just make sure this works. And we will save it and run the main math again. We're trying to run both of these functions at the same time.
4:43 And let's see, we get it. Perfect. We get the addition and the multiplication 9 and 20. And just to show you that these will work if they're even in a different file, let's cut them from our code.gs and put them in our utilities.
4:58 We'll save both of these files. And again, we will select our main math. And as you notice here, when I select main math, the other ones don't exist.
5:08 You have to go to that other file here. So let's go main math and run and let's see if it works exactly the same.
5:15 Perfect. It works exactly the same. We are going to cut this just to show you. Another issue here, or not an issue but something else, is in this do addition first variable second variable, we have first variable and we can take it all the way.
5:33 We see it's course. We see function do addition first variable result first variable. These are all new. Name the same, but let's just name this first and first.
5:46 In this function, it is the first item here is the first item here. But in here we have first variable and second variable.
5:56 Let's see if this still works. We're going to call main math. And just run it again as, and it should work the same.
6:04 Exactly. Why does this occur? Because in this function up here, main math, we're saying this first variable is equal to this first variable.
6:16 Literally the words first variable. Whereas in this function. The first thing here, is the first thing here, is literally the word first.
6:25 So that's probably a, a terrible kind of tongue twister here. Uh, let me try to make this a little easier to see.
6:34 Uhm, we can call this anything, like coconut. Ah, these variables. Variable names can be literally anything. Variable coconut, and variable, uh, packs.
6:47 Let's see. And so, you see coconut and packs here. But here we see, let's call this first thing, and second thing, and you'll see that you because we have not used them in here.
7:05 There we go. And so again, this will run, this will save it and will run exactly the same as before.
7:09 Uhm, first variable is not defined. I think I just, oh, this first variable is not. There we go. So we need to change this.
7:20 This one to Coconut and this one to Pax. So it tells us exactly what's wrong. Save it and run it again.
7:27 And it still works even though in this function we have completely different variable names. You see Coconut and Pax and here in multiplication we have first variable, second variable.
7:39 Because it's not defined. Just the order of items in our function here that really matter, we can call them anything you want.
7:48 This can cause some issues as you're trying to search for how functions run. If other people make them and you try to run them or you make them and other people try to run them.
7:59 It is good. I think and better to try to keep some variable names but it is very ah common that you will have in here in this function a variable name in which you do not want the same variable name in the function that runs based on that.
8:19 That does happen. Uh and I think it is totally fine either way if you want to keep or maintain variable names or if you want to make sure that it is readable and you can definitely tell which one is first and second.
8:35 But that's really what it is. This do addition first item here is going to be the first item here. I hope that makes sense.
8:44 If you have any questions around scoping and any additional things that you're having problems with, let me know. I think it is really, really awesome that you're able to take a function, break it up into tiny functions, and then run all those tiny functions based on input and outputs, and do things
9:01 with them, and then run them again, or run them uh separately, uh I think it's pretty awesome. This is really useful if you are creating functions that people use, like little automations, I call them semi-automations, where someone is doing one thing, then maybe they need to do another thing, and they
9:19 need that same thing. but then sometimes they need to run both things at the same time, so you want only one button, so it makes it super easy to run a bunch of stuff at the same time based on one click, or one function running, uh or you can also have the option to run each of those things individually
9:35 , so I think that's really awesome that you're able to do that now, and hopefully you can. Bye.
Courses
Spreadsheet Automation 101: Introduction to Pre-course Videos
Breaking Through Errors In Apps Script
Think Like a Programmer: Develop The Mindset of an Apps Script Coder
Tips to Navigating Thousands of Lines of Code In Apps Script
Spreadsheet Automation 101: Functions
Spreadsheet Automation 101: Variables
Spreadsheet Automation 101: Dot Notation
Spreadsheet Automation 101: Camel Case
Spreadsheet Automation 101: Parentheses
Spreadsheet Automation 101 Lesson 1: GetValue - Introduction to SpreadsheetApp
Spreadsheet Automation 101 Lesson 1: Spreadsheet Taxonomy
Spreadsheet Automation 101 Lesson 1: A1 Notation vs Row,Column Syntax
Spreadsheet Automation 101 Lesson 1: getActiveSpreadsheet() vs getActiveSheet()
Spreadsheet Automation 101 Lesson 1: onOpen() Trigger - Custom Menu
This Seems Like Automation
Spreadsheet Automation 101 Lesson 2: Get Values - Introduction
Spreadsheet Automation 101 Lesson 2: Arrays
Spreadsheet Automation 101 Lesson 2: For Loop
Spreadsheet Automation 101 Lesson 2: Bracket Notation
Spreadsheet Automation 101 Lesson 2: Logger.log()
Spreadsheet Automation 101 Lesson 2: If ( ){ } and Checkboxes
Spreadsheet Automation 101 Lesson 2: onEdit() Trigger
Introduction to Spreadsheet Automation 101 Lesson 3
Spreadsheet Automation 101 Lesson 3: MailApp
Spreadsheet Automation 101 Lesson 3: Email Yourself For Loop
Spreadsheet Automation 101 Lesson 3: Send Email Every Week Trigger
Spreadsheet Automation 101 Lesson 3: Email Other People For Loop
Spreadsheet Automation 101 Lesson 4: Access APIs Introduction
Spreadsheet Automation 101 Lesson 4: UrlFetchApp
Spreadsheet Automation 101 Lesson 4: OmdbAPI get ApiKey, get Data in URL
Spreadsheet Automation 101 Lesson 4: OmdbAPI get data in Apps Script
Spreadsheet Automation 101 Lesson 4: JSON (beautifier) and OmdbAPI parameters
Spreadsheet Automation 101 Lesson 4: OmdbAPI Parameter Picker
Automatically Clear Content | Refresh Reuse Recycle Templates
Automate Google Sheets With Zero Experience
Automatically Uncheck A Daily Checklist
Activate A Certain Sheet When Opening a Spreadsheet
Scoping Functions in Apps Script