Hey there stranger!

Sign up to get access.

Spreadsheet Automation 101 Lesson 2: Bracket Notation

About this Tutorial

Learn how and when to use Bracket Notation in an array. No memorization is needed!. You’ll also be able to see the gap between what it is that’s missing.

Video Transcript

00:01 Hello, welcome to BRCA Notations. So this is pretty cool. And probably one of the weirdest and mind altering kinds of parts of learning app script.
00:13 BRCA notation is used whenever we have an array. So arrays are very important when dealing with app script, especially in like emailing many people or or anytime we're just managing lots of data in like a row or a column or in a table of rows and columns.
00:29 And we wanna parse that data, right? We wanna go through the for loop and we wanna access sort of all parts of that array.
00:40 Because what happens is when we use dot get values spreadsheet app and all that stuff, and then get values of a range, it creates an array for us.
00:49 So I wanted to go through bracken notation and get this show you lots of ways to do it because, and lots of ways to figure it out because I could sort of explain it to you all and it might just wash over your head, such as it did for me.
01:09 Only when I started using it and understanding how to use it did I figure out, I didn't really have to memorize it, I just had to know the process of figuring it out.
01:18 So bracket notation is pretty simple. It has these brackets and around some type of array, we'll name that array and then that bracket, this, these brackets will be around a number.
01:32 So the number zero or one or two, three, or in the case of four loops, we'll use I a lot.
01:39 And this is going to tell, this is going to tell the script to get which element of the array arrays are always like in an order from 0 1 2 to n and having array zero in brackets will, will be the first item.
02:03 Now, if you have an array inside of an array or an array of arrays, which you can have you'll access by two brackets.
02:11 And the first item of the first array will be zero, zero. And then you can also say, the second item of the first array is gonna be zero one, the second array, but the first item is gonna be one zero.
02:28 And I'll show you, you don't have to memorize this in any way. You just have to follow along as we do in here, and I'll show you sort of how to figure it out, right?
02:38 We may make some mistakes and we may learn it as we go and experience it. So let's first start a function, right?
02:48 We just start any function, and we can create an array. We can create an array in two ways. We can say variable, just name any array.
02:57 And this new array with a capital A and parenthesis must have parenthesis on here. Or we can just use these two brackets to create a new array.
03:09 We can log, let's log this in array and log another array and just see what they are. And I want to do this because I like showing you that you should be using logger a lot.
03:26 This is a way to figure stuff out and see they're the same thing. One note I will mention is that you may know that in the logger we can write like this is an array in a string and then add it to this so that it combines it.
03:46 Something will happen though, and I want to, I want to you to see this. If we run this again, Notice the brackets are gone.
03:56 This is an array, has nothing in it. It's because it's an empty array. So it has no items, but the brackets are gone.
04:02 Why is that? That's because when we add a string at the beginning of this, it will string a file, turn this array into a string.
04:09 So let's say we, instead of another array, we actually have it item one, item two, item three. Let's say that.
04:20 And when we log it, it will show up. Item one is not defined. I think it needs to actually be in quotes.
04:28 That's why <laugh> it's thinking it's a variable. So let's put in quotes, these three strings. There you go. Item one, item two, item three.
04:40 And now if we put this, let's see what happens if we do this. This is another great, and again, I'm showing this to you.
04:50 Oh, we need to add a plus so that, you know, you can do this too if you need to process and figure out what am what are you doing in your script?
04:57 Or if you copy and paste a script from online and it has a raise, these are how you can figure out what a rays are doing And check this out.
05:07 So it combined them with, with commas, and it took the quota out, right? So it took each of those items and put it in here.
05:17 And that's because it, it, the first thing in here is a string, and it's trying, and it turns the array into a string.
05:23 But let's see if we don't have that, it's an array. So it has those brackets. So those two things are the same when logged it and with a string, it changes it to a string.
05:37 Okay, let's look at bracket notation. Now that we have an array, now that we have our arrays we can also create a third array.
05:48 And this one is going to be an array of arrays. So we have AUM one, AUM two, and in this second part we'll put item three, so we can see how to get those.
06:05 Let's log it as well. Log this third array and it will set up our pallet. Our what we're going to be using bracket notation to get to.
06:19 So we have two different brackets two different arrays here, and we're going to access them through bracket notation. How do we do that?
06:27 Here's what we do. We just name, we just name the array. Let's do logger. So it's gonna be in the case of the, another array.
06:38 Just name it, put the name and then bracket zero. And in this bracket, what it'll do is it should get this item one, And it did.
06:49 So it's the first item in this array. So it's one array across, and it's getting the first item in that array.
06:57 If we do bracket one, it should give us item two. Now, it's always good to give a little hypothesis first, run it and see what ha what happens, right?
07:08 Execute it. And we have item two in our log. We can also just for so sweet, see one to simplify what we're running here, let's run that again.
07:18 Item two, right? We're using the bracket notation in two brackets. And the number one, again, super simple. If we use number two and run it, it will give us item three.
07:31 Okay? So again, arrays start with zero and go to N. However many items are in the array. Now, this third array, how do we access that?
07:41 Well, let's see what happens if we go third array and bracket zero. Let's run that. We're getting the first item, which is an array item one and two.
07:55 So these are two arrays inside of one array. So it's first array, then the second array, then third array. So now let's see what happens if we put in one.
08:06 Now, if your guess, my guess is that it'll be item three. Right? And this is a simple, I've sorry to set this up.
08:12 It should not be item two and correct, it's item three. Okay? So this now is different than the one up here.
08:23 So how do we access the second item in the first item? So item two, we would theoretically do something like this.
08:35 Zero one with two brackets. Let's see, let's, let's see if it works. Item two. Now, if we do, I zero zero here in these brackets, We get item one, right?
08:47 So it's a first item of the first item, and they're all in order, right? It's all ordered. This is not necessarily rows and columns or a, a table.
08:55 It is every array is an order of items. What's really cool about this is that we can create different arrays that all have the same order, and then we can take out parts with the if and push it, right?
09:10 We can do something like take array. Let's take this another array, push and put item four. So now that we take an array and we push one more item to it, pushing just means that we're adding one item to this array.
09:30 I wanna log this new array, the, the array that it, the new array. Let's see. So now it is exactly the same array as before, except it has a fourth item, item four.
09:48 So we can use push to keep adding items to an array. We can use bracken notation to identify what is the item, the thing, the element, the part of the array that we want to get.
10:00 This is really cool and I hope this was really helpful to understand bracket notation. And you don't necessarily have to memorize this, right?
10:10 So if out of all of this, you, you forget everything else, and you only have to, and you can only remember one thing, remember to just try it, try to access things, especially if you're using I and in an a four loop and things are just not working out correctly.
10:27 Just try to get one item, get one thing out of it using this bracken notation.  Again, we can use I here in a four loop, but just go ahead and try to log something like zero and something like zero.
10:41 See what happens. Log it, look at it, look at what it gets, get a hypothesis, what it gets, and then you'll see the gap between what it is that's missing.
10:55 Also try to push things to an array. One of the most common mistakes that happens is that we don't create an array.
11:03 So theran doesn't exist. So when we push to it, like, or we can't or we don't push anything to it, right?
11:10 In in four loops, a lot of times we are trying to just get a few items out of a larger array or a larger list of items, and we just end up not pushing it.
11:20 We're like, okay, we get the, the correct if calculation, and then we're like, okay, return the thing and we only, and we return each of those items, but we don't add them all to a new array.
11:33 So we'll only get like the last item out of it. We'll return the last item, which is not cool. So you can create a new array by literally saying new array with a capital A and parenthesis.
11:46 That's very key to remember, or variable new array equals, and you're just gonna have two empty brackets. And sure you can create an array with items.
12:00 Actually, that's one good thing to do. If you're trying to debug your app script in, and you're like, okay, I'm grabbing the information from this page and I'm putting it into this array, and then I'm taking this array and I'm go doing a for loop on it, or I'm accessing it through this BRCA notation and it's getting, it's not getting anything undefined, or it's or it's a wrong thing.
12:25 Try to create the array that you think it is as just text. So just call it that and see if the actions you're taking are correct, right?
12:36 So if the act, if you create the array you think you're creating and the actions are correct, then you'll get the right answer.
12:42 And so the actual problem will be at the beginning where you get the array. Sometimes Google Sheets is really funky.
12:50 Sometimes when you're grabbing like all of the, the entire row, it'll be a different syntax in the array than if you could grab an entire column.
13:02 It'll be like an array, one array or it'll be an array of arrays. So there's different ways to access that array, either using two or one bracket notation.
13:14 So I hope this was helpful in some way to help you through bracket notation and understand how to access these, because this is really, really powerful.
13:24 I really love working with arrays and being able to access like empowered to access them in this way especially with the Four Loop.
13:36 Hopefully. And, and also if you have any questions, feel free to ask Bite.

Courses

Spreadsheet Automation 101: Introduction to Pre-course Videos

Think Like a Programmer: Develop The Mindset of an Apps Script Coder

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