Office-JS API:从 table 中获取过滤后的数据

Office-JS API: Fetching filtered data from table

如果 Office-JS API.

中的过滤器处于活动状态,我正在尝试找出一种仅从 table 中获取过滤值的方法

目前我想获取所有 table 数据的唯一方法是从 table 范围值 属性:

var table = tables.getItemAt(0);
var tableRange = table.getRange();
tableRange.load("values");
ctx.sync().then(function () {
    // This returns all the values from the table, and not only the visible data
    var values = tableRange.values;
});

如果过滤器处于活动状态,关于如何继续从 table 中仅获取可见值的任何想法?

根据以前使用 Office Interop 的经验,我通过遍历 table 范围的不同区域实现了相同的效果,但我无法找到与 Office-JS 中的区域等效的内容。

一种只获取过滤数据的方法是通过 Binding.getDataAsync method, which takes a filterType parameter.

Office.select("bindings#myTableBinding1").getDataAsync({
    coercionType: "table",
    filterType: "onlyVisible"
},function(asyncResult){
    var values = (asyncResult.value.rows);
});

此代码假定您已经创建了到 table 的绑定。如果没有,你可以先运行下面的代码,它使用table名字来调用Bindings.addFromNamedItemAsync:

Office.context.document.bindings.addFromNamedItemAsync("Table1","table",{
    id: "myTableBinding1"
},function(asyncResult){
    // handle errors and call code sample #1
});

请注意,早在 Excel 2013 年就支持上述解决方案,因为它使用共享的 APIs。 Excel-特定的 API 集还不能 return 只有未过滤的数据。

-Michael Saunders,Office 加载项项目经理

作为 Excel JS API 1.3 的一部分,即将推出的下一波功能将包括一个新对象 "RangeView",它允许您仅读取 Range 对象的可见值。 这是 GitHub - https://github.com/OfficeDev/office-js-docs/tree/ExcelJs_1.3_OpenSpec/excel 上开放规范的 link。 请注意,这目前还不可用,但会在不久的将来提供。

你的情况下 table 的用法如下所示:

var table = tables.getItemAt(0);
var visibleView = table.getRange().getVisibleView();
ctx.load(visibleView);
ctx.sync().then(function () {
    var values = visibleView.values;
});