Forum Discussion

MD_Wahib's avatar
MD_Wahib
Copper Contributor
Nov 28, 2023

Excel Online Script to loop through rows

I need an Excel online script to loop through rows inside the table and delete the rows based on cell value 

2 Replies

  • SergeiBaklan's avatar
    SergeiBaklan
    Diamond Contributor

    MD_Wahib 

    As variant, if for such table

    to remove rows with "a" in any cell, it could be

    function main(workbook: ExcelScript.Workbook) {
    
        const tbl = workbook
            .getTable("Table1")
        const rng = tbl
            .getRangeBetweenHeaderAndTotal()
        const value = "a"
        const values = rng
            .getValues()
    
        let id = -1;
        for (let row of values ) {
            let deleteRow = false;
    
            for (let cell of row) {
                if (cell.toString() == value) {
                    deleteRow = true
                }
            }
            ++id
            if (deleteRow) { tbl.deleteRowsAt(id, 1); --id} 
        }
    }

    Result is

Resources