脚本在单元更改文本时更改行颜色

脚本在单元更改文本时更改行颜色

本文介绍了Google Spreadsheet:脚本在单元更改文本时更改行颜色;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个googlespreadsheet,在那里我保存一个bug列表,每当我修复一个bug时,我将状态从未开始更改为完成。我想为Google文档电子表格编写脚本,以便每当将状态更改为完成时,整行以某种颜色突出显示。



我已经知道Google Spreadsheet已经有了改变文本颜色的功能,但是该功能只改变单元格的颜色,并且不会改变整行的颜色。 解决方案

  //根据状态列中的值设置行颜色。 
函数setRowColors(){
var range = SpreadsheetApp.getActiveSheet()。getDataRange();
var statusColumnOffset = getStatusColumnOffset(); (var i = range.getRow(); i< range.getLastRow(); i ++){
rowRange = range.offset(i,0,1);


status = rowRange.offset(0,statusColumnOffset).getValue();
if(status =='Completed'){
rowRange.setBackgroundColor(#99CC99);
} else if(status =='In Progress'){
rowRange.setBackgroundColor(#FFDD88);
} else if(status =='Not Started'){
rowRange.setBackgroundColor(#CC6666);
}
}
}

//返回标题为状态
//的列的偏移值(例如,如果第7列是标签为Status,这个函数返回6)
函数getStatusColumnOffset(){
lastColumn = SpreadsheetApp.getActiveSheet()。getLastColumn();
var range = SpreadsheetApp.getActiveSheet()。getRange(1,1,1,lastColumn); (var i = 0; i< range.getLastColumn(); i ++){
if(range.offset(0,i,1,1).getValue()=

=状态){
return i;
}
}
}


I have a googlespreadsheet where I keep a list of bugs and whenever I fix a bug I change the status from "Not Started" to "Complete". I want to write a script for the Google Docs spreadsheet such that whenever I change the status to "Complete" the entire row gets highlighted in a certain color.

I already know that Google Spreadsheet already has "change color on text" but that function only changes the color of the cell and does not change the color of the entire row.

解决方案
//Sets the row color depending on the value in the "Status" column.
function setRowColors() {
  var range = SpreadsheetApp.getActiveSheet().getDataRange();
  var statusColumnOffset = getStatusColumnOffset();

  for (var i = range.getRow(); i < range.getLastRow(); i++) {
    rowRange = range.offset(i, 0, 1);
    status = rowRange.offset(0, statusColumnOffset).getValue();
    if (status == 'Completed') {
      rowRange.setBackgroundColor("#99CC99");
    } else if (status == 'In Progress') {
      rowRange.setBackgroundColor("#FFDD88");
    } else if (status == 'Not Started') {
      rowRange.setBackgroundColor("#CC6666");
    }
  }
}

//Returns the offset value of the column titled "Status"
//(eg, if the 7th column is labeled "Status", this function returns 6)
function getStatusColumnOffset() {
  lastColumn = SpreadsheetApp.getActiveSheet().getLastColumn();
  var range = SpreadsheetApp.getActiveSheet().getRange(1,1,1,lastColumn);

  for (var i = 0; i < range.getLastColumn(); i++) {
    if (range.offset(0, i, 1, 1).getValue() == "Status") {
      return i;
    }
  }
}

这篇关于Google Spreadsheet:脚本在单元更改文本时更改行颜色;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 18:17