【问题标题】:Create tree from 2D array and update Google Sheet从二维数组创建树并更新 Google Sheet
【发布时间】:2021-01-15 02:56:06
【问题描述】:

我正在尝试为我们的 OrgUnit 创建一个树形视图(并将其转储到 Google 表格中)。 我有以下脚本可以提取和排序数据:

function listAllOUs() {
  const ouArray = []
  const page = AdminDirectory.Orgunits.list('my_customer', {
      orgUnitPath: '/',
      type: 'all'
    });
    const orgUnits = page.organizationUnits;
    if (orgUnits) {
      for (let i = 0; i < orgUnits.length; i++) {
        const orgUnit = orgUnits[i];
        ouArray.push(orgUnit.orgUnitPath)
      }
    } else {
      Logger.log('Could not find any OUs');
    }
    ouArray.sort()
}

上面的代码产生了一个名为ouArray的数组,看起来像这样:

[/Parent_01, /Parent_01/Child_01, /Parent_01/Child_01/Grandchild_01, /Parent_01/Child_02, /Parent_01/Child_03, /Parent_01/Child_04, /Parent_02, /Parent_02/Child_01, /Parent_02/Child_02, /Parent_02/Child_02/Grandchild_01, /Parent_02/Child_05, /Parent_02/Child_06, /Parent_02/Child_07, /Parent_02/Child_07/Grandchild_01, /Parent_02/Child_08, /Parent_02/Child_09, /Parent_02/Child_09/Grandchild_01, /Parent_02/Child_3, /Parent_02/Child_4, /Parent_03, /Parent_03/Child_01, /Parent_03/Child_01/Grandchild_01, /Parent_03/Child_02, /Parent_03/Child_02/Grandchild_01, /Parent_03/Child_02/Grandchild_02, /Parent_03/Child_03, /Parent_03/Child_03/Grandchild_01, /Parent_03/Child_03/Grandchild_02, /Parent_03/Child_04, /Parent_03/Child_05, /Parent_03/Child_05/Grandchild_01, /Parent_03/Child_05/Grandchild_02, /Parent_10, /Parent_11]

现在我要做的是获取该数据,对其进行格式化并将其放入 Google 表格中,使其类似于以下内容:

+====+============+=====================+===================================+
|    |     A      |          B          |                 C                 |
+====+============+=====================+===================================+
|  1 | /Parent_01 |                     |                                   |
+----+------------+---------------------+-----------------------------------+
|  2 |            | /Parent_01/Child_01 |                                   |
+----+------------+---------------------+-----------------------------------+
|  3 |            |                     | /Parent_01/Child_01/Grandchild_01 |
+----+------------+---------------------+-----------------------------------+
|  4 |            | /Parent_01/Child_02 |                                   |
+----+------------+---------------------+-----------------------------------+
|  5 |            | /Parent_01/Child_03 |                                   |
+----+------------+---------------------+-----------------------------------+
|  6 |            | /Parent_01/Child_04 |                                   |
+----+------------+---------------------+-----------------------------------+
|  7 | /Parent_02 |                     |                                   |
+----+------------+---------------------+-----------------------------------+
|  8 |            | /Parent_02/Child_01 |                                   |
+----+------------+---------------------+-----------------------------------+
|  9 |            | /Parent_02/Child_02 |                                   |
+----+------------+---------------------+-----------------------------------+
| 10 |            |                     | /Parent_02/Child_02/Grandchild_01 |
+----+------------+---------------------+-----------------------------------+
| 11 |            | /Parent_02/Child_05 |                                   |
+----+------------+---------------------+-----------------------------------+
| 12 |            | /Parent_02/Child_06 |                                   |
+----+------------+---------------------+-----------------------------------+
| 13 |            | /Parent_02/Child_07 |                                   |
+----+------------+---------------------+-----------------------------------+
| 14 |            |                     | /Parent_02/Child_07/Grandchild_01 |
+----+------------+---------------------+-----------------------------------+
| 15 |            | /Parent_02/Child_08 |                                   |
+----+------------+---------------------+-----------------------------------+
| 16 |            | /Parent_02/Child_09 |                                   |
+----+------------+---------------------+-----------------------------------+
| 17 |            |                     | /Parent_02/Child_09/Grandchild_01 |
+----+------------+---------------------+-----------------------------------+
| 18 |            | /Parent_02/Child_3  |                                   |
+----+------------+---------------------+-----------------------------------+
| 19 |            | /Parent_02/Child_4  |                                   |
+----+------------+---------------------+-----------------------------------+
| 20 | /Parent_03 |                     |                                   |
+----+------------+---------------------+-----------------------------------+
| 21 |            | /Parent_03/Child_01 |                                   |
+----+------------+---------------------+-----------------------------------+
| 22 |            |                     | /Parent_03/Child_01/Grandchild_01 |
+----+------------+---------------------+-----------------------------------+
| 23 |            | /Parent_03/Child_02 |                                   |
+----+------------+---------------------+-----------------------------------+
| 24 |            |                     | /Parent_03/Child_02/Grandchild_01 |
+----+------------+---------------------+-----------------------------------+
| 25 |            |                     | /Parent_03/Child_02/Grandchild_02 |
+----+------------+---------------------+-----------------------------------+
| 26 |            | /Parent_03/Child_03 |                                   |
+----+------------+---------------------+-----------------------------------+
| 27 |            |                     | /Parent_03/Child_03/Grandchild_01 |
+----+------------+---------------------+-----------------------------------+
| 28 |            |                     | /Parent_03/Child_03/Grandchild_02 |
+----+------------+---------------------+-----------------------------------+
| 29 |            | /Parent_03/Child_04 |                                   |
+----+------------+---------------------+-----------------------------------+
| 30 |            | /Parent_03/Child_05 |                                   |
+----+------------+---------------------+-----------------------------------+
| 31 |            |                     | /Parent_03/Child_05/Grandchild_01 |
+----+------------+---------------------+-----------------------------------+
| 32 |            |                     | /Parent_03/Child_05/Grandchild_02 |
+----+------------+---------------------+-----------------------------------+
| 33 | /Parent_10 |                     |                                   |
+----+------------+---------------------+-----------------------------------+
| 34 | /Parent_11 |                     |                                   |
+----+------------+---------------------+-----------------------------------+

