Home > database >  How to change list values/format?
How to change list values/format?

Time:12-30

I'm trying to check a table, that contains "editors" and various "markets" attached to it, and to get a unique list of editors with their number of entries per market.

With this script, I manage to get two list : "editor" and "market":

function JonSnow() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = sheet.getRange("XX!J1:J114");
  var data = range.getDisplayValues();
  Logger.log(JSON.stringify(data));
  const editor = [];
  const market = [];



  data.forEach( (val, index) =>{
    if(val !="None - regular editor" && val !="N/A" && val !="Cov?" && val !="" && val !="None - no cross-market coverage for this task"){
      if(index >= 2 && index <= 12){
        markett = "Dach";
      }else if (index >= 15 && index <= 30) {
        markett = "Dutch";
      }else if (index >= 32 && index <= 48) {
        markett = "French";
      }else if (index >= 52 && index <= 64) {
        markett = "Italian";
      }else if (index >= 67 && index <= 74) {
        markett = "Nordics";
      }else if (index >= 77 && index <= 92) {
        markett = "polish";
      }else if (index >= 95 && index <= 114) {
        markett = "Uk";
      };
      editor.push(val)
      market.push(markett)
  } else {
    return
  };
  });
  Logger.log(editor);
  Logger.log(market);
  const edunique = [...new Set(editor)];

My next steps would have been to get a list of unique editors and with loops, counting the number of market entries. Problem is, the [...new Set(editor)], doesn't work, and I believe it's because my "editor" list returns [[Name1],[Name2],[Name1]], and should return ["Name1","Name2","Name1"] to work.

Here are my questions :

  • How do you call to a format like that ? [[],[],[]]
  • Where did I went wrong and got this ?
  • How can I fix this, or prevent it ?

Thanks a lot!! (Sorry if my title is not helpful, as I have no idea what to call the issue I have)

CodePudding user response:

The Range..getDisplayValues() function gets a 2D array. To get a 1D array, use Array.flat(), like this:

  const data = range.getDisplayValues().flat();
  • Related