Home > Mobile >  Formatting Emails from Sheets in Google App Script
Formatting Emails from Sheets in Google App Script

Time:02-11

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);
};

CodePudding user response:

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);
}

CodePudding user response:

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]

  • Related