如果有人对我如何实现这一点有任何想法,那就太棒了。逻辑上(至少在我看来),它需要某种字符串/部分字符串匹配?但我一辈子都想不通!

【问题讨论】:

    标签: javascript google-apps-script google-sheets google-api tree


    【解决方案1】:

    解释:

    以下脚本的逻辑很简单:

    • 遍历ouArrayforEach元素找出正斜杠的数量/

    • 使用嵌套的ternary operator,对于每种可能的情况push,使用对应的数组到最终的data 数组。

    更详细*

    • 1 x /:追加[t,"",""]
    • 2 x /:追加["",t,""]
    • 3 x /:追加["","",t]

    其中touArray 中的每个元素。

    *(1x/ 表示一个正斜杠等)

    完整解决方案:

    function listAllOUs() {
      // get spreadsheet details
      const ss = SpreadsheetApp.getActive();
      const sh = ss.getSheetByName('Sheet1'); // choose the name of your sheet
    
      const ouArray = []
      const page = AdminDirectory.Orgunits.list('my_customer', {
          orgUnitPath: '/',
          type: 'all'
        });
        const orgUnits = page.organizationUnits;
        if (orgUnits) {
          for (let i = 0; i < orgUnits.length; i++) {
            const orgUnit = orgUnits[i];
            ouArray.push(orgUnit.orgUnitPath)
          }
        } else {
          Logger.log('Could not find any OUs');
        }
        ouArray.sort()
      
      data = [];
      ouArray.forEach(t=>{       
           let nslashes = [...t].filter(l => l === '/').length;
           data.push(
             nslashes==1?[t,"",""]:
             nslashes==2?["",t,""]:["","",t]
           )           
       });
     // set the values back to the sheet
     sh.getRange(1,1,data.length,data[0].length).setValues(data);
    }
    

    可重现的例子:

    function myFunction() {
      const ss = SpreadsheetApp.getActive();
      const sh = ss.getSheetByName('Sheet1'); // choose the name of your sheet
      const ouArray = ["/Parent_01", "/Parent_01/Child_01", "/Parent_01/Child_01/Grandchild_01", 
                       "/Parent_01/Child_02", "/Parent_01/Child_03", "/Parent_01/Child_04",
                       "/Parent_02", "/Parent_02/Child_01", "/Parent_02/Child_02", "/Parent_02/Child_02/Grandchild_01",
                       "/Parent_02/Child_05", "/Parent_02/Child_06", "/Parent_02/Child_07", 
                       "/Parent_02/Child_07/Grandchild_01", "/Parent_02/Child_08", "/Parent_02/Child_09"]
      data = [];
      ouArray.forEach(t=>{       
           let nslashes = [...t].filter(l => l === '/').length;
           data.push(
             nslashes==1?[t,"",""]:
             nslashes==2?["",t,""]:["","",t]
           )           
       });
     sh.getRange(1,1,data.length,data[0].length).setValues(data);
    }
    

    可重现示例的输出:

    【讨论】:

    • 不错。我认为它也可以修改为不对数组进行硬编码。 [编辑] 类似ar=new Array(maxslashes).fill("");ar[nslashes-1]=t
    猜你喜欢
    • 1970-01-01
    • 2019-12-04
    • 2021-10-28
    • 2023-01-22
    • 2016-12-27
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    相关资源
    最近更新 更多