根据单元格值自定义排序顺序

问题描述 投票:0回答:3

目前我通过此功能对工作表进行排序

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(a);
var dataRange = sheet.getDataRange();
dataRange.sort([
  {column: role, ascending: true},
  {column: cases, ascending: false}
]);

这首先按角色按字母顺序排序。是否可以自定义这种排序? 例如,理想情况下,排序是按角色级别而不是按字母顺序排序

例如。如果角色是[实习生、初级、高级、领导、经理],工作表可以按该顺序排序吗?

javascript google-sheets google-apps-script
3个回答
0
投票

您可以使用

Map
对象为每个角色分配一个数值,并根据这些值进行排序。希望这能按预期工作。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(a);
var dataRange = sheet.getDataRange();
var data = dataRange.getValues();

var roleOrder = {
  'intern': 1,
  'junior': 2,
  'senior': 3,
  'lead': 4,
  'manager': 5
};

data.sort(function(row1, row2) {
  var role1 = row1[role - 1]; 
  var role2 = row2[role - 1];
  
  if (roleOrder[role1] < roleOrder[role2]) {
    return -1;
  } else if (roleOrder[role1] > roleOrder[role2]) {
    return 1;
  } else {
    return row2[cases - 1] - row1[cases - 1];
  }
});

sheet.getRange(1, 1, data.length, data[0].length).setValues(data);

0
投票

使用 ForEach 对角色级别进行排序

通过从internmanager排序,你的dataRange中的所有数据都会被检索出来,并且setValues将返回到指定的范围。

样本表:

实习生 a
初级 b
经理 c
领先 d
前辈 e
实习生 f
初级 g
经理 h
领先
前辈 j

输出表:

实习生 a
实习生 f
初级 b
初级 g
前辈 e
前辈 j
领先 d
领先
经理 c
经理 h

脚本:

function myFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("a");
  var dataRange = sheet.getDataRange();
  var data = dataRange.getValues();
  var output = []
  var hier = ['intern', 'junior', 'senior', 'lead', 'manager'];
  hier.forEach(x => {
    var temp = [];
    data.forEach(y => (y[0] == x) ? temp.push(y) : null);
    temp.forEach(z => output.push(z))
  });
  dataRange.setValues(output);
}

参考:


0
投票

这是我的示例数据。 我的表格没有显示它,但 a 列是下拉选择

A B C D E
1 COL1 COL2 COL3 COL4 COL5
2 实习生 B2 C2 D2 E2
3 初级 B3 C3 D3 E3
4 前辈 B4 C4 D4 E4
5 领先 B5 C5 D5 E5
6 经理 B6 C6 D6 E6
7 实习生 B7 C7 D7 E7
8 初级 B8 C8 D8 E8
9 前辈 B9 C9 D9 E9
10 领先 B10 C10 D10 E10
11 经理 B11 C11 D11 E11
12 实习生 B12 C12 D12 E12
13 初级 B13 C13 D13 E13
14 前辈 B14 C14 D14 E14
15 领先 B15 C15 D15 E15
16 经理 B16 C16 D16 E16
17 实习生 B17 C17 D17 E17
18 初级 B18 C18 D18 E18
19 前辈 B19 C19 D19 E19
20 领先 B20 C20 D20 E20
21 经理 B21 C21 D21 E21
22 实习生 B22 C22 D22 E22
23 初级 B23 C23 D23 E23
24 前辈 B24 C24 D24 E24
25 领先 B25 C25 D25 E25
26 经理 B26 C26 D26 E26

我的脚本:

function myFunction() {
  var ss = SpreadsheetApp.getActive();
  var sh = ss.getSheetByName("Sheet0");
  var rg = sh.getRange(2,1,sh.getLastRow() - 1, sh.getLastColumn());
  var vs = rg.getValues();
  var list = ['intern', 'junior', 'senior', 'lead', 'manager'];
  vs.sort((a,b) =>{
    return list.indexOf(a[0]) - list.indexOf(b[0]);//switch a and b to sort descending
  } )
  Logger.log(JSON.stringify(vs).replace(/],/g,'],\n'))
  //rg.setValues(vs);
}

这是记录器输出:

[["intern","B2","C2","D2","E2"],
["intern","B7","C7","D7","E7"],
["intern","B12","C12","D12","E12"],
["intern","B17","C17","D17","E17"],
["intern","B22","C22","D22","E22"],
["junior","B3","C3","D3","E3"],
["junior","B8","C8","D8","E8"],
["junior","B13","C13","D13","E13"],
["junior","B18","C18","D18","E18"],
["junior","B23","C23","D23","E23"],
["senior","B4","C4","D4","E4"],
["senior","B9","C9","D9","E9"],
["senior","B14","C14","D14","E14"],
["senior","B19","C19","D19","E19"],
["senior","B24","C24","D24","E24"],
["lead","B5","C5","D5","E5"],
["lead","B10","C10","D10","E10"],
["lead","B15","C15","D15","E15"],
["lead","B20","C20","D20","E20"],
["lead","B25","C25","D25","E25"],
["manager","B6","C6","D6","E6"],
["manager","B11","C11","D11","E11"],
["manager","B16","C16","D16","E16"],
["manager","B21","C21","D21","E21"],
["manager","B26","C26","D26","E26"]]

只需学习如何使用 Javascript Array.sort() 和比较函数即可。使用您最喜欢的 javascript 参考。

© www.soinside.com 2019 - 2024. All rights reserved.