从分页搜索中提取最后 n 个结果

Extract the last n results from a Paged Search

我正在尝试从 SuiteScript 2.0 中的分页搜索中获取最后 200 个结果。当我 运行 简单代码时,我得到错误

"name":"INVALID_PAGE_RANGE","message":"Invalid page range: fetch."

我到底做错了什么?

下面的代码是 运行 在 NS 调试器中的(为简洁起见,我删除了一些代码):

function(ui, email, runtime, search, file, config, format, record, log) {

    var mySearch = search.load({
            id: 'customsearch_mht_lrf_export_to_lab'
        });

    // 200 results per page (am I correct here?)
    var results = mySearch.runPaged({pageSize:200});

    var count = results.count; // = 264

    // Obtain the last 200 results. From the documentation; 
    // index is 'The index of the page range that bounds the desired data.'
    // Error occurs on the next line
    var data = results.fetch({index: results.count});

    var x = 0;

});

(我已经在 Slack 小组上回答了这个问题,但我会在这里复制我的回答以防万一有人遇到这个问题并遇到 post)。

你传递给results.fetchindex参数是你想要的"page"数据的索引。在上面的示例中,如果您有 264 个结果并且页面大小为 200,那么将有 2 页结果。结果 1 - 199 将在第一页(索引 = 0),200 - 264 在第二页。

为了获得最后 200 个结果,您将始终需要检索最后 2 页(除非结果计数是 200 的整数倍),然后只查看这些结果中的最后 200 个。