Office script to filter column

jsmith2094

New Member
Joined
Aug 19, 2021
Messages
35
Office Version
  1. 365
Platform
  1. Windows
HI,

not sure if this is the correct place to post.

I need an office script to use with power automate to filter a column called "Ref" and to only include anything that starts with IM or AF.

when I use the recorder it does not do it dynamically

can anyone help ?

here is the code:

function main(workbook: ExcelScript.Workbook) {
let tABLE1 = workbook.getTable("Table1");
// Apply checked items filter on table tABLE1 column Reference Num
tABLE1.getColumnByName("Reference Num").getFilter().applyValuesFilter(["AF-123456", "IM-123456"]);
}
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
I haven't found a way to do this with a filter method, but this might do in a pinch. It loops through the column and hides rows that meet the criteria. This will hide instances of lower case "AF" and "IM" as well. If you want that just delete the two instances of ".toUpperCaSE()" IN LINE 10.

JavaScript:
function main(workbook: ExcelScript.Workbook) {
    let table1 = workbook.getTable("Table1");

    let refColumn = table1.getColumnByName("Reference Num").getRangeBetweenHeaderAndTotal();

    let rowCount = refColumn.getRowCount();

    for (let i = rowCount -1; i >= 0; i--) {
        let cellVal = refColumn.getTexts()[i][0];
        if (cellVal.substring(0, 2).toUpperCase() === "AF" || cellVal.substring(0, 2).toUpperCase() === "IM"){
            refColumn.getCell(i,0).setRowHidden(true);
        }
    }
}
 
Upvote 0
I haven't found a way to do this with a filter method, but this might do in a pinch. It loops through the column and hides rows that meet the criteria. This will hide instances of lower case "AF" and "IM" as well. If you want that just delete the two instances of ".toUpperCaSE()" IN LINE 10.

JavaScript:
function main(workbook: ExcelScript.Workbook) {
    let table1 = workbook.getTable("Table1");

    let refColumn = table1.getColumnByName("Reference Num").getRangeBetweenHeaderAndTotal();

    let rowCount = refColumn.getRowCount();

    for (let i = rowCount -1; i >= 0; i--) {
        let cellVal = refColumn.getTexts()[i][0];
        if (cellVal.substring(0, 2).toUpperCase() === "AF" || cellVal.substring(0, 2).toUpperCase() === "IM"){
            refColumn.getCell(i,0).setRowHidden(true);
        }
    }
}
...if you DON'T want that just delete the two instances of ".toUpperCaSE()" in line 10. Sorry about that!
 
Upvote 0
...if you DON'T want that just delete the two instances of ".toUpperCaSE()" in line 10. Sorry about that!
I'm sorry, you wanted to filter for those, right? This will get you closer.
JavaScript:
function main(workbook: ExcelScript.Workbook) {
    let table1 = workbook.getTable("Table1");

    let refColumn = table1.getColumnByName("Reference Num").getRangeBetweenHeaderAndTotal();

    let rowCount = refColumn.getRowCount();

    for (let i = rowCount -1; i >= 0; i--) {
        let cellVal = refColumn.getTexts()[i][0];
        if (cellVal.substring(0, 2).toUpperCase() !== "AF" && cellVal.substring(0, 2).toUpperCase() !== "IM"){
            refColumn.getCell(i,0).setRowHidden(true);
        }
    }
}
 
Upvote 0

Forum statistics

Threads
1,223,099
Messages
6,170,107
Members
452,302
Latest member
TaMere

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top