1

Sorry, I am new to Office Scripts but have asked ChatGPT, Bing Chat and done lots of Google searches without much luck. I thought it should be something very simple for Microsoft to provide. I must have missed something very crucial or it is a bug in Office Script or Microsoft just tried to make it super hard to use this filtering feature.

Problem: Access the filtered range of a Table from Office Script (see image attachment)

Problem

table1.getRange() => is there a way to captured the filtered range of a Table?

Of course, I can solve it by simply looping through all the records and manually filtering the records. However, it is very inefficient to do it this way as I am dealing with a large number of records and multiple loops.

I was hoping there was a solution to allow me to filter out several columns in a table and retrieve the filtered rows easily from Office Scripts.

2
  • Please share the code instead of image in OP. Commented Oct 3, 2023 at 3:27
  • You will have to anyway loop through the array to retrieve the values of col c1 and c2. No ? Commented Oct 3, 2023 at 4:05

1 Answer 1

2
  • getSpecialCells(...) is utilized to obtain the visible range within a filtered table.

Options for retrieving columns in a filtered table:

  • Iterate through all areas within the visible range (as filtered tables may have discontinuous ranges) and loop through all cells within each area range.
  • Acquire the visible range of the desired column and iterate through its cells.
function main(workbook: ExcelScript.Workbook) {
    let table1 = workbook.getTable("Table3");
    // Enable AutoFilter
    let isFilterON = table1.getAutoFilter().getEnabled();
    if(!isFilterON) {
        table1.getAutoFilter().apply(table1.getRange());
    }
    // Apply checked items filter on table table2 column C1
    let tabFilter = table1.getColumnByName("C1").getFilter();
    let tableRange = table1.getRange();
    // Get the visible range of table w/ header
    let tabVisibleRange = tableRange
        .getSpecialCells(ExcelScript.SpecialCellType.visible);
    for (let i = 0; i < tabVisibleRange.getAreaCount(); i++) {
        let areaRange = tabVisibleRange.getAreas()[i];
        console.log(`Area_${i} range: ${areaRange.getAddress()}`)
    }
    // Get all data in column C2 
    let c2List: string[] = [];
    for (let i = 0; i < tabVisibleRange.getAreaCount(); i++) {
        let areaRange = tabVisibleRange.getAreas()[i];
        let rowCount = areaRange.getRowCount();
        for (let r = 0; r < rowCount; r++) {
            c2List.push(areaRange.getCell(r, 1).getText());
        }
    }
    console.log(c2List.join(","))
    // Get the visible range of column C2 w/o header
    let colVisibleRange = table1.getColumnByName("C2")
        .getRangeBetweenHeaderAndTotal()
        .getSpecialCells(ExcelScript.SpecialCellType.visible);
    console.log(colVisibleRange.getAddress())
}

Output:

Area_0 range: Sheet2!A1:C3
Area_1 range: Sheet2!A5:C5
Area_2 range: Sheet2!A7:C7
C2,Cat,Dog,Pig,Mouse                << data in column C2
Sheet2!B2:B3,Sheet2!B5,Sheet2!B7    << visible range in column C2
Sign up to request clarification or add additional context in comments.

3 Comments

This is I got when I ran the codes: Line 4: Cannot read properties of undefined (reading 'getFilter')
I have updated the code to handle the exception.
It works now. Thank you so much. So the answer is using .getSpecialCells()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.