如何使用智能表API递归检索文件夹

时间:2017-09-27 11:54:53

标签: python json smartsheet-api

我正在为python中的智能表工作备份工具。我想检索我域中的所有用户文件。

我不知道如何获取数据,但我不知道如何保存文件夹树,如:

Folder A:  
    - File A  
    - Folder AB:
        *File AZ
        *File AE
        *Folder AC:
            File E

以上例子只是主观的。

使用Smartsheet检索工作区时使用:

ss.Home.list_all_contents()

这里的ss是smartsheet.Smartsheet(令牌)

你得到了回报:

{
"folders": [
    {
        "id": 5709073300645764,
        "name": "folder 1",
        "permalink": "https://app.smartsheet.com/b/home?lx=Dsje3YKtpyZScrCX6Z1"
    }
],
"sheets": [
    {
        "id": 4583173393803140,
        "name": "sheet 1",
        "accessLevel": "OWNER",
        "permalink": "https://app.smartsheet.com/b/home?lx=Dsje3YKtpyZScrCX6Za", 
        "createdAt": "2015-06-05T20:05:29Z",
        "modifiedAt": "2015-06-05T20:05:43Z"
    },
    {
        "id": 2331373580117892,
        "name": "Copy of sheet 1",
        "accessLevel": "OWNER",
        "permalink": "https://app.smartsheet.com/b/home?lx=Dsje3YKtpyZScrCX6Zb",
        "createdAt": "2015-06-05T20:05:29Z",
        "modifiedAt": "2015-06-05T20:05:43Z",
        "source": {
            "id": 4583173393803140,
            "type": "sheet"
        }
    }
],
"reports": [],
"templates": [],
"workspaces": [],
"sights": []
}

但是嵌套文件夹看起来像这样:

{
    "id": 138433004889988,
    "folders": [
        {
            "id": 672830015727492,
            "folders": [
                {
                    "id": 3791044992100228,
                    "folders": [],
                    "reports": [],
                    "favorite": null,
                    "name": "3nested",
                    "templates": [],
                    "permalink": "",
                    "sheets": []
                }
            ],
            "reports": [],
            "favorite": null,
            "name": "2nested",
            "templates": [],
            "permalink": "",
            "sheets": [
                {
                    "accessLevel": "OWNER",
                    "userSettings": null,
                    "id": 2308634748184452,
                    "fromId": null,
                    "modifiedAt": "2017-08-26T07:30:23+00:00",
                    "attachments": [],
                    "resourceManagementEnabled": null,
                    "projectSettings": null,
                    "effectiveAttachmentOptions": [],
                    "favorite": null,
                    "showParentRowsForFilters": null,
                    "createdAt": "2017-08-07T08:17:20+00:00",
                    "version": null,
                    "rows": [],
                    "columns": [],
                    "readOnly": null,
                    "name": "file 2nested",
                    "permalink": "",
                    "owner": null,
                    "totalRowCount": null,
                    "ganttEnabled": null,
                    "source": null,
                    "dependenciesEnabled": null,
                    "ownerId": null,
                    "discussions": []
                }
            ]
        }
    ],
    "reports": [],
    "favorite": null,
    "name": "fnfd",
    "templates": [],
    "permalink": "",
    "sheets": [
        {
            "accessLevel": "OWNER",
            "userSettings": null,
            "id": 8542174187939716,
            "fromId": null,
            "modifiedAt": "2017-08-26T07:30:24+00:00",
            "attachments": [],
            "resourceManagementEnabled": null,
            "projectSettings": null,
            "effectiveAttachmentOptions": [],
            "favorite": null,
            "showParentRowsForFilters": null,
            "createdAt": "2017-08-03T18:16:29+00:00",
            "version": null,
            "rows": [],
            "columns": [],
            "readOnly": null,
            "name": "msp",
            "permalink": "",
            "owner": null,
            "totalRowCount": null,
            "ganttEnabled": null,
            "source": null,
            "dependenciesEnabled": null,
            "ownerId": null,
            "discussions": []
        }
    ]
}

以上json的树看起来像那样:

folder fnfd:  
    - File msp.xlsx
    - Folder 2nested:
        -File file 2nested.xlsx
        -Folder 3nested

问题

无论您需要检索的文件夹数量是多少,我如何递归检索所有这些文件夹

1 个答案:

答案 0 :(得分:1)

您需要一个标准的递归下降算法。对于每个返回文件夹列表的调用,您需要遍历文件夹ID并调用ss_client.Folders.get_folder(folder_id)以检索包含的工作表和任何子文件夹的列表。