【发布时间】: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