The Array method push() in Apps Script
The Array method
push() adds elements to the end of an array. It returns the new length of the array after the elements are added to it.
This tutorial assumes that you're familiar with:
Basic Apps Script concepts (such as Arrays in Apps Script).
How to create and run simple Apps Script scripts using the script editor in Google Sheets.
array.push(element1, element2, …, elementN)
The elements to be added to the array. These will be added to the end of the array.
push() method returns the new length of the array after elements are added to it.
The code below uses the
push() array method to add two new elements to the array
In Apps Script, you can only run functions. So you have to copy and paste the code snippet below into a function before you run it.
var colors = ["red", "blue", "green", "black", "orange", "purple"]; Logger.log(colors); Logger.log(colors.length); var newLength = colors.push("indigo", "violet"); Logger.log(colors); Logger.log(newLength);
[red, blue, green, black, orange, purple]
[red, blue, green, black, orange, purple, indigo, violet]
In this tutorial, you learned how to use the
push() array method to add elements to the end of an array.
Stay up to date
I'd appreciate any feedback you can give me regarding this post.
Was it useful? Are there any errors or was something confusing? Would you like me to write a post about a related topic? Any other feedback is also welcome. Thank you so much!