![](/img/trans.png)
[英]How to use an if statement in conditional formatting for Google Sheets?
[英]Google Sheets custom function conditional formatting
如果單元格的值等於其上方兩行的單元格的值,則需要使單元格文本變為紅色。 我的腳本編輯器具有以下兩個功能:
/**
* 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
。 我希望我只是不恰當地使用條件格式的“自定義公式為”功能,但是文檔很少 。
只需了解您要執行的操作,就可以使用條件格式設置,選擇范圍並申請第一個單元格,它將正確地適用於所有單元格:
例如。 在條件格式對話框中,選擇“自定義公式”,粘貼自定義公式=J10=J8
,然后選擇范圍J10:J100
。
OldAnswer:
您創建了一個循環引用。
如果在單元格中輸入 =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; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.