Adam Gosnell
Adam Gosnell

Reputation: 31

Formatting Emails from Sheets in Google App Script

I'm trying to format emails from a Google Sheet into a string that I can use as email recipients. I've gotten the emails, and I'm using a for loop to try to remove empty values, but it's not catching ("true" never triggers). Can someone tell me what I'm doing wrong?

Here's my sample sheet: https://docs.google.com/spreadsheets/d/1RPw_sNHF3JTlMYBD_Mk1BTOHDLaXya8IaeuU3YkxHUQ/edit?usp=sharing

Here's my code:

function affiliateReportReminder() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Sheet1');
  var lastRow = sheet.getRange("A1:A").getLastRow();
  var emails = sheet.getRange("A1:A" + lastRow).getValues();
  for (i = 0; i > lastRow; i++) {
    if (emails[i]='') {
      console.log(true)
      emails.splice(i,1);
    }
  } 
  console.log(emails);
};

Upvotes: 0

Views: 111

Answers (2)

Cooper
Cooper

Reputation: 64032

comma separated list of email recipients

function affiliateReportReminder() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('Sheet1');
  const emails = sh.getRange(1,1,sh.getLastRow()).getValues().flat().join(",");
  console.log(emails);
}

Upvotes: 4

Yuri Khristich
Yuri Khristich

Reputation: 14502

By the way, if you want to remove empty elements from array you can do it with array.filter(String) method. In this case it could be:

var emails = sheet.getRange("A1:A" + lastRow).getValues().flat().filter(String);

array.flat() makes 1D array from 2D array: [[a],[''],[b]] --> [a,'',b]

array.filter(String) removes empty elements: [a,'',c] --> [a,b]

Upvotes: 2

Related Questions