简体   繁体   English

Google表格脚本自定义排序

[英]Google Sheets Script Custom Sort

my range has following values "Postpone","Yes","No". 我的范围具有以下值:“推迟”,“是”,“否”。 The values need to be sorted in order 值需要按顺序排序

  1. "Yes" “是”
  2. "No" “没有”
  3. "Postpone". “推迟”。

There is no room for additional columns with 1,2,3 values and worksheet function are also not an option. 没有其他空间可以容纳具有1,2,3值的其他列,并且工作表功能也不是一种选择。 Is it possible to do this just with Google Aps Script? 是否可以仅使用Google Aps脚本来做到这一点?

Extending the issue: There is two additional columns that contain text values and need to be sorted ascending. 扩展问题:还有两个附加列包含文本值,需要按升序排序。 Currently I am doing this way: 目前,我正在这样做:

rng.sort([{column: 1, CUSTOM_SORT_NEEDED}, {column: 2, ascending: true}, {column: 3, ascending: true}, ]);

So, here is what works for me, I used a dictionary. 所以,这对我有用,我用了字典。 Not sure if best, or nicest but works. 不知道最好还是最好,但是可以用。

function compare(a,b) {
  var crits = {"Yes":1,"No":2,"Postpone":3};

  var col =1;
  var result = 0;

  if (crits[a[col]] > crits[b[col]]) {result = 1}
  else if (crits[a[col]] == crits[b[col]]) {result = 0}
  else {result = -1}

  return result;
}

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

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