简体   繁体   English

尝试根据 Google Apps 脚本中的关键字设置背景颜色

[英]Trying to setBackgroundColor based off of a keyword in Google Apps Script

I'm trying to highlight a row in a table based off of the relevant keyword.我正在尝试根据相关关键字突出显示表格中的一行。 However, my attempts at setting the background color of my range of cells have not yielded any colors.但是,我尝试设置单元格范围的背景颜色并没有产生任何 colors。 I used loggers inside the if/else statement and they all worked, so I think I'm doing setBackgroundColor wrong somehow.我在 if/else 语句中使用了记录器,它们都有效,所以我认为我在某种程度上做错了setBackgroundColor

function statusHighlighter(){
  
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheetName = ss.getActiveSheet();
  var sheetValues = ss.getDataRange().getValues();

  sheetValues.forEach(function(row, index) {
    var acctStatus = row[7];
    var highlightRange = sheetName.getRange(index+1, 1, 1, sheetName.getLastColumn());

    if(acctStatus === "Kept"){
      highlightRange.setBackgroundColor('##fffec4');
      Logger.log(acctStatus + " " + index);
    } else if(acctStatus === "Suspended"){
      highlightRange.setBackground('##cf4a4a');
    } else if(acctStatus === "Deleted"){
      highlightRange.setBackground('##6c75ad');
    }
  });
} 

Explanation / Issues:说明/问题:

There is one issue in your code and a couple of optimizations that I would like to suggest.您的代码中有一个问题,我想提出一些优化建议。

  • The hex color codes must contain only one # but you are using two ## .十六进制颜色代码必须只包含一个#但您使用的是两个##

  • You are iteratively using setBackground but instead you can create an array with colors inside the forEach loop and then use setBackgrounds to set the colors in one go.您正在迭代地使用setBackground ,但您可以在forEach循环中创建一个包含 colors 的数组,然后使用setBackgrounds在一个 Z34D1F91FB2E514B8576FAB1A75A8A 中设置 colors。 Have a look at best practices to see why.查看最佳实践以了解原因。

  • Since you have a particular column of interest, it makes sense to consider only one column instead of the full dataRange .由于您有一个特定的列感兴趣,因此只考虑列而不是完整的dataRange是有意义的。 In the following script, acctStatusVals considers only column 8 .在以下脚本中, acctStatusVals仅考虑8列。 I choose 8 because in your code you use the array index for 7 which is equivalent for column 8 .我选择8是因为在您的代码中,您使用7的数组索引,这相当于8列。 Be careful on that.对此要小心。 In my code, 8 means column H .在我的代码中, 8表示H列。 Adjust this to your needs.根据您的需要进行调整。 To convert the 2D array into 1D since we are using a single column, you can use flat .由于我们使用的是单列,因此要将 2D 数组转换为 1D,您可以使用flat

  • I use the following expression: Array(lastCol).fill('#6c75ad') to create an array of lastCol number of elements ( last column with content ) and fill it with the same value, because the goal is to color the full row with the same color value.我使用以下表达式: Array(lastCol).fill('#6c75ad')创建一个包含lastCol元素数的数组( 最后一列内容)并用相同的值填充它,因为目标是为整行着色具有相同的颜色值。

Optimized Solution:优化解决方案:

Be careful with the active sheet.小心活动表。 Make sure you select the right sheet before you run this code.在运行此代码之前,请确保 select 正确。 I would advice you to use ss.getSheetByName('Sheet1');我建议你使用ss.getSheetByName('Sheet1'); but I leave it as a comment since this step is optional.但我将其作为评论留下,因为这一步是可选的。

function statusHighlighter(){ 
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheetName = ss.getActiveSheet();
//  const sheetName = ss.getSheetByName('Sheet1'); // change to the name of your sheet
  const lastRow = sheetName.getLastRow();
  const lastCol = sheetName.getLastColumn();
  const acctStatusVals = sheetName.getRange(1,8,lastRow).getValues().flat();
  const colors = [];
  acctStatusVals.forEach(acctStatus=>{
   if(acctStatus === "Kept"){
      colors.push(Array(lastCol).fill('#fffec4'));
    } else if(acctStatus === "Suspended"){
      colors.push(Array(lastCol).fill('#cf4a4a'));
    } else if(acctStatus === "Deleted"){
      colors.push(Array(lastCol).fill('#6c75ad'));
    }                   
  });
  sheetName.getRange(1, 1, lastRow, lastCol).setBackgrounds(colors);
}

Sheet used for the above script:用于上述脚本的工作表:

在此处输入图像描述

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

相关问题 Google Apps 脚本根据 email 正文中的关键字发送不同的自动回复 - Google Apps Script to send different auto-replies based on keyword in email body Google Apps脚本:尝试根据单元格的背景色发送电子邮件 - Google Apps Script: Trying To Send Emails Based Upon Background Color Of Cell 尝试在 Google Apps 脚本中编写 IF/AND 语句 - Trying to write IF/AND statement in Google Apps Script 为什么使用 Google Apps 脚本的日期值结果会相差 1 天? - Why is the date value result off by 1 day with Google Apps Script? Google Apps 脚本:读取持续时间值已关闭 X 分钟 - Google Apps Script: Reading Duration values is off by X minutes 使用带有Google Apps脚本的基于Google Spreadsheets的列表框制作Webapp - Making a webapp with listboxes based on Google Spreadsheets with Google Apps Script 尝试执行Google Apps脚本时值不正确 - Bad value when trying to execute google Apps Script ParseError: Unexpected token = 尝试在 Google Apps 脚本编辑器中保存时 - ParseError: Unexpected token = when trying to save in google Apps Script editor 尝试在 Google Apps 脚本中获取文件的文件夹路径 - Trying to get folder path of file in Google Apps Script 尝试使用应用程序脚本制作基本的谷歌表格功能 - Trying to make a basic google sheets function with apps script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM