直接在 Apps 脚本中按单元格颜色对 google-sheet 值进行排序

Posted

技术标签:

【中文标题】直接在 Apps 脚本中按单元格颜色对 google-sheet 值进行排序【英文标题】:Sorting google-sheet values by cells color directly in Apps Script 【发布时间】:2020-10-08 13:10:41 【问题描述】:

今年年初(我认为)Google 发布了在 google-sheet 中按颜色排序(和过滤)的功能。

我想在我的工作表中使用此功能,但我所有的操作都是在我的脚本中以编程方式完成的(对于一个简化的示例): SpreadsheetApp.getActive().getSheetByName("Test").getRange("A:I").sort(column 2, ascending: false);(实际上参数将在排序之外计算,但这不是这里的问题)。

我正在努力寻找文档或示例来填充传递给Range.sort() 函数的SortSpecObj 以添加关于单元格颜色的条件。我试过了:colorbackgroundbgcolorfillfillcolor,......但它们似乎都不起作用。

试图从工作表中提取值以对其进行排序并将它们推回工作表中,我也希望添加另一个列到我的工作表和数值以进行排序。我想通过代码访问 Web 应用程序中以图形方式提供的功能。

或者如果我们可以覆盖class Range使用的排序函数,但由于代码是原生的,我认为这是不可能的,否则性能会受到太大影响。

非常感谢。

【问题讨论】:

我不认为你想要做的事情是可能的类范围。它与允许外部提供排序功能的 javascript array.sort() 方法不同。 【参考方案1】:

我相信你的目标如下。

您希望使用 Google Apps 脚本按单元格的背景颜色对范围进行排序。

问题和解决方法:

不幸的是,在当前阶段,Class Range 的sort(sortSpecObj) 似乎无法按单元格的背景颜色进行排序。但是当使用 Sheets API 时,我认为您的目标可以实现。在这个答案中,作为一种解决方法,我想建议在 Sheets API 中使用“spreadsheets.batchUpdate”方法的“SortRangeRequest”。

此示例脚本的流程如下。

    从单元格中检索背景颜色。 为使用 Sheets API 的 batchUpdate 方法创建请求正文。 使用请求正文向 Sheets API 请求。

示例脚本:

示例脚本如下。请将以下脚本复制并粘贴到电子表格的容器绑定脚本的脚本编辑器中。和please enable Sheets API at Advanced Google services。这样,Sheets API 就可以与 Google Apps 脚本一起使用。

function myFunction() 
  const sheetName = "Sheet1";  // Please set the sheet name.
  const a1Notation = "A1:C10"; // Please set the sort range as a1Notation.

  // 1. Retrieve the background colors from the cells.
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName(sheetName);
  const range = sheet.getRange(a1Notation);
  const backgrounds = range.getBackgroundObjects();
  
  // 2. Create the request body for using the batchUpdate method of Sheets API.
  const backgroundColors = Object.values(
    backgrounds.reduce((o, [a]) => 
      const rgb = a.asRgbColor();
      return Object.assign(o, [rgb.asHexString()]: red: rgb.getRed() / 255, green: rgb.getGreen() / 255, blue: rgb.getBlue() / 255)
    , )
  );
  const startRow = range.getRow() - 1;
  const startColumn = range.getColumn() - 1;
  const srange = 
    sheetId: sheet.getSheetId(),
    startRowIndex: startRow,
    endRowIndex: startRow + range.getNumRows(),
    startColumnIndex: startColumn,
    endColumnIndex: startColumn + range.getNumColumns()
  ;
  const requests = [
    sortRange: range: srange, sortSpecs: [dimensionIndex: 0, sortOrder: "ASCENDING"],
    sortRange: range: srange, sortSpecs: backgroundColors.map(rgb => (backgroundColor: rgb))
  ];
  
  // 3. Request to Sheets API using the request body.
  Sheets.Spreadsheets.batchUpdate(requests: requests, ss.getId());

在此示例中,在运行背景颜色排序之前,“A”列按“ASCENDING”排序。

结果:

使用上述脚本,可以得到如下结果。

注意:

如果要设置颜色顺序,请设置sortSpecs的数组,如sortSpecs: [backgroundColor: rgb1, backgroundColor: rgb2, backgroundColor: rgb3,,,]

参考资料:

sort(sortSpecObj) Method: spreadsheets.batchUpdate SortRangeRequest

【讨论】:

以上是关于直接在 Apps 脚本中按单元格颜色对 google-sheet 值进行排序的主要内容,如果未能解决你的问题,请参考以下文章

使用 AppS 脚本返回 Google 表格单元格的绝对值

Google Apps脚本:表格形式数据处理,如果某些单元格为空,则删除行,同时维护某些列

如何获取显示#ERROR的单元格的后端值!使用 Google Apps 脚本?

如何修复本地时间的 Google Apps 脚本并自动清除单元格

Google Apps脚本:动态单元格位置;无权调用setValues

如何使用google apps脚本在自定义函数中编辑自定义函数的调用单元格?