我一直在努力寻找我需要做的事情但我无法通过之前发现的帖子弄清楚如何做到这一点。
我有这个Google电子表格here
想要我'我们喜欢做的是分析名为" actual"并在满足条件时设置单元格的背景颜色。所以基本上,我正在寻找一个脚本来做到这一点:
get the data from the sheet called 'actual'
if a cell is error (equals to "#N/A") then set the color font to white
if a cell equals to "wnd" then set the background color to "red"
if the cell equals to "otc", then set the background color to "green"
etc..
它将有大约50个条件,这就是为什么我喜欢用代码而不是常规的条件格式来做这个。
提前谢谢。
答案 0 :(得分:0)
function setCellColors() {
//Get the sheet you want to work with.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet()
// determine how how big your data range will be
var lastColumn = sheet.getLastColumn() + 1
var lastRow = sheet.getLastRow() + 1
// iterate through the data.
for (var column = 1; column < lastColumn; column++) {
for (var row = 1; row < lastRow; row++) {
var cellget = sheet.getRange(row, column).getValue();
var cellset = sheet.getRange(row, column);
//Set the rules logic
if (cellget === "#N/A") {
//Set the cell background
cellset.setBackground("red");
cellset.setFontColor("white");
}
}
}
}
答案 1 :(得分:0)
更改代码以尽量减少使用get调用。这些都是慢速通话。所以你想要一个大的。然后处理所有数据。这是一些示例代码:
注意此功能检查值3并将其设置为带有白色文本的红色背景。其工作的当前范围在var范围内定义。把它自己设置为你需要的任何东西。
function setCellColors() {
//Get the sheet you want to work with.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
//Grab the entire Range, and grab whatever values you need from it. EX: rangevalues
var range = sheet.getRange("A1:E17");
var rangevalues = range.getValues();
//Loops through range results
for (var i in rangevalues) {
for (var j in rangevalues) {
//Get the x,y location of the current cell.
var x = parseInt(j, 10) + 1;
var y = parseInt(i, 10) + 1;
//Set the rules logic
if (rangevalues[i][j] == 3) {
//Set the cell background
sheet.getRange(y,x).setBackground("red");
sheet.getRange(y,x).setFontColor("white");
}
}
}
}