0

I am facing a problem in the following script. I am not much into scripting at all and this is not my script also but here am getting the result which is grouping the values( For Example if i have a value A in three cells it should return the value as 3 instead it is returning AAA. Can someone help me out to count the values and return it

Thanks in Advance,

Here is the script :

function sumBackgroundColors(rangeString, color) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var sumRange = s.getRange(rangeString);
//var sum = 0;
var openCount = 0;
var sumRangeBackground = sumRange.getBackgroundColors();
var sumRangeValues = sumRange.getValues();

for(var row = 0; row < sumRangeBackground.length; row++ ) {
for(var col = 0; col < sumRangeBackground[0].length; col++ ) {
if( sumRangeValues[row][col]=="LG M"&& sumRangeBackground[row][col] == color ) {
openCount = openCount + sumRangeValues[row][col];
//if(sumRangeBackground[row][col] == color && sumRangeValues[row][col] == 1 ) {
// sum = sum + parseFloat(sumRangeValues[row][col]);

} 
}
}
return openCount;
//return sum;
}
4

2 回答 2

0

你的问题可能是由于openCount = openCount + sumRangeValues[row][col];

根据您的示例sumRangeValues[row][col]不是整数。int + 不是 int = ??? 如果你想记录你可能希望openCount++替换该行的东西,这只是一个快捷方式openCount = openCount + 1;

即使sumRangeValues[row][col] 一个 int,那条线仍然不是你要找的。如果您在电子表格中搜索所有 3,您的代码将找到您的前 3 个,然后该行将执行0 = 0 + 3恭喜,您刚刚找到 3 个 3。每次找到三时,您都会继续添加三。

Waqar 的代码本质上是您的代码(非常简化,但迭代方式相同),只是他不检查颜色并且使用 ++ 而不是该行。

于 2012-05-30T17:01:11.430 回答
0

这是一个将要搜索的值作为参数的函数。其余内容已在注释行中进行了解释。

function searchCount(value){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  //Get the whole data from activesheet in a 2D array
  var data = sheet.getDataRange().getValues();

  //Initialize counter
  var count = 0;

  //Iterate through the array
  for(var i in data){
    for(var j in data[i]){
      // if a match found, increament the counter
      if(value.toString == data[i][j]){
        count++;
      }
    }
  }
  // return the count value
  return count;
}
于 2012-05-14T11:07:30.403 回答