2

I am trying to get filter values in array, separated by comma in Apps Script. To illustrate, here is my function:

function filterReport(){
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var dataSheet = ss.getSheetByName('DATA');
 var emailds = getColumnNrByName(dataSheet, 'Email');
 var emailFilters = dataSheet.getRange(2, emailds, dataSheet.getLastRow() -1).getValues();

  for (var i = 0; i < emailFilters.length; i++) {

    var payload = JSON.stringify({
    'reportMetadata': {
    'reportFilters': [{  
      'value' : emailFilters[i][0],
      'column': 'EMAIL',
      'operator': 'equals' 
   }]
   }
    });

      Logger.log(payload)

}
 }

This is what I get:

[16-08-09 10:54:34:612 PDT] {"reportMetadata":{"reportFilters":[{"value”:”[email protected]","column":"EMAIL","operator":"equals"}]}}
[16-08-09 10:54:34:613 PDT] {"reportMetadata":{"reportFilters":[{"value":"[email protected]","column":"EMAIL","operator":"equals"}]}}
[16-08-09 10:54:34:613 PDT] {"reportMetadata":{"reportFilters":[{"value":"[email protected]","column":"EMAIL","operator":"equals"}]}}
[16-08-09 10:54:34:614 PDT] {"reportMetadata":{"reportFilters":[{"value":"[email protected]","column":"EMAIL","operator":"equals"}]}}

And I need:

{"reportMetadata":{"reportFilters":[{"value”:”[email protected], [email protected], [email protected], [email protected]","column":"EMAIL","operator":"equals"}]}}

Thanks!

2 Answers 2

3

Have you already tried to use the split command ? By using this command you can turn a string into an array.

You can take a look at this link for mode details:

String.prototype.split()

Sign up to request clarification or add additional context in comments.

3 Comments

Closer! if I run var array = emailFilters.toString().split(","); I get {"reportMetadata":{"reportFilters":[{"value”:[”[email protected], [email protected], [email protected], [email protected]”],”column":"EMAIL","operator":"equals"}]}}. How do I remove '[ ]' after value?
var array = emailFilters.toString().split(",").join(); worked
I do recommend take a look at the website cited below. Actually you're dealing with json object and I think will be pretty interesting dig a little bit deeper about this subject. json.org/js.html
1

I used Cordeiro split with an array of emails from a sheet and it worked well.

I took a column of emails as an array and then use the split:

//Array
var EMAILS = emss.getRange(["C3:"+"C"+em_wlr]).getDisplayValues();
//Split
var emails_cs = EMAILS.toString().split(",").join();

Then I can use in the MailApp:

MailApp.sendEmail(emails_cs, templateSubject, templateText);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.