I need to the the minimum (or maximum, or some other values) of a range of data using Office Script in Excel. My data (a column of a table) contains only numeric values or empty cells.
I can loop over the data, that's not the problem, but I thought there must be an easier way as this is the very nature of a spreadsheet. However, it seems it is not so easy.
Something like WorksheetFunctions of course doesn't exists.
I read the data into a variable, that works
let myValues = table.getColumnByName('myColumn').getRange().getValues();
But what now?
- Using
Math.min(myValues)fails because myValues contains data of that fancystring | number | booleandata type. - Tried to declare
myValueswithas number. The editor gives a nasty hint that this may fail, but checking with the debugger (haha, just kidding) with console.log shows that the content are numbers. ButMath.min(myValues)still fails, even if I type a 0 in all empty cells. - Thought that maybe the strange array definition causes this and tried to use the
flat()function, but that is not avaialable for the array ("does not exist on type...")
Even Copilot gives up and uses a loop. Is there really no better way?
Set myRange = Worksheets("Sheet1").Range("A1:A5"); answer = Application.WorksheetFunction.Min(myRange); MsgBox answer