简体   繁体   中英

Pick highest number in a JavaScript Array I got from a Google Sheet

I am making a simple score board for a game I often play with friends using Google Sheets. I am summing up all the points at the top under each player's name and I want to write a custom function to compare these numbers against each other.

After a little bit of googling I stumbled upon a very simple method of picking the highest number out of an array but when I try to use it I only get #NUM! as my output. When I mouse over it it says 'The outcome was not a number".

My code:

function Compare_Scores(input) {  
  return Math.max.apply(Math,input );
}

And a screenshot off my Sheet:

在此处输入图片说明

If you can help me out I would appreciate it a lot.

Cheers

为什么不使用默认的max函数呢?

=MAX(B2:I2)

Add this line to display input on the console:

console.log(input);

There must be some non-numeric data in the array (if it truly is an array).

It seems Math.max.apply(Math,input ) wants to work with columns. To convert you input data to a column array that will work with your function use:

=Compare_Scores(transpose( split( arrayformula( concatenate(B2:I6 & char(9)) ), char(9) ) ))

But =MAX(B2:I2) is the much easier way to do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM