0

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?

1 Answer 1

2

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(',')]);
}
Sign up to request clarification or add additional context in comments.

1 Comment

@doubleunary It is weird, but if the array is not separate, for some reason indexOf does not work as expected. This way it worked, now I just have to figure out how to make it look at the correct compilation of "if it's indexOf", send it to the Folder ID in the same array. Thank you!

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.