Share via

How to filter pivot table using office scripts?

Vignesh Babu Sundararajan 46 Reputation points
2021-10-26T10:36:38.34+00:00

How to filter a pivot table using office script?

  • Date Filter
  • Name filter
Microsoft 365 and Office | Development | Other

3 answers

Sort by: Most helpful
  1. R LIBIN 1 Reputation point
    2021-12-19T01:21:39.867+00:00

    This is not the most best answer but it worked way.

    Was this answer helpful?

    0 comments No comments

  2. R LIBIN 1 Reputation point
    2021-12-19T01:20:37.02+00:00

    get all the items not required from the pivot table using the cells or hierarchies.

    uncheck all and check the required items.

    (or)

    get all the list of not_required items and remove them.

    function not_required_items(workbook: ExcelScript.Workbook, Company: string[], PName: string) {
    let sheet = workbook.getActiveWorksheet();
    let PT = sheet.getPivotTable(PName);

    //console.log(myString.replace(/(.*?)/g, ""));

    /for (let i = 0; i < ; i++) {
    console.log(PT.getRowHierarchy("Company").getPivotField("Company").getPivotItem(Company(i)).setVisible(false));
    }
    /
    //console.log(PT.getRowHierarchy("Company").getPivotField("Company").getPivotItem("AllianzGI").setVisible(true));
    ;

    sheet.getRange("A1").select;
    var PivotCount = sheet.getUsedRange().getRowCount();

    var company_list = [" "];
    var not_req_company = [""];
    for (var i = 2; i < PivotCount; i++) {
    var items = workbook.getActiveWorksheet().getRange("A" + i).getValue()

    company_list.push(items.toString())
    
    for (var j = 0; j < Company.length; j++) {
      if (items.toString() === Company[j].toString()) {
        company_list.pop();
        //console.log(items);
        //continue;
      }
      else {
    
      }
    }
    //console.log(items)
    

    }
    company_list.shift();

    return company_list;
    }

    Was this answer helpful?

    0 comments No comments

  3. Vignesh Babu Sundararajan 46 Reputation points
    2021-11-01T05:04:27.893+00:00

    I tried but no luck!

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.