最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - google apps script for conditional color formatting of one cell based on another cell's value - Stack Overf

programmeradmin1浏览0评论

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.

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.

Share Improve this question asked Aug 8, 2012 at 1:34 koogeekoogee 9633 gold badges12 silver badges24 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

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);
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论