繁体   English   中英

如何在 Excel Power Query 中设置分页?

[英]How can I setup Pagination in Excel Power Query?

我正在使用 JSON 从 web 将财务数据导入 excel,但由于源使用分页(每页给出 50 个结果,我需要按顺序导入所有结果以实现分页

数据源为 JSON:

    https://localbitcoins.com//sell-bitcoins-online/VES/.json?page=1 
    or https://localbitcoins.com//sell-bitcoins-online/VES/.json?page=2

?page=1, ?page=2, ?page=3

我使用以下代码实现分页,但收到错误:

= (page as number) as table =>
let
    Source = Json.Document(Web.Contents("https://localbitcoins.com//sell-bitcoins-online/VES/.json?page="  & Number.ToText(page) )),
    Data1 = Source{1}[Data],
    RemoveBottom = Table.RemoveLastN(Data1,3)
in
    RemoveBottom

当我调用一个参数(第 1 页为 1)来测试它时,我收到以下错误,我似乎无法找出原因?

An error occurred in the ‘GetData’ query. Expression.
Error: We cannot convert a value of type Record to type List.
Details:
    Value=Record
    Type=Type

作为记录,我尝试使用 ListGenerate 包括页面处理:

= List.Generate( ()=>
[Result= try GetData(1) otherwise null, page = 1],
        each [Result] <> null,
        each [Result = try GetData([page]+1) otherwise null, Page = [Page]+1],
        each [Result])

在 MS Excel 中使用 Power Query 实现分页的默认方法是什么?

我知道你在将近一个月前问过这个问题,并且可能已经找到了答案,但无论如何都会回复,以防它帮助其他人。

这行Data1 = Source{1}[Data]对我来说没有意义,因为我认为Source将是一条记录,您不能对记录使用{1}位置查找语法。

下面的代码为我返回 7 页。 您可能想检查它是否获得了您需要/期望的所有页面。

let
    getPageOfData = (pageNumber as number) =>
        let
            options = [
                Query = [page = Number.ToText(pageNumber)]
            ],
            url = "https://localbitcoins.com/sell-bitcoins-online/VES/.json",
            response = Web.Contents(url, options),
            deserialised = Json.Document(response)
        in deserialised,
    responses = List.Generate(
        () => [page = 1, response = getPageOfData(page), lastPage = null],
        each [lastPage] = null or [page] <= [lastPage],
        each [
            page = [page] + 1,
            response = getPageOfData(page),
            lastPage = if [lastPage] = null then if Record.HasFields(response[pagination], "next") then null else page else [lastPage]
        ],
        each [response]
    )
in
    responses

List.Generate中,我的selector只选择[response]字段以保持简单。 您可以在selector本身(例如each [response][data][ad_list] )中更深入地研究数据,或者创建一个新的步骤/表达式并使用List.Transform来执行此操作。

经过一定程度的钻取和转换后,您可能会看到一些数据,例如:

输出

但这取决于您需要数据的外观(以及您感兴趣的列)。


顺便说一句,我在上面的查询中使用了getPageOfData ,但是这个特殊的 API 在其响应中包含下一页的 URL。 因此,第 2 页及之后的页面可能只是在响应中请求了 URL (而不是调用getPageOfData )。

暂无
暂无

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

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