Push() Function

In Google Apps Script, the push() method is an array method that allows you to add one or more elements to the end of an array. It modifies the original array in place, and returns the new length of the array after the elements have been added.

Here is an example of using the push() method to add elements to an array of country names:

var countries = ["USA", "Canada", "Mexico"];

// Add elements to the end of the countries array
countries.push("Brazil", "Argentina");

// Output the modified array
Logger.log(countries); // Outputs: ["USA", "Canada", "Mexico", "Brazil", "Argentina"]

In this example, we call the push() method on the countries array to add two elements, "Brazil" and "Argentina", to the end of the array. The original countries array is modified in place, and the modified array is outputted to the log using the Logger.log() method.

You can also use the push() method to add a single element to an array, such as a value or an object:

var numbers = [1, 2, 3];

// Add a single value to the end of the numbers array
numbers.push(4);

// Add a single object to the end of the numbers array
var person = { name: "John", age: 30 };
numbers.push(person);

// Output the modified array
Logger.log(numbers); // Outputs: [1, 2, 3, 4, { name: "John", age: 30 }]

In this example, we call the push() method on the numbers array to add a single value, 4, and a single object, person, to the end of the array. The original numbers array is modified in place, and the modified array is outputted to the log using the Logger.log() method.

In conclusion, the push() method in Google Apps Script is a simple but powerful tool for adding one or more elements to the end of an array. It can be used in various contexts to modify and transform array data, such as adding new values or objects to an existing array.