如果单元格的值等于其上方两行的单元格的值,则需要使单元格文本变为红色。我的脚本编辑器具有以下两个功能:

/**
 * Compares cell value to another cell relative in position to it.
 * Returns true if equal values.
 *
 * @param {Number} colDiff Relative positioning column difference.
 * @param {Number} rowDiff Relative positioning row difference.
 */
function equalsRelativeCellValue(rowDiff, colDiff) {
  var thisCell = SpreadsheetApp.getActiveRange();
  var relativeCellValue = getRelativeCellValue(rowDiff, colDiff);
  if (thisCell.getValue() === relativeCellValue)
    return true;
  else
    return false;
}


/**
 * Returns value of cell according to relative position
 *
 * @param {Number} colDiff Relative positioning column difference.
 * @param {Number} rowDiff Relative positioning row difference.
 */
function getRelativeCellValue(rowDiff, colDiff) {
  var range = SpreadsheetApp.getActiveRange();
  var col = range.getColumn();
  var row = range.getRow();
  var range2 = SpreadsheetApp.getActiveSheet().getRange(row + rowDiff,col + colDiff);
  return range2.getValue();
}


第二个功能getRelativeCellValue(rowDiff, colDiff)正常工作,我将8放在一个单元格中,并在其下方的两个单元格中输入getRelativeCellValue(-2, 0),并将该单元格评估为8

但是第一个函数getRelativeCellValue(rowDiff, colDiff)由于某些原因无法在条件格式中用作我的自定义函数:

Custom formula is: =equalsRelativeCellValue(-2,0)


困难的部分是在条件格式中引用被引用的单元格的值。但是我的函数对我来说似乎正确,如果单元格值相等,则返回true,否则返回false。我希望我只是不正确地使用条件格式的“自定义公式为”功能,但是documentation is pretty sparse

最佳答案

只需了解您要执行的操作,就可以使用条件格式设置,选择范围并申请第一个单元格,它将正确地适用于所有单元格:

例如。
在条件格式对话框中,选择“自定义公式”,粘贴自定义公式=J10=J8,然后选择范围J10:J100

旧答案:

您创建了一个循环引用。


如果在单元格中输入=equalsRelativeCellValue(-2,0),并且它在等待函数解析,它的值怎么可能是什么?

您可以在值之外的一栏中克服该问题,或直接在函数中传递值。

您还可以使用它来使between单元格具有true / false状态:

function equalsRelativeCellValue(rowDiff, colDiff) {
  var below = getRelativeCellValue(1, 0);
  var relativeCellValue = getRelativeCellValue(2, 0);
  if (below === relativeCellValue)
    return true;
  else
    return false;
}

10-05 20:55
查看更多