Trying to search for column by column value in Office Scripts

Lai, Ian 20 Reputation points
2024-06-27T16:56:38.69+00:00

Hi everyone,

I am trying to create an office script that will calculate the percentage of Y's and Y+N's in a certain column, and searching for the column name in row 4 rather than hard-coding the index. Below is my code, but I keep getting the issue with percentValues.length, where it's saying the length is 0 so i'm not pulling anything in. I know for a fact that the string i'm searching for exists and its in the range A4:DK4 so I was wondering if anyone sees an issue with my code here?
Thanks!

// Function to calculate the percentage of 'Y' values in a range
function calculatePercentage(sheet: ExcelScript.Worksheet, value: string): number {
    let column = sheet.getRange("A4:DK4").getValues()[0].indexOf(value) + 1;
    let percentRange = sheet.getRange(column + "5:300");
    let percentValues = percentRange.getValues();
    let yCount = 0;
    let nCount = 0;
    for (let row = 0; row < percentValues.length; row++) {
        if (percentValues[row][0] === 'Y') {
            yCount++;
        } else if (percentValues[row][0] === 'N') {
            nCount++;
        }
    }
    return yCount / (yCount + nCount);
}
Excel
Excel
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
1,638 questions
JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
919 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,673 questions
0 comments No comments
{count} votes