google-apps-script - 使用自定义比较器功能对一系列单元格中的背景颜色进行排序

标签 google-apps-script google-sheets

我使用脚本对列中特定项目的工作表进行排序。 此列中的数据是以字母开头的数字,因此 rangesheet 类中的普通 sort 方法无法正常工作。

这是脚本,实际问题在下面。

function sortIDs(){ // test on column 5
  sortOnNumbersInCol(5);
}

function sortOnNumbersInCol(col){ // numeric sort except for 2 first rows on numeric value in column col
  var sh = SpreadsheetApp.getActive().getSheetByName('copie de travail');
  var data = sh.getDataRange().getValues();
  //Logger.log('length départ='+data.length);
  var codes = data.shift();
  var headers = data.shift();
  var dataOut=[];
  for(var n=0 ; n<data.length ; n++){
    if(data[n][col] != ''){
      dataOut.push(data[n]);
      //Logger.log('data['+n+']['+col+']='+data[n][col].substring(7)+'|');
    }
  }  
  dataOut.sort(function(a,b){
    var aE,bE;
    aE=a[col].substring(7); // the numeric part comes at the 7 th position (and following)
    bE=b[col].substring(7);
    return aE - bE
  })
  dataOut.unshift(headers);
  dataOut.unshift(codes);
  sh.getRange(1,1,dataOut.length,dataOut[0].length).setValues(dataOut);
}

这非常适合数据排序,但它显然不关心单元格颜色...... 我的一些同事使用颜色来指定此工作表中的项目,而当我对范围进行排序时,颜色并没有遵循。

所以我的问题是:如何根据我的特定标准对这张工作表进行排序并保持与单元格颜色的相关性?

下面是工作表数据的屏幕截图

screenshot of the sheet

如果我在此范围内运行我的脚本,颜色将不会移动...那是我的问题;)

最佳答案

更新:

另一种没有空间复杂度的方法是对键/索引而不是实际数组进行排序。

function sortOnNumbersInCol(col = 5) {
  const sh = SpreadsheetApp.getActive().getSheetByName('copie de travail');
  const dataRg = sh.getDataRange();
  const noOfHeadrs = 2;
  const rg = dataRg.offset(
    //Remove  headers before getting values
    noOfHeadrs,
    0,
    dataRg.getNumRows() - noOfHeadrs,
    dataRg.getNumColumns()
  );
  let data = rg.getValues();
  let colors = rg.getBackgrounds();

  /* Filter Empty rows if needed 
  [data, colors] = [data, colors].map(arr =>
    arr.filter((_, i) => data[i][col] !== '')
  );*/

  const dataKeys = [...data.keys()];
  dataKeys.sort(function(a, b) {
    return data[a][col].substring(7) - data[b][col].substring(7);
  });
  const [dataOut, colorsOut] = [data, colors].map(arr =>
    dataKeys.map(i => arr[i])
  );
  const outRg = sh.getRange(
    noOfHeadrs + 1,
    1,
    dataOut.length,
    dataOut[0].length
  );
  outRg.setValues(dataOut);
  outRg.setBackgrounds(colorsOut);
}

创建一个 map {数据:颜色}:

  const rg = sh.getDataRange();
  const data = rg.getValues();
  const colors = rg.getBackgrounds();
  const dcMap = data.reduce((mp, row, i) => mp.set(row[col], colors[i]),new Map)

然后在排序之后,您可以使用排序后的值作为键并创建一个新的排序颜色数组:

  dataOut.sort(function(a,b){
    var aE,bE;
    aE=a[col].substring(7); // the numeric part comes at the 7 th position (and following)
    bE=b[col].substring(7);
    return aE - bE
  })
  dataOut.unshift(headers);
  dataOut.unshift(codes);

  const sortedColors = dataOut.map(row=>dcMap.get(row[col]));
  const outRg = sh.getRange(1,1,dataOut.length,dataOut[0].length);
  outRg.setValues(dataOut);
  outRg.setBackgrounds(sortedColors);
}

关于google-apps-script - 使用自定义比较器功能对一系列单元格中的背景颜色进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63100263/

相关文章:

javascript - 在 Apps 脚本函数中获取 html 文本框的值

google-sheets - 为什么 Google 财经在 Google 表格中显示 IOC(印度石油公司)的错误价格?

javascript - 您无权调用 AppendRow

javascript - 简单 "Delete Sheet/Insert Sheet"脚本的服务器错误

CSS3 列填充水平溢出

javascript - 如何格式化此 Google Apps 脚本中的时间?

javascript - 更高效的 'remove keywords' 函数

google-sheets - 在 Google 表格中粘贴 dd/mm/yyyy 日期

regex - 如何规范/关联Google表格中的Unicode字符?

javascript - 谷歌应用脚​​本: Calling a function from menu with a spreadsheet range as the parameter