【问题标题】:How to sort rows based on background color in google sheets如何根据谷歌表格中的背景颜色对行进行排序
【发布时间】:2020-07-02 23:19:56
【问题描述】:

我有一个谷歌表格文件。在这个文件中,我为满足以下条件的每一行设置了背景颜色:

即:如果第 8 列为空,则将整行背景颜色设置为红色。

我已经能够做到这一点。但现在我想做的是在第 8 列已满的无色行/行之后将所有红色行向下推。

您能帮我解决这个问题吗?我有以下代码,但我完全不知道如何进行排序。

谢谢。

function SelectRecords() {
   const ss=SpreadsheetApp.getActive();
   const sh=ss.getSheets()[0];
   const rg=sh.getRange(3,1,sh.getLastRow()-2,sh.getLastColumn());
   let vs=rg.getValues()
   var rows=vs.map(function(r,i){
   if(r[8]=='') {
      sh.getRange(i+3,1,1,sh.getLastColumn()).setBackground('#ff0000'); 
      return r;
   }
   }).filter(function(e){return e;});
 }

 SHEET_NAME = "CURRENT MONTH";
 SORT_DATA_RANGE = "A4:J999";
 var SORT_ORDER = [{column: 8, ascending: false}];

function sorting(){
   var ss = SpreadsheetApp.getActiveSpreadsheet(); 
   var sheet = ss.getSheetByName(SHEET_NAME);
   var SORT_DATA_RANGE = "A4:I"; 

   var range = sheet.getRange(SORT_DATA_RANGE);

   range.sort(SORT_ORDER);
   ss.toast('Sort complete.');
} 

【问题讨论】:

  • 因为这不是您的第一个问题:请不要在问题中添加appscript 标签,因为您的问题与google-apps-script 有关。我们有一个漫长而痛苦的problem with the tag,如果您能帮助我们解决这个问题,我们将不胜感激
  • 哦,我明白了。将停止这样做。对此非常抱歉。但是你能给我一个线索吗?我真的很想知道这是怎么做到的。因为我现在正在使用 Google 表格。谢谢。
  • 不用担心 - 标签的原始用途在很久以前就过期了,但我们仍然不能使其成为 GAS 的同义词,因此问题......也就是说,我不认为你可以使用Rangesort 方法实现你想要的——据我所知,它只能按列值排序。你想做的是,嗯,有点复杂:获取所有背景,与行索引关联,然后sort 这些“元数据”对象,然后使用moveRows 类的moveRows 方法相应地移动行

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


【解决方案1】:

我相信您之前已经在另一个问题中提出过这个问题。

阅读他们告诉你每个步骤的作用的 cmets。

总而言之,我取了一列背景颜色并将其与数据数组连接起来,这样我就可以将它们一起排序,这样一旦它们被排序,我就可以知道每一行的最终颜色。我将颜色列展平,通过一个集合将其传递以获取唯一的颜色数组,并使用 colors.indexOf(组合数组的最后一列)在自定义排序函数中对数据数组进行排序。最后两个步骤涉及将组合数组拆分为其组成部分,以使用 setValues() 和 setBackgrounds() 来显示最终结果。

 function sortOnColors() {
  const ss=SpreadsheetApp.getActive();
  const sh=ss.getSheets()[0];
  const rg=sh.getRange(3,1,sh.getLastRow()-2,sh.getLastColumn());
  let vA=rg.getValues();
  let cA=sh.getRange(3,1,sh.getLastRow()-2,1).getBackgrounds();
  let vC=vA.map(function(r,i){return r.concat(cA[i][0]);});//concat of vA andd vC adding the color of the row as a value to each row
  let S=new Set(cA.map(function(r){return r[0]}));
  let colors=[...S];//this creates an array of unique colors
  vC.sort(function(a,b){return colors.indexOf(b[b.length-1])-colors.indexOf(a[a.length-1])});//this sorts the array on the colors index of the value in the last column which is the background color of the row and keeps the row and color tied together
  vD=vC.map(function(r,i){return r.slice(0,-1);});//this removes the last column
  vE=vC.map(function(r,i){let t=r.slice(0,-1);return t.fill(r[r.length-1],0);});//this create a 2d array of background colors for each row based upon the color value in the last column which is removed at the same time
  sh.getRange(3,1,vD.length,vD[0].length).setValues(vD);
  sh.getRange(3,1,vE.length,vE[0].length).setBackgrounds(vE);
}

之前:

之后:

【讨论】:

    猜你喜欢
    • 2016-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-28
    • 1970-01-01
    • 2019-03-28
    • 1970-01-01
    • 2018-03-16
    相关资源
    最近更新 更多