There is no need to memorize all of these methods at once. The table below lists the array methods available in Apps Script along with a brief description of what it is used for. We already discussed the length property of arrays.Ī method is a property that has a function value associated with it. This means they can have properties and methods. Logger.log(Array.isArray(age)) //false Array methodsĪrrays are a special type of object. Array.isArray(valueToCheck) //checks if valueToCheck is an array var colors = This function returns true if valueToCheck is an array and false otherwise. You can check if a value is an array by using the Array.isArray(valueToCheck) function. This is because the data you're working with will usually be stored in Google Sheets. In Apps Script, you will be working with tabular data like this a lot. Remember to separate the values within an array with a comma (this also applies to the three inner arrays which are the values contained within the outer array). Essentially you're creating an array whose values are also arrays. You can also create these tabular arrays with a single statement like the one below. Logger.log(students) //4 (Rakesh's age is now correct) You can fix it by assigning the correct value to students. Let's imagine that Rakesh's age is actually 4 and you entered 5 by mistake. You can also change the value at a specific location. Here are a few more examples: Logger.log(students) //Ryan In the above statement, students is the second student's info. You can also get the second student's grade using a single statement. Var grade = student //The student's grade is the 4th value. var student = students //Since array indices start at 0, the value at index 1 is the second student's info We need to first get the second student's info and then get their grade. Let's say we want to access the second student's grade (ie, the selected cell below). Logger.log(students) //undefined (there are only 5 students so Apps Script returns undefined when we try and access the 6th value)Īs you can see above, the values stored in the array students are themselves arrays. Let's try accessing the values stored in the students array. Each of those arrays contains 4 values (first name, last name, age and grade). Logger output (an array containing 5 array values): Then, we're going to create another array called students using the five arrays we just created. First, we are going to create an array per student to store their information. We are going to create an array that stores all of this information. There are 5 rows in the table and each one contains information about a student. Logger.log(colors.length) Using arrays to store tabular dataĪn array can also contain other array values. The property length will tell you the number of values in the array. Try it out.Īll arrays in Apps Script have certain properties and methods to help you do more with the values stored in them. So, Logger.log(colors) will return undefined. In the above example, there are only six colors in the array colors. If you try accessing a value that does not exist in the array, Apps Script will return the value undefined. You can access the value at a specific index by using the brackets notation. Since the indexing system starts at 0, the last value in the array will have the index N - 1, where N is the number of values in the array. The color at index 0 is "red", 1 is "blue" and so on. The indexing system starts at 0 and not 1. Logger.log(colors) //Īrrays have an indexing system to help you access values stored at specific positions. You list a number of values within square brackets ( ). Here is how you declare and initialize an array called colors. You will use arrays extensively while working with Google Sheets using Apps Script. An array is a special type of object that is used to store a list of values.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |