按 2 个变量排序

时间:2021-01-27 21:19:54

标签: sorting google-apps-script google-sheets

这是对我之前回答的问题的跟进:How to Not Transfer Blank Column to Master Sheet?

2 个新问题。

  1. 如何按 2 个项目排序?我需要按人员及其状态(即已完成)进行排序。在状态单元格中也是一个日期,我只是想让它找到已完成的部分。
  2. 状态已完成如何跳过?这两个都是针对已经合并的页面。希望这是有道理的。

样本表:https://docs.google.com/spreadsheets/d/1Y48N8W8CdaNlrxXopj5lnHTiNep33g_qA2-kTrPdt3A/edit#gid=130911536

rreplace!(@view(Pop[end, :, 1]), (33=>3, Bernoulli(0.7)), (44=>4, Bernoulli(0.85)))

1 个答案:

答案 0 :(得分:0)

注意: 在我的示例中,我从示例中删除了 Other Data 列以使工作表更干净。我还添加了 Person 列。

试试这个:

表 1:

enter image description here

代码:

function filternsort() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
  var sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet2');
  var sheet3 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet3');
  //get sheet1 data
  var val = sheet.getDataRange().getValues();
  var completed = [];
  var notcompleted = [];
  //start to 1 to exclude header
  for (var i = 1; i < val.length; i++) {
    if(val[i][5].toString().match(/Completed.+/)){
      //add to array completed if it matches the word Completed in the Date Due column
      completed.push(val[i]);
    }else{
      //add to array notcompleted if not match.
      notcompleted.push(val[i]);
    }
  }
  //insert header to the beginning of the array
  completed.unshift(val[0]);
  //set filtered values
  sheet2.getRange(1, 1, completed.length, completed[0].length).setValues(completed);
  //sort by Person
  sheet2.getRange(2, 1, completed.length, completed[0].length).sort(1);

  //insert header to the beginning of the array
  notcompleted.unshift(val[0]);
  //set filtered values
  sheet3.getRange(1, 1, notcompleted.length, notcompleted[0].length).setValues(notcompleted);
  //sort by Person
  sheet3.getRange(2, 1, notcompleted.length, notcompleted[0].length).sort(1);  
}

表 2:

enter image description here

表 3:

enter image description here

参考文献:

相关问题