【问题标题】:Excel Office JS filter dataExcel Office JS 过滤数据
【发布时间】:2017-09-01 14:55:44
【问题描述】:

在 Excel 中,用户可以选择一个范围并点击 Ctrl+Shift+L 以显示过滤器。我正在尝试从 Office.js 加载项中获得等效行为。

我最接近的方法是在我想要过滤的范围内添加一个表格,然后在表格中添加一个过滤器。然而,这似乎存在一些重大问题。

首先,以这种方式为超过 30000 行添加一个表非常慢,而且我经常使用比这大得多的表。如果我在该大小的范围内执行 Ctrl+Shift+L ,则它是瞬时的。

此外,当我添加表格时,Office.js 会对范围进行样式化。我不想为我只想添加一个过滤器的范围添加任何新样式。

我当前的代码如下所示:

await Excel.run(async ctx => {
    const table = await getOrCreateDataTable(ctx, "CostData", new ExcelRange(this.stateService.headerRow)); //see below
    const validationColumn: Excel.TableColumn = table.columns.getItemOrNullObject("Validation");
    validationColumn.filter.applyCustomFilter(`*${searchString}*`)
    await ctx.sync();
});

export const getOrCreateDataTable = async(ctx: Excel.RequestContext, tableName: string, headerRow: ExcelRange): Promise < Excel.Table > => {

    let table: Excel.Table = ctx.workbook.tables.getItemOrNullObject(tableName)
    await ctx.sync();
    if (!table.isNullObject)
        console.log(`Table: ${tableName} found`)
    else {
        const sheet = await getSheet(ctx, headerRow.sheet)
        const headerRange = sheet.getRange(headerRow.getRange()).getEntireRow().getUsedRange()
        const usedRange: Excel.Range = sheet.getUsedRange()
        const tableRange = headerRange.getBoundingRect(usedRange.getLastCell())
        table = ctx.workbook.tables.add(tableRange, true)
        table.name = tableName
        await ctx.sync();

    }
    return table;
}

【问题讨论】:

    标签: office-js


    【解决方案1】:

    您现在可以从 Excel Javascript API 1.9 开始向范围添加过滤器: https://docs.microsoft.com/en-us/office/dev/add-ins/reference/requirement-sets/excel-api-1-9-requirement-set

    这是一个应用自定义过滤器的示例,该过滤器将查找带有子字符串“test”的单元格:

    Excel.run((ctx) => {
        var sheet = ctx.workbook.worksheets.getActiveWorksheet();
        var range = sheet.getUsedRange();
        var columnIndex = 3; //zero based index
        var condition = { criterion1: "=*test*", filterOn: Excel.FilterOn.custom }        
    
        sheet.autoFilter.apply(range, columnIndex, condition);
    
        return ctx.sync();
    }).catch(errorHandlerFunction);
    

    删除过滤器:

    sheet.autoFilter.remove();

    在此处查找更多详细信息:

    https://docs.microsoft.com/en-us/office/dev/add-ins/excel/excel-add-ins-worksheets#filter-data

    或在脚本实验室:https://www.microsoft.com/en-us/garage/profiles/script-lab/

    【讨论】:

      【解决方案2】:

      目前ExcelApi 仅支持过滤Table 对象。

      您在这里寻找的是对过滤Range 的支持。我强烈推荐访问UserVoice 和这个建议。

      【解决方案3】:

      将@cs_pupil 答案和 MS Docs 我想出了这个函数。

      注意:在让 MS 版本运行后,我注意到的第一件事是它无法正常使用我主要使用的通配符,所以我切换到 custom 过滤与 values

      https://docs.microsoft.com/en-us/office/dev/add-ins/excel/excel-add-ins-tables#apply-filters-to-a-table

      function Do_FilterTable(TBLObj, ColHeader, criterion1Str) {
          TBLObj.columns.getItem(ColHeader).filter.apply({
              filterOn: Excel.FilterOn.custom,
              criterion1: criterion1Str
          }); 
      }
      

      这里是更多用于过滤范围和使用 2x criterion 的示例代码:

      var ws = context.workbook.worksheets.getActiveWorksheet();
      var range = ws.getUsedRange();
      var columnIndex = 27; //zero based index
      var condition = {
          criterion1: ">42",
          operator: "Or",
          criterion2: "=*>*",
          filterOn: Excel.FilterOn.custom
      } 
      ws.autoFilter.apply(range, columnIndex, condition);
      

      还有……我做的一个设置对象来帮助我记住事情:

      const Filter_Settings_Obj = {
          "filterOn": {
              "Excel": {
                  "FilterOn": {
                      "BottomItems": "BottomItems",
                      "BottomPercent": "BottomPercent",
                      "CellColor": "CellColor",
                      "Dynamic": "Dynamic",
                      "FontColor": "FontColor",
                      "Values": "Values",
                      "TopItems": "TopItems",
                      "TopPercent": "TopPercent",
                      "Icon": "Icon",
                      "Custom": "Custom",
                  },
                  "DynamicFilterCriteria": {
                      "Unknown": "Unknown",
                      "AboveAverage": "AboveAverage",
                      "AllDatesInPeriodApril": "AllDatesInPeriodApril",
                      "AllDatesInPeriodAugust": "AllDatesInPeriodAugust",
                      "AllDatesInPeriodDecember": "AllDatesInPeriodDecember",
                      "AllDatesInPeriodFebruray": "AllDatesInPeriodFebruray",
                      "AllDatesInPeriodJanuary": "AllDatesInPeriodJanuary",
                      "AllDatesInPeriodJuly": "AllDatesInPeriodJuly",
                      "AllDatesInPeriodJune": "AllDatesInPeriodJune",
                      "AllDatesInPeriodMarch": "AllDatesInPeriodMarch",
                      "AllDatesInPeriodMay": "AllDatesInPeriodMay",
                      "AllDatesInPeriodNovember": "AllDatesInPeriodNovember",
                      "AllDatesInPeriodOctober": "AllDatesInPeriodOctober",
                      "AllDatesInPeriodQuarter1": "AllDatesInPeriodQuarter1",
                      "AllDatesInPeriodQuarter2": "AllDatesInPeriodQuarter2",
                      "AllDatesInPeriodQuarter3": "AllDatesInPeriodQuarter3",
                      "AllDatesInPeriodQuarter4": "AllDatesInPeriodQuarter4",
                      "AllDatesInPeriodSeptember": "AllDatesInPeriodSeptember",
                      "BelowAverage": "BelowAverage",
                      "LastMonth": "LastMonth",
                      "LastQuarter": "LastQuarter",
                      "LastWeek": "LastWeek",
                      "LastYear": "LastYear",
                      "NextMonth": "NextMonth",
                      "NextQuarter": "NextQuarter",
                      "NextWeek": "NextWeek",
                      "NextYear": "NextYear",
                      "ThisMonth": "ThisMonth",
                      "ThisQuarter": "ThisQuarter",
                      "ThisWeek": "ThisWeek",
                      "ThisYear": "ThisYear",
                      "Today": "Today",
                      "Tomorrow": "Tomorrow",
                      "YearToDate": "YearToDate",
                      "Yesterday": "Yesterday",
                  },
              },
          },
          "operator": {
              "And": "And",
              "Or": "Or",
          },
      }
      

      【讨论】:

        猜你喜欢
        • 2016-11-09
        • 2011-11-23
        • 1970-01-01
        • 2020-04-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多