Google sheets 筛选库以显示类别和所有子类别的所有项目

Google sheets 筛选库以显示类别和所有子类别的所有项目,google-sheets,powerapps,Google Sheets,Powerapps,我正在尝试使用PowerApps创建一个库存应用程序,目前每个项目都属于一个主类别,至少有一个子类别。这方面的一个小例子如下: Concessions | Food | Chips | Brand 1 | Brand 2 | Brand 3 | Candy | Chocolate | Brand 1 | Brand 2 |

我正在尝试使用PowerApps创建一个库存应用程序,目前每个项目都属于一个主类别,至少有一个子类别。这方面的一个小例子如下:

Concessions
|    Food
|        Chips
|            Brand 1
|            Brand 2
|            Brand 3
|        Candy
|           Chocolate
|               Brand 1
|               Brand 2
|           Hard
|           Gum
|        Grill
|    Drinks
Travel Goods
|    TravelGoodsSubCat
CatID    CatName            ParentCat
1        Concessions        0
2        Travel Goods       0
3        Food               1
4        Chips              3
5        Candy              3
6        TravelGoodsSubCat  2
.... And So on....
CatID  UltimateParentCat
1      1
2      2
3      1
4      1
5      1
6      2
它保存在一个谷歌表格中(以及同一本书中另一张表格中的库存数据),如下所示:

Concessions
|    Food
|        Chips
|            Brand 1
|            Brand 2
|            Brand 3
|        Candy
|           Chocolate
|               Brand 1
|               Brand 2
|           Hard
|           Gum
|        Grill
|    Drinks
Travel Goods
|    TravelGoodsSubCat
CatID    CatName            ParentCat
1        Concessions        0
2        Travel Goods       0
3        Food               1
4        Chips              3
5        Candy              3
6        TravelGoodsSubCat  2
.... And So on....
CatID  UltimateParentCat
1      1
2      2
3      1
4      1
5      1
6      2
因此,我设想的是两个图库,一个水平横穿屏幕顶部,只显示类别0的子类别。第二个是垂直图库,显示当前类别下的每个项目。因此,如果用户在水平图库中选择特许权,垂直图库应填充分配给类别1、3、4、5等的所有项目


在我看来,如何实现对所有这些可能的子类别的递归搜索?

这是可以实现的,但为了使性能不会显著降低,您需要首先在集合中缓存类别表。如果使用Google Sheets作为数据源,情况尤其如此

然后,您可以创建一个包含两列的集合:CatID和UltimateParentCat,如下所示:

Concessions
|    Food
|        Chips
|            Brand 1
|            Brand 2
|            Brand 3
|        Candy
|           Chocolate
|               Brand 1
|               Brand 2
|           Hard
|           Gum
|        Grill
|    Drinks
Travel Goods
|    TravelGoodsSubCat
CatID    CatName            ParentCat
1        Concessions        0
2        Travel Goods       0
3        Food               1
4        Chips              3
5        Candy              3
6        TravelGoodsSubCat  2
.... And So on....
CatID  UltimateParentCat
1      1
2      2
3      1
4      1
5      1
6      2
这是您将使用递归的地方,但要对每个步骤进行编码,因此您需要在预期的最大深度处停止

因此,您需要将应用程序的OnStart属性或某个按钮的OnSelect属性设置为类似的值,以便只填充一次集合

ClearCollect(CachedCategories, GoogleSheetsTable);

ClearCollect(Step_1,
    AddColumns(
        ShowColumns(
            Filter(CachedCategories, ParentCat=0),
            "CatID"
            ),
        "UltimateParentCat", CatID
        )
    );

Clear(Step_2);

ForAll(Step_1,
    Collect(Step_2,
        AddColumns(
            ShowColumns(
                Filter(CachedCategories, ParentCat=Step_1[@CatID]),
                "CatID"
                ),
        "UltimateParentCat", Step_1[@UltimateParentCat])
        ));

Clear(Step_3);

ForAll(Step_2,
    Collect(Step_3,
        AddColumns(
            ShowColumns(
                Filter(CachedCategories, ParentCat=Step_2[@CatID]),
                "CatID"
                ),
        "UltimateParentCat", Step_2[@UltimateParentCat])
        ));

ClearCollect(CatsWithUltimate, Step_1, Step_2, Step_3)
完成此操作后,水平库的Items属性应为

Filter(CachedCategories, ParentCat=0)
Filter(Products,
    ProductCategory in Filter(CatsWithUltimate,
        UltimateParentCat=GalleryHorizontal.Selected.CatID).CatID)
然后,垂直图库的Items属性应为

Filter(CachedCategories, ParentCat=0)
Filter(Products,
    ProductCategory in Filter(CatsWithUltimate,
        UltimateParentCat=GalleryHorizontal.Selected.CatID).CatID)
请让我知道上面的任何拼写错误,以便我可以修复


作为补充说明,我建议迁移到另一个数据源,如Azure SQL数据库,只生成CatsWithUltimate作为视图。

唯一的说明(这可能是我可以在google sheets端更改的内容)是,来自google sheets的所有值似乎都是字符串,而不是数字。因此,我不得不将步骤1中的Filter命令更新为
Filter(CachedCategories,Value(ParentCat)=0),
此外,是否有一致的方法来触发OnStart属性?最后,我在主屏幕上按了一个按钮来缓存数据。它实际上似乎在数据可用之前触发,因此集合是空的。。。但有时它会起作用,结果好坏参半。关于OnStart的不可靠性,希望随着PowerApps的最终成熟,它会有所改进。