【发布时间】:2021-01-04 18:16:57
【问题描述】:
我正在尝试在 Google Apps 脚本中的数组中排序三列。我知道有很多解释,但我不明白。我希望有人可以解决下面的代码截图。
所需功能:使用“列”8、9、10(文本和空值)按顺序对数组进行排序。
问题:空值似乎没有正确排序。
代码sn-p:
shdv.sort(function(a,b){
var so = -1
a[10]>b[10] ? so=1 : null; // section
a[10] == b[10] && (a[9]>b[9] || a[9] == '') ? so=1 : null;
a[10] == b[10] && a[9]==b[9] && a[8]>a[9] ? so=1 : null;
null;
})
示例结果
[Not Tracking, Site, 2.85208117E8]
[Not Tracking, , 2.83812926E8]
[Not Tracking, , 2.83991529E8]
[Not Tracking, Site, 2.83812602E8]
想要的结果
[Not Tracking, Site, 2.85208117E8]
[Not Tracking, Site, 2.83812602E8]
[Not Tracking, , 2.83812926E8]
[Not Tracking, , 2.83991529E8]
【问题讨论】:
-
提供minimal reproducible example。您的排序功能有拼写错误并且不返回任何内容。还提供
shdv的示例值 -
你不能用
range.sort([{column:8,ascending:true},{column:9,ascending:true},{column:10,ascending:true}])Example here -
代码中的错字已修复...
-
@Cooper - 谢谢,但这仅适用于数组/没有电子表格调用来解决此问题。
标签: javascript arrays sorting google-apps-script