Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

Google Spreadsheet: Script to change background color of a cell based on a hex code in the neighboring cell

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...