JavaScript 2 Dimensional Arrays, Reading & Writing Array To and From Ranges - Part 2 - Apps Script

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
when we talk about two-dimensional array that basically means array over race so I'll show you what I mean so let's start with new array variable and I'm gonna create this now in this array I'm gonna place an array like this and then comma another array and then comma yeah let's just leave that to for now so I'm gonna go inside of the first array and I'm gonna do first element to be Linda and then comma and the second element will do some number let's say that's our age and then we'll do another person and see I'm not putting age 27 in quotes because that's a number so I'm gonna go here and do the second person something like this so now I have this new array that's the the bracket starts here and it ends here in the end which has two elements in it this is the first element this is the second element and each element in its turn is an array as well so this is also an array and that's an array so now let's get to different parts of this array so for example if I do new array 0 new array is this that has two arrays in it so the first one 0 which is position 0 is gonna be the first thing in that array which is this part just before this comma so it's gonna give me this entire array as a result now if I wanted to get to that person's name to Linda if I look inside of this array it's the first thing which means since the first starts from 0 I'm gonna do 0 and another square brackets and another 0 so if I run this and look at my results say I'm getting her name now if I change that 0 to 1 Rhonda's take a look see it gives me a twenty seven which is the number twenty seven and that's because this first zero is gonna give me this array and inside of that array we're gonna get zero one which is the second element which is 27 now if we wanted to get Lisa's name that would be if we look inside of this array this is the first one which is zero this is the second one which is one so I'm gonna do one and then zero because inside of this array the first thing is the name so 1 0 would give us the name 1 1 would give us the age so if I look at this and by the way you can get this lock by pressing command enter or control enter so that gives me Lisa sometimes to make this somewhat easy to read because these lines can get pretty long people just move these two different lines and you can do that something like this just keep in mind this doesn't change anything about how this code works it is still going to run the same way so this is still our end of statement which is the semicolon so just to show you if I run this I'm still getting the same thing now we're looking at this two-dimensional array so let's say we want to add something to this two-dimensional array so to do that I'm going to do that name of the array and then dot and then we add it by using push now keep in mind that what I'm doing I'm adding to this new array which has two things in it the first thing and the second thing and by the way we can find out how many things it has in it if you remember by doing new array dot length so if I run this two things again so if I do this push now I want to add another person now but that person has again it comes with all this information together so it's the name of the person and the age which means now instead of just pushing something like Steve I need to push an array so I'm gonna do the square brackets and then the comma and then at his age now if I run this it should tell us there is one more thing in our array which means three right but if I simply just log the array so I can see what it looks like you'll see that what we have is this array see with three arrays in it so that's first one this is the second one and finally there is this new one that we just did which is Steve so we're pushing Steve to this now if I take new array and do something like shift now obviously if you're doing C on shift on shift if you remember adds a new element to the beginning which means you would still use the same thing as you would do with push it would only add that in the beginning before Linda but shift if you remember drops the first element so the first element in this is gonna be this entire array so if I run this you'll see now we got only Lisa and Steve we just got rid of the rest by doing shift if I do shift again we're gonna remove the next first element and so on the same applies to pop right so if we do pop it's just gonna remove this entire line that we're adding here just keep in mind if you're pushing or doing any of this methods to push on shift like these methods that I just mentioned over here then now because you're inside of an array of arrays you're basically adding a whole array to an array instead of just one thing now you could still add to a particular part of this so let's say we decided that in addition to name and age we're also going to have their location to do that what I'll have to do now I'm gonna push to the appropriate spot of this so what I mean by that is that after I do this new array first I'm going to do the index which is zero so the zero is gonna be this which is also an array the first thing and then I'm gonna do that push again and then are we gonna add a location so if I save this I've run this if I open it see what happened we added inside of that first array another element which is Chicago now I probably want to do that for Lisa as well so I would do one because Lisa is the second one here and we'll push to that array which is a third element here and we'll do something like Sanja las' so run that here it is so we're adding inside that array now if you wanted to change something let's say we're trying to change Lisa's age from 32 to 33 then we have to get to that by saying new array and then Lisa is this whole line for her is 0-1 and then I have to change the name right so that's the first element in here so that would be zero and then I would say that is actually it's a number so say we're changing through 33 so now if I run this we'll see that hopefully Oh wrong spot let's see what I did we don't want to change the name we want to change the age right so this would be 1 so that one is the second and then one is the second again so run this again so now we got Lisa and the new age 33 you can obviously change the whole line so let's say we're trying to change this entire line for Linda all together so we can do that's the first line and we can say now that equals to now the line itself is an entire array so I'm going to change it to an array so we'll do Bob and then 22 so if I run this see we have Bob here 22 I forgot we're adding cities to that as well so let's do that good so now we're basically changing the line so the only method we didn't use is this can cat so first of all let me just comment these lines and let's create another array I'm just gonna copy this so let's say let's give this a different name so now if I take this new array so let's create a new variable I'm gonna take that and I'm gonna can catch that new people are right to it log that result so if you look here again what we have is basically two of them combined together now when you're doing this you have to make sure that if you're concatenated two arrays is that they're of the same size and type so see how we have this array that has this array in it so basically we're gonna take that and we're gonna combine them so this wouldn't work if your second array was just something that was like this if I take that and I can cat that see what I'm gonna get is well we got this array that's the first one we got the second one and then this third one it just added as two different items so instead of having an array of two things we just ended up having like third and fourth parts in this main array which we don't want to do so you want to be careful with things like this so if you're passing or combining two arrays you have to make sure they're of the same structure so if you wanted to work with the single name you would put something like this so that's an array that has an array and now if I run this see now that works let's also do one more thing something like this so I'm gonna run this and open so you can see that this is adding the array to the appropriate spot but you most likely don't want to do something like this because you want to make sure that it's kind of the same structure as I said before so if you have this name and age and there is no location attached to it then you probably don't want to concatenate another array that has a location so I'm gonna get rid of this now one of the things that you need to know is that every time you access Google sheets and you're grabbing a range of things and you're pulling to script you basically can just convert it to an array very quickly so let's do that so I'm just gonna take this and comment this out and I move down here oh this needs to be commented to okay that's good now what I'm gonna do first actually I'm gonna leave this alone and it's getting too confusing I'm just gonna create a new function call this one test so make sure I change this here I'll have to save it first so it shows up so first what we want to do we want to access the spreadsheet so I'm gonna do variable SS and spread sheet app and I mistyped something they're not there it is so we're gonna get active spreadsheet and then we'll get active sheet so then I'm gonna go inside of that active sheet and I'm gonna use this method get' and range so get range is what we want so the starting position 1 1 so that would be the first row first column here and then we want to get 4 rows and 3 columns so comma 4 comma 3 and then what we do with dot and do this method called get values you have to be careful don't use get value because get value only works if you have one cell one single cell this is a whole bunch of cells so we have to do get values and when you do this let's create a variable data so let me just log this out now perfect data run this and if I look at this see what I'm getting this was my spreadsheet I'm getting an array of a race so that's the main array and in it it has the first array the second array the third array and a fourth array basically what we're going to get is that each line is one of those arrays inside so this first line a b1 is gonna be see this first array inside of our array and then we have this can one which is CD and two and then so it goes and the way you can think about it is that this first main array has all this rows in it this is the first row second row third row fourth row and then inside of that this separation is the columns so this first one is the first column second is the second column third is the third column so that's how you can simply just grab data from Google sheets and basically read it into an array and then you can also take an array and place it in Google sheets so let's do an example of that I'm actually gonna just copy this and remove this comments so here we go so this array has what it has this main array and it has basically two rows in it this is the first row this is the second row and each row has two columns so I want to just place this someplace on my spreadsheet so let's say we'll put it right here so to put it right here first I'll have to get the range where I'm actually placing it so I'm gonna go to my spreadsheet and then get range and then I have to do the starting row and starting column so the starting role for this is one and the starting column is 1 2 3 4 5 so 1 and 5 and then we'll have to pick what is this the two columns and two rows right so it has to go like this so number of rows is gonna be two and number of columns is gonna be two as well and then what we're gonna do we're gonna do is set values again don't do value because it's not one cell is values and then as a value I'm gonna pass that array so if I run that I go back to my spreadsheet see we just placed that array in here a couple of things that can be modified here to make this better so if I decide I'm adding another line here then I do a comma here and add another line now if I try to run this see I'm getting an error and the reason I'm getting an error is because I said I want to place this array in this area which only has two columns and two rows but now I have three rows in my data and two columns so I have to now go back and modify this to three instead of doing that because we don't want to do this modification every single time instead of typing three we can simply just calculate the length so instead of typing three I'm gonna do that array dot length and that should just calculate how many rows we have here and just correct it for us so if I run this now I go back see it worked no problem and then finally this two can be also modified in case we decide we're adding new columns or something like that so instead of typing - we just have to calculate the length of this array so any one of those should be fine because if we calculate what the length of this it's to calculate the length of this it's - it doesn't matter so I'm just gonna grab the first one and the first one means we're gonna do that array 0 that's gonna give us the first thing in this main array which is this line and then we need to calculate how many elements we have in that so we'll do dot length as well and this code should run fine again we can see no problem but the difference is now if we decide we're adding something here I'm gonna run this it will automatically figure out how many columns and place it on our spreadsheet so that's a pretty good start I think for our race I think that's enough for this particular video I'm gonna go into more complicated array methods in the next video hopefully you've enjoyed it thanks for watching please subscribe and I'll see in the next one
Info
Channel: Learn Google Spreadsheets
Views: 37,338
Rating: undefined out of 5
Keywords: JavaScript, 2 Dimensional Arrays, Reading, Writing, Array, Range, Apps Script, google sheets, tutorial
Id: FuHuqhrx_rY
Channel Id: undefined
Length: 22min 28sec (1348 seconds)
Published: Sat Oct 20 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.