简体   繁体   English

如何根据 Google Apps 脚本中的 .getValues( ) 方法的结果对单元格应用条件格式?

[英]How do I apply conditional formatting to cells based upon the result of the .getValues( ) method in Google Apps Script?

I am writing this script to change the formatting of a row of checkboxes that total 0 selections.我正在编写此脚本以更改总共 0 个选择的一排复选框的格式。 In my data validation, "true" = 1 and "false" = 0. The range is represented as an object [ ][ ].在我的数据验证中,“true” = 1 和“false” = 0。范围表示为 object [ ][ ]。

The logic below determines which rows need to be formatted, but how do I actually apply the formatting to this row?下面的逻辑决定了哪些行需要格式化,但我如何将格式化实际应用到这一行? I am having trouble accessing the row's formatting because I am working at the value level with the resulting object [ ] [ ].我无法访问行的格式,因为我正在使用生成的 object [ ] [ ] 在值级别上工作。

Here is what the end result should look like: Camping Gear Tally最终结果应该是这样的:野营装备理货

(I recognize there is probably an easy way to do this with conditional formatting built in, but I'm just trying to get better at coding) (我认识到可能有一种简单的方法可以使用内置的条件格式来做到这一点,但我只是想在编码方面做得更好)

 function updateFormatting() { var cellValues = SpreadsheetApp.getActiveSheet().getRange("C2:I37").getValues(); for (let i = 0; i < cellValues.length; i++) { let rowTotal = 0; for (let j = 0; j < cellValues[i].length; j++) { rowTotal += cellValues[i][j]; } if (rowTotal == 0) { /* Apply formatting to this row */ } } }

Thank you for your guidance!感谢您的指导!

I believe your goal is as follows.我相信你的目标如下。

  • You want to set the background color of the rows that all checkboxes of columns "C" to "I" are unchecked.您想设置未选中列“C”到“I”的所有复选框的行的背景颜色。
  • In your situation, the values of the checked and the unchecked checkboxes are 1 and 0 , respectively.在您的情况下,选中和未选中复选框的值分别为10

Modification points:修改点:

  • When the values of the checked and the unchecked checkboxes are 1 and 0 , respectively, when those values are retrieved by getValues , those are retrieved as the string type.当检查的值和未选中的复选框的值分别为10时,当这些值通过getValues检索时,将这些值作为字符串类型检索。 So, in your script, when rowTotal += cellValues[i][j];因此,在您的脚本中,当rowTotal += cellValues[i][j]; is run, the values are added as the string.运行时,值将作为字符串添加。 Please be careful about this.请注意这一点。
  • About /* Apply formatting to this row */ , when the script for setting the cell format is put in a loop, the process cost will be high.关于/* Apply formatting to this row */ ,当设置单元格格式的脚本放在循环中时,处理成本会很高。

When these points are reflected in your script, how about the following modification?当这些点反映在你的脚本中时,下面的修改怎么样?

Modified script:修改后的脚本:

function updateFormatting() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var cellValues = sheet.getRange("C2:I37").getValues();
  var ranges = [];
  for (let i = 0; i < cellValues.length; i++) {
    let rowTotal = 0;
    for (let j = 0; j < cellValues[i].length; j++) {
      rowTotal += Number(cellValues[i][j]);
    }
    if (rowTotal == 0) {
      var row = i + 2;
      ranges.push(`C${row}:I${row}`);
    }
  }
  sheet.getRangeList(ranges).setBackground("#fce5cd");
}
  • In this modification, the range list is created in a loop.在此修改中,范围列表是在循环中创建的。 And, it is used with sheet.getRangeList(ranges).setBackground("#fce5cd") and the background color of rows are set as #fce5cd .并且,它与sheet.getRangeList(ranges).setBackground("#fce5cd")使用,并且行的背景颜色设置为#fce5cd

Reference:参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何对 JavaScript 数组的值应用条件格式? - How do I apply conditional formatting on the values of a JavaScript array? 如何改进用于在 Google Apps 脚本中拖动多个单元格的脚本? - How do I improve my script for dragging multiple cells in Google Apps Scripts? 如何使用 Google Apps 脚本对 10 个单元格进行分组 - How can I group 10 cells using Google Apps Script Google App Script:根据条件循环列出单元格项目 - Google App Script: Listing Cell Items Based Upon Conditional Loop 如何使用单元格的应用程序脚本将 Google 表格数据验证添加到列中,以仅允许输入颜色和十六进制颜色代码 - How do I add Google sheets data vaildation to a column using apps script for cells to only allow colors and hex color codes to be inputted 如何将 Google Apps 脚本应用于多个工作表 - How to apply Google Apps Script to multiple sheets Google Apps 脚本(电子表格) - 根据单元格中的条件选择电子表格中的数组 - Google Apps Script (Spreadsheet) - selecting an array in spreadsheet based on a condition in cells 如何在Google Apps脚本中异步运行方法? - How can I run a method asynchronously in Google Apps Script? 应用程序脚本条件格式的值不在数组中 - Apps script conditional formatting for value not in array Google Apps 脚本或 JavaScript - 查找和替换结果比要求的高一排 - 如何将其上移一排以进行替换? - Google Apps Script or JavaScript - Find & Replace result is one row higher than required - How do I move it one row up to replace?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM