Blog

Creating Arrays of Apps Script. Adding, Deleting and Modifying Array Elements

Arrays in Apps Script can be created in several ways:

  1. Declare the name of the array in the line of code and equate it to a sequence of values taken in square brackets;
  2. Read data from a Google SpreadSheet using the .getValues() method;
  3. Dividing a string using the .split([delimiter]) method (usually a space is used as the delimiter delimiter;
  4. PP 3, when a word is used as a string, and as an divide, an empty string.

function createArray() {
  
// ======== HOW TO CREATE ARRAY? ===========

  // 1.) Write
  var arr = [1, 2, 3];
  
  // 2.) Read from spreadsheet
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var arr1 = ss.getDataRange().getValues(); // [[1.0, 2.0, 3.0]]
  
  // 3.) Using string splitting
  var string = 'I am learning Google Spreadsheet';
  var arr2 = string.split(' '); // [I, am, learning, Google, Spreadsheet]
  
  // 4.) Using string splitting
  var arr3 = 'word'.split(''); // [w, o, r, d]
  
}

There are several ways to add elements to an array:

  1. The simplest one is to equate a specific element of an array (by specifying its index in square brackets) to the desired value.

  2. If the maximum index value is exceeded by more than one, then all elements between the old and new maximum indexes are automatically set to null .

  3. Adding elements to the end of an array using the .push (value) method;
  4. Adding elements to the beginning of the array using the .unshift (value) method;
  5. Deleting an element with maximum index .pop () ;

  6. The value of the deleted element can be assigned to a new variable.

  7. Deleting an element with a null index .shift () ;
  8. Address change of array elements using the .splice (index, hawManyDelete, whatInsert) method;

  9. Where:
    • index - index number from which indexes will be inserted / deleted;
    • hawManyDelete - the number of items to be deleted. If hawManyDelete = 0, there will be no deletion;
    • whatInsert - the value of the element (s) that (s) will be inserted, starting at index number index .
  10. Method of changing the order of indexes in an array from direct to reverse and vice versa .reverse() ;
  11. The method for sorting the elements of the array .sort() . Without a parameter in the form of a function, it is used extremely rarely due to its limitations: it only sorts unicode strings in ascending order.

function addDeleteItems() {

// ================ ADD ITEMS =======================

  // 1.) [index]
  var arr4 = [1, 2, 3];
  arr4[3] = 4; // [1.0, 2.0, 3.0, 4.0]
  arr4[5] = 6; // [1.0, 2.0, 3.0, 4.0, null, 6.0]
  arr4[-1] = 0; // [1.0, 2.0, 3.0, 4.0, null, 6.0]
  
  // 2.) .push(value)
  arr4.push(7); //  [1.0, 2.0, 3.0, 4.0, null, 6.0, 7.0]
  
  // 3.) .unshift(value)
  arr4.unshift(0) // [0.0, 1.0, 2.0, 3.0, 4.0, null, 6.0, 7.0]
  
// ================= DEL ITEMS ======================

  // 4.) .pop()
  var x = arr4.pop(); // [0.0, 1.0, 2.0, 3.0, 4.0, null, 6.0]
  
  // 5.) .shift()
  var x = arr4.shift(); // [1.0, 2.0, 3.0, 4.0, null, 6.0]
  
// ===== ADD (INSERT), DELETE, CAHGE =================

  // 6.) .splice(index, hawManyDelete, whatInsert)
  arr4.splice(4, 0, 5); // [1.0, 2.0, 3.0, 4.0, 5.0, null, 6.0]
  arr4.splice(5, 1);    // [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]

// ============ CHANGE ORDER =========================
  // 7.) .reverse()
  arr4.reverse(); // [6.0, 5.0, 4.0, 3.0, 2.0, 1.0]
  
  // 8.) .sort()
  arr4 = [6.0, 5.0, 404.0, 41.0, 4.0, 3.0, 2.0, 1.0];
  arr4.sort();
}

You can find more information and examples in this video (RU voice):