Google Apps Script Sheets Cycle Through Values

If you need to run a function on values in a whole column inside a sheet, and then populate the results in another column, then this example will assist you in processing all of those values.  For the example listed here, we are going to take a list of email addresses from Column A, and use an Apps Script Function to calculate the domain and put it in Column B.  When the function runs, it will process all values, and output the results.

function stripDomain() { // placing in column B

  var ss = SpreadsheetApp.getActive();

  var active_sheet = SpreadsheetApp.setActiveSheet(ss.getSheets()[0]);

  var last_row = active_sheet.getLastRow();

  var range = active_sheet.getRange("A2:A" + last_row);

  for (row = 2, len=last_row; row <= len; row++) {;

    var modEmailDomain = active_sheet.getRange("B" + row);

    var currentemail = active_sheet.getRange("A" + row).getValue();

    domainArray = currentemail.split("@");

    domain = domainArray[1]




The above script starts scanning in ROW 2.  It is considering ROW 1 as a header row. 

Now in order to make this more seemless, once the script is created, you should not need to jump into the Apps Script interface to run the function.  You can create a menu that appears directly on the Google Sheet.  For more information on a generic example of this, please see Google Apps Script in Sheets Menus.  You can create your own menu, or use the code below for this specific example.

function onOpen() {

  // Create the menu on the sheet

  var ui = SpreadsheetApp.getUi();

  ui.createMenu('Apps Script Functions')

      .addItem('Domain Splitter (column B)', 'stripDomain')