谷歌应用程序脚本根据另一个单元格的值为一个单元格进行条件颜色格式化 [英] google apps script for conditional color formatting of one cell based on another cell's value
问题描述
我正在尝试将Google应用程序脚本放在一起,该脚本根据另一个单元格的值更改单元格的字体颜色。如果单元格B2> D2,请将B2的字体颜色更改为红色。问题是我没有使用JavaScript的经验...所以我复制粘贴和编辑的小脚本不起作用。
函数格式化(){
var sheet = SpreadsheetApp.getActiveSpreadsheet()。getSheetByName('Sheet1');
var columnO = sheet.getRange(2,2,sheet.getLastRow() - 1,1);
var oValues = columnO.getValues();
var column1 = sheet.getRange(2,4,sheet.getLastRow() - 1,1);
var oValues1 = columnO.getValues();如果(oValues [i] [0]> oValues1 [i] [0]){$ b $(b
sheet.getRange(i,1,1,1).setFontColors('red');
}
}
}
我想延长这个脚本能够做到:如果B2> D2为红色,并且如果B2 < C2颜色为蓝色,否则为绿色。
column0.getValues()
应该是 column1.getValues()
。 另一个错误是在for循环中,在 getRange(i,1,1)
中。在你写的问题中,你想改变 B2
单元格颜色,这个语句改变了列 A
。另外,行索引应该从2开始,而不是0。
另一个需要考虑的脚本是使用批处理函数,因为它会加速它很大程度上,例如
function formatting(){
var sheet = SpreadsheetApp.getActiveSpreadsheet( ).getSheetByName( 'Sheet 1中');
var values = sheet.getRange(2,2,sheet.getLastRow() - 1,3).getValues(); // B,C和D
var colors = []; (var i = 0; i< values.length; i ++)
colors.push([values [i] [0]> values [i] [2])的
? 'red':
values [i] [0]< values [i] [2]?'blue':'green']);
sheet.getRange(2,2,colors.length,1).setFontColors(colors);
}
I'm trying to put together a google apps script that changes the font color of of a cell based on the value of another cell. If cell B2 > D2, change the font color of B2 to red. Problem is that I have no experience with javascript...so the little script I copy pasted and edited together doesn't work.
function formatting() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
var columnO = sheet.getRange(2, 2, sheet.getLastRow()-1, 1);
var oValues = columnO.getValues();
var column1 = sheet.getRange(2, 4, sheet.getLastRow()-1, 1);
var oValues1 = columnO.getValues();
for (var i = 0; i < oValues.length; i++) {
if (oValues[i][0] > oValues1[i][0]) {
sheet.getRange(i, 1, 1, 1).setFontColors('red');
}
}
}
I would like to extend this script to be able to do: if B2 > D2 color red and if B2 < C2 color blue, otherwise color green.
First thing I see is a mistake/typo on line 6: column0.getValues()
should be column1.getValues()
.
Another error is on the for-loop, where you getRange(i, 1, 1, 1)
. On your question you wrote you want to change B2
cell color and this statement is changing column A
. Also, the row index should start on 2, not 0.
Another important thing to consider on a script like this is to use batch functions, as it will speed it up greatly, e.g.
function formatting() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
var values = sheet.getRange(2, 2, sheet.getLastRow()-1, 3).getValues(); //B, C and D
var colors = [];
for (var i = 0; i < values.length; i++)
colors.push([values[i][0] > values[i][2] ? 'red' :
values[i][0] < values[i][2] ? 'blue' : 'green']);
sheet.getRange(2, 2, colors.length, 1).setFontColors(colors);
}
这篇关于谷歌应用程序脚本根据另一个单元格的值为一个单元格进行条件颜色格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!