Javascript 如何计算具有特定文本的单元格&;颜色

Javascript 如何计算具有特定文本的单元格&;颜色,javascript,google-apps-script,google-sheets,Javascript,Google Apps Script,Google Sheets,我需要计算谷歌表格中具有特定颜色和文本的单元格。 我找到了一个用颜色计算细胞的代码,在这里 function countColor(countRange,colorRef) { var activeRg = SpreadsheetApp.getActiveRange(); var activeSht = SpreadsheetApp.getActiveSheet(); var activeformula = activeRg.getFormula(); var countRang

我需要计算谷歌表格中具有特定颜色和文本的单元格。 我找到了一个用颜色计算细胞的代码,在这里

function countColor(countRange,colorRef) {
  var activeRg = SpreadsheetApp.getActiveRange();
  var activeSht = SpreadsheetApp.getActiveSheet();
  var activeformula = activeRg.getFormula();
  var countRangeAddress = activeformula.match(/\((.*)\,/).pop().trim();
  var backGrounds = activeSht.getRange(countRangeAddress).getBackgrounds();  
  var colorRefAddress = activeformula.match(/\,(.*)\)/).pop().trim();
  var BackGround = activeSht.getRange(colorRefAddress).getBackground();
  var countCells = 0;
  for (var i = 0; i < backGrounds.length; i++)
    for (var k = 0; k < backGrounds[i].length; k++)
      if ( backGrounds[i][k] == BackGround )
        countCells = countCells + 1;
  return countCells;
};
其中,
A:A1
是范围,
C1
是带有颜色样本的单元格

我的一个朋友帮了我一点忙,做了一个计算文本和颜色的函数,但是文本应该在函数内部键入。 代码如下:

function count_Color_and_text_Cells(countRange,colorRef) { 
var activeRg = SpreadsheetApp.getActiveRange(); 
var activeSht = SpreadsheetApp.getActiveSheet(); 
var activeformula = activeRg.getFormula(); 
var countRangeAddress = activeformula.match(/\((.*)\,/).pop().trim(); 
var backGrounds = activeSht.getRange(countRangeAddress).getBackgrounds(); 
var text = "test" // Text you whant to count
var arrData = activeSht.getRange(countRangeAddress).getDisplayValues() 
var colorRefAddress = activeformula.match(/\,(.*)\)/).pop().trim(); 
var BackGround = activeSht.getRange(colorRefAddress).getBackground(); 
var countCells = 0; 
for (var row = 0; row < backGrounds.length; row++) 
for (var col = 0; col < backGrounds[row].length; col++) 
if ( backGrounds[row][col] == BackGround ) 
if (arrData[row][col].match(text)) 
countCells = countCells + 1; 
return countCells; 

}
因此,它可以在公式中计算具有适当颜色和用户键入文本的单元格


谢谢你,祝你今天愉快

您可以提供文本作为参数,如下所示:

function count_Color_and_text_Cells(countRange,text,colorRef) { 
不要再次声明
text
,即删除
var text='

阅读和练习:

您可以提供如下文本作为参数:

function count_Color_and_text_Cells(countRange,text,colorRef) { 
不要再次声明
text
,即删除
var text='

阅读和练习:

text
添加为该函数的可接受参数?请大师解释?将
text
添加为该函数的可接受参数?请大师解释?
function count_Color_and_text_Cells(countRange,text,colorRef) {