Excel
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
1,638 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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);
}