Bruna B
Bruna B

Reputation: 173

Loop through multidimensional array and split | Google Apps Script

I am in need of help when looping through a multidimensional array. I have two columns A and B, in a Google Sheet, I need to use them as search name and folder id. However, in the search name column there is more than one name. For example

Search Name Folder ID Other Columns...
EX3264 hdushfush -
EX8347,EX9384, EX273 dsjuhdujfh -
EX374,EX009 dndddda -

I would like to be able to loop between the two columns and separate the search names, which are separated by commas. Once separated, I would like to use the final array to say: if the search name was found in the subject of an email, move it to the folder with ID in the same array.

So the final array should be:

Data (Array 3) 
[0] Array 2: 
   0: 'hdushfush' 
   1: 'EX3264'
[1] Array 4:
   0: 'dsjuhdujfh' 
   1: 'EX8347'
   2: 'EX9384'
   3: 'EX273' 
... 

What I have and that works so far, is to split the search names separately and name the folder one by one (using getFolderByName), but the idea is to interact within the array.

  var finalarray = [{}];
    for (var j = 1; j <= sheet.getRange("a2:b5").getValues().length; j++) {
      var testcol = sheet.getRange("d" + (j +1)).getValue();
      var tt = testcol.split(",");
      finalarray[j - 1] = tt;
    }
...

          for (var k = 0; k < tt.length; k++) {
            if (filename.indexOf(tt[k]) !== -1) {
              folderName = folder1;
            } else {
              folderName = folder2;
            }

Any suggestion?

Upvotes: 0

Views: 300

Answers (1)

doubleunary
doubleunary

Reputation: 18864

Your expected result format is a bit unusual, but try this:

function weirdArray() {
  const data = SpreadsheetApp.getActive().getRange('A2:B5').getValues();
  return data.map(row => [row[1], ...row[0].split(',')]);
}

Upvotes: 2

Related Questions