Copilot for Microsoft 365 Tech Accelerator
Feb 28 2024 07:00 AM - Feb 29 2024 10:30 AM (PST)
Microsoft Tech Community

Excel Online Script to loop through rows

Copper Contributor

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

2 Replies
What have you tried yourself so far?

@MD_Wahib 

As variant, if for such table

image.png

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

image.png