简体   繁体   English

循环遍历多维数组并拆分 | Google Apps 脚本

[英]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.我有两列 A 和 B,在 Google 表格中,我需要将它们用作搜索名称和文件夹 ID。 However, in the search name column there is more than one name.但是,在搜索名称栏中有多个名称。 For example例如

Search Name搜索名称 Folder ID文件夹 ID Other Columns...其他栏目...
EX3264 EX3264 hdushfush hdushfush - -
EX8347,EX9384, EX273 EX8347、EX9384、EX273 dsjuhdujfh dsjuhdujfh - -
EX374,EX009 EX374,EX009 dndddda 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.分开后,我想用最后的数组说:如果在 email 的主题中找到搜索名称,则将其移动到同一数组中具有 ID 的文件夹中。

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.到目前为止,我所拥有的并且有效的是分别拆分搜索名称并一一命名文件夹(使用 getFolderByName),但想法是在数组内进行交互。

  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?有什么建议吗?

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(',')]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM