Google Spreadsheet:脚本,用于根据相邻单元格中的十六进制代码更改单元格的背景颜色 [英] Google Spreadsheet: Script to change background color of a cell based on a hex code in the neighboring cell

查看:154
本文介绍了Google Spreadsheet:脚本,用于根据相邻单元格中的十六进制代码更改单元格的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个脚本,它将采用一列十六进制代码,并将相邻列的背景颜色设为该十六进制代码颜色,但我不确定如何操作。



我一直在寻找这些:

1) https://developers.google.com/apps-script/reference/spreadsheet/range#setbackgroundcolor



2) https://developers.google.com/apps-script/reference/spreadsheet/range#getcellrow-column



3) Google Spreadsheet:当单元格更改文本时更改行的颜色的脚本;



但是还没有真正的进步。任何帮助将不胜感激!



谢谢!

解决方案

这应该可以解决你的问题:

$ p $ function onEdit(){

var sheet = SpreadsheetApp.getActiveSheet( );
var range = sheet.getDataRange();
var actCell = sheet.getActiveCell();
var actData = actCell.getValue();
var actRow = actCell.getRow();
if(actData!=''&& actRow!= 1)//留出空行和标题行
{
range.getCell(actRow,2).setBackground(actData) ;
}

}

虽然这只会着色一个单元格但它描绘了如何根据单元格输入设置背景颜色。根据您的使用情况,您是否一次复制粘贴多个十六进制代码,或者您是否有现有的十六进制代码列表,并且您想使用 getCell(row,column ).setBackground(String)应该能够帮助你。


I'm trying to write a script which will take a column of hex codes and color the adjacent column's background color to be that hex code color, but I'm just not sure how to do it.

I've been looking at these:

1) https://developers.google.com/apps-script/reference/spreadsheet/range#setbackgroundcolor

2) https://developers.google.com/apps-script/reference/spreadsheet/range#getcellrow-column

3) Google Spreadsheet: Script to Change Row Color when a cell changes text;

But haven't really hade any progress. Any help would be appreciated!

Thanks!

解决方案

Something like this should solve your problem:

function onEdit() {

  var sheet = SpreadsheetApp.getActiveSheet();
  var range = sheet.getDataRange();
  var actCell = sheet.getActiveCell();
  var actData = actCell.getValue();
  var actRow = actCell.getRow();
  if (actData != '' && actRow != 1)  //Leaving out empty and header rows
  {
    range.getCell(actRow, 2).setBackground(actData);
  }

}

Although, this only colors one cell at a time, but it portrays how you can set a background color based on cell input. Depending on your use case, whether you are copy-pasting multiple hex codes at a time or whether you have an existing list of hexcodes and you would like to show their color against those, using getCell(row,column).setBackground(String) should be able to help you out.

这篇关于Google Spreadsheet:脚本,用于根据相邻单元格中的十六进制代码更改单元格的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