Check cell by cell using office script in an excel report

For some reports, I need to check cell by cell then if one cell complies with the requirements, some actions need to be performed. There are 2 codes, one doing from top to down, mostly used for everything and one from down to top, more suitable to insert rows for instance.

 

When I use the script ?

When I have to verify each cell to comply with some values.

 

How to create the script ?

Read How to create, edit and select an Office Script in an excel report

 

How to create the button to associate it with the script ?

Read How to create a button and associated it to an Office Script in an excel report

 

How is/are the script(s) ?

Copy the code below and paste it into your script. You will see my comments in green if exist so follow the help to adapt to your need.

From top to down:


function main(workbook: ExcelScript.Workbook) {
    //change Sheet1 by yours
    //for active sheet, change getWorksheet('Sheet1') by workbook.getActiveWorksheet()
    let sheet = workbook.getWorksheet('Sheet1');
    //change A2:A10 by yours
    let range = sheet.getRange("A2:A10").getValues();
    for (let i = 1; i < range.length; i++) {
        //your code
    }
}              
              

From down to top:


function main(workbook: ExcelScript.Workbook) {
    //change Sheet1 by yours
    //for active sheet, change getWorksheet('Sheet1') by workbook.getActiveWorksheet()
    let sheet = workbook.getWorksheet('Sheet1');
    //change A2:A10 by yours
    let range = sheet.getRange("A2:A10").getValues();
    for (let i = range.length - 1; i >= 0; i--) {
        //your code
    }
}              
              

Interesting Topics