我有一个Google表格文件。在此文件中,我为符合以下条件的每一行设置背景色:
即:如果第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.');
}
我相信您已经在另一个问题的一部分中提过这个问题了。
阅读他们告诉您几乎每个步骤的注释。
总之,我将一列背景色与数据数组连接起来,这样我就可以将它们都排序在一起,以便对它们进行排序后,我就能知道每一行的最终颜色。我展平了color列,将其放到一个集合中以得到唯一的颜色数组,并使用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);
}
之前:
后:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句