Javascript 按背景颜色计数行失败
嘿,伙计们,我在谷歌表格excel中,我想数一数有多少行的表格是以背景色为基础的 我有下一个代码:Javascript 按背景颜色计数行失败,javascript,regex,google-apps-script,google-sheets,Javascript,Regex,Google Apps Script,Google Sheets,嘿,伙计们,我在谷歌表格excel中,我想数一数有多少行的表格是以背景色为基础的 我有下一个代码: function countColoredCells(countRange,colorRef) { var activeRg = SpreadsheetApp.getActiveRange(); var activeSht = SpreadsheetApp.getActiveSheet(); var activeformula = activeRg.getFormula(); va
function countColoredCells(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;
};
function onOpen() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Contar",
functionName : "countColoredCells"
}];
spreadsheet.addMenu("Contar numero de casillas por color", entries);
}
当您运行
activeformula.match(/\(.*),/)
时,我认为当.match()
为时会出现一些实例,因此您无法调用.pop()。
你必须这样做
var match = activeformula.match(/\((.*)\,/);
if(match) var countRangeAddress = match.pop().trim(); //if match is not null...
但是当然要处理在脚本的其余部分不包含该变量的问题。您发布的脚本使用了一种不寻常的获取范围参数的方法。它在正则表达式中失败,因为在您所在的位置,工作表使用
;
作为分隔符,而不是,
使匹配为空。无论如何,直接获取参数应该是可行的。因此,请尝试更改原始代码
从
到
您还可以尝试用;
替换正则表达式匹配,
(如匹配(/\(.*)\;/)
)
用法应如下所示:
=countcoloredcells("B1:B9";"A1")
请改用此代码。
参数需要作为字符串传递
/**
* Usage countColoredCells("A1:B3";"C5")
* @param {range} countRange Range to be evaluated
* @param {colorRef} colorRef Cell with background color to be searched for in countRange
* @return {number}
* @customfunction
*/
function countColoredCells(countRange,colorRef) {
var activeRange = SpreadsheetApp.getActiveRange();
var activeSheet = activeRange.getSheet();
var formula = activeRange.getFormula();
var backgrounds = activeSheet.getRange(countRange).getBackgrounds();
var colorRefBackground = activeSheet.getRange(colorRef).getBackground();
var count = 0;
for(var i=0;i<backgrounds.length;i++)
for(var j=0;j<backgrounds[0].length;j++)
if( backgrounds[i][j] == colorRefBackground )
count=count+1;
return count;
};
/**
*使用计数彩色单元格(“A1:B3”、“C5”)
*@param{range}countRange要计算的范围
*@param{colorRef}colorRef单元格,背景色要在countRange中搜索
*@return{number}
*@customfunction
*/
函数countColoredCells(countRange,colorRef){
var activeRange=SpreadsheetApp.getActiveRange();
var activeSheet=activeRange.getSheet();
var formula=activeRange.getFormula();
var backgrounds=activeSheet.getRange(countRange.getBackgrounds();
var colorRefBackground=activeSheet.getRange(colorRef.getBackground();
var计数=0;
对于(var i=0;i从应用程序脚本>视图>执行日志中发送错误消息,无法调用null pop方法i更改代码,请参阅“更改代码时出错”是的,但这显然不是他的目的。@haemse我不明白你的意思。如果目标是使用不带引号的公式,那么正如答案中所说,你只需在match()
中更改正则表达式即可。
var countRangeAddress = countRange
var backGrounds = activeSht.getRange(countRangeAddress).getBackgrounds();
var colorRefAddress = colorRef
=countcoloredcells("B1:B9";"A1")
/**
* Usage countColoredCells("A1:B3";"C5")
* @param {range} countRange Range to be evaluated
* @param {colorRef} colorRef Cell with background color to be searched for in countRange
* @return {number}
* @customfunction
*/
function countColoredCells(countRange,colorRef) {
var activeRange = SpreadsheetApp.getActiveRange();
var activeSheet = activeRange.getSheet();
var formula = activeRange.getFormula();
var backgrounds = activeSheet.getRange(countRange).getBackgrounds();
var colorRefBackground = activeSheet.getRange(colorRef).getBackground();
var count = 0;
for(var i=0;i<backgrounds.length;i++)
for(var j=0;j<backgrounds[0].length;j++)
if( backgrounds[i][j] == colorRefBackground )
count=count+1;
return count;
};