简体   繁体   English

从Google表格中选择JavaScript数组中的最高编号

[英]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. 我正在为我经常与朋友使用Google表格进行的游戏制作一个简单的计分板。 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! 经过一番谷歌搜索后,我偶然发现了一个非常简单的方法,该方法是从数组中选取最高的数字,但是当我尝试使用它时,我只会得到#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: 添加此行以在控制台上显示input

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. 看来Math.max.apply(Math,input)想使用列。 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. 但是= MAX(B2:I2)是更简单的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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