Thursday, November 12, 2015

Google Spreadsheet Count of colored cells in a range in Custom Function

Count of colored cells in a range in Custom Function



I have the following formula in cell E9:
=countColoredCells(A1:C6,E1)

and the following formula in cell F9:
=countColoredCells(A1:C6,F1)

countColoredCells is a custom function created with the help of the following script code, in this function you need to pass the range in first parameter in which you want to count the colored cells, and also you need to pass the cell in the second parameter which have the background that you need to count in the range (provided in first parameter).

Put the following script code in the script editor of your spreadsheet:

/////////////////////////////////////////////////

/**
* @param {range} countRange Range to be evaluated
* @param {range} 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 rangeA1Notation = formula.match(/\((.*)\,/).pop();
  var range = activeSheet.getRange(rangeA1Notation);
  var bg = range.getBackgrounds();
  var values = range.getValues();
  
  var colorCellA1Notation = formula.match(/\,(.*)\)/).pop();
  var colorCell = activeSheet.getRange(colorCellA1Notation);
  var color = colorCell.getBackground();
  
  var count = 0;
  
  for(var i=0;i<bg.length;i++)
    for(var j=0;j<bg[0].length;j++)
      if( bg[i][j] == color )
        count=count+1;
  return count;
};

/////////////////////////////////////////////////

NOTE:


When you change the color of any cell the value will not get auto updated in the result,
so you need to temporary change the value of any of one of the cell in the range (and then change it back as you want..)

this will update the color counting result...

For getting sum instead of count of cells having a background color, look at the following link:
http://igoogledrive.blogspot.com/2015/11/google-spreadsheet-sum-of-colored-cells.html

Wednesday, November 11, 2015

Google Spreadsheet Sum of a colored cells in a range in Custom Function

Sum of a colored cells in a range in Custom Function



I have the following formula in cell E9:

=sumColoredCells(A1:C6,E1)

and the following formula in cell F9:

=sumColoredCells(A1:C6,F1)

sumColoredCells is a custom function created with the help of the following script code, in this function you need to pass the range in first parameter in which you want to sum the values of the colored cells, and also you need to pass the cell in the second parameter which have the background that you need to sum in the range (provided in first parameter).

Put the following is the script code in the script editor of your spreadsheet:


////////////////////////////////////////////////////


/**

* @param {range} sumRange Range to be evaluated
* @param {range} colorRef Cell with background color to be searched for in sumRange
* @return {number}
* @customfunction
*/

function sumColoredCells(sumRange,colorRef) {

  var activeRange = SpreadsheetApp.getActiveRange();
  var activeSheet = activeRange.getSheet();
  var formula = activeRange.getFormula().toString();
  formula = formula.replace(new RegExp(';','g'),',');
  
  
  var rangeA1Notation = formula.match(/\((.*)\,/).pop();
  var range = activeSheet.getRange(rangeA1Notation);
  var bg = range.getBackgrounds();
  var values = range.getValues();
  
  var colorCellA1Notation = formula.match(/\,(.*)\)/).pop();
  var colorCell = activeSheet.getRange(colorCellA1Notation);
  var color = colorCell.getBackground();
  
  var total = 0;
  
  for(var i=0;i<bg.length;i++)
    for(var j=0;j<bg[0].length;j++)
      if( bg[i][j] == color )
        total=total+(values[i][j]*1);
  return total;
};

////////////////////////////////////////////////////

NOTE:


When you change the color of any cell the value will not get auto updated in the sum result,
so you need to temporary change the value of any of one of the cell in the range (and then change it back as you want..)


this will update the colored cells sum result...

For getting count instead of sum of values of cells having a background color, look at the following link:
http://igoogledrive.blogspot.com/2015/11/google-spreadsheet-count-of-colored.html

Saturday, November 7, 2015

Google Spreadsheet Getting the last 5 values before the previous last 5

Question:

Hi, I am trying to get the average values in a row present at the last 5 rows before the last 5 rows, in other words in a sheet with 15 rows in a column I would like to know how to attain the average value of rows 5 to 10.

Solution:

Following is the screenshot of the sheet having data in column A:


Now I have the following formula in cell C1:
=arrayformula({A:A,row(A:A)})
to get the same values in column C and the row number in column D


Now I have the following formula in cell C1:
=query(arrayformula({A:A,row(A:A)}),"select Col1,Col2 where Col1 is not null",0)
to get the both columns having equal number of rows (that is rows having values in Column C)


Now I have the following formula in cell C1:
=query(arrayformula({A:A,row(A:A)}),"select Col1,Col2 where Col1 is not null order by Col2 desc",0)
to get the data in descending order of row number, as we want second last 5 rows...


Now I have the following formula in cell C1:
=query(arrayformula({A:A,row(A:A)}),"select Col1,Col2 where Col1 is not null order by Col2 desc offset 5",0)
to start the data from second last 5 rows, I have used "offset" in query formula.


Now I have the following formula in cell C1:
=query(arrayformula({A:A,row(A:A)}),"select Col1,Col2 where Col1 is not null order by Col2 desc limit 5 offset 5",0)
to limit the data upto only 5 rows.


Now I have the following formula in cell C1:
=query(arrayformula({A:A,row(A:A)}),"select Col1 where Col1 is not null order by Col2 desc limit 5 offset 5",0)
to get the data in only column C of second last 5 rows.


Now I have the following formula in cell C1:
=average(iferror(query(arrayformula({A:A,row(A:A)}),"select Col1 where Col1 is not null order by Col2 desc limit 5 offset 5",0)))
to finally get the average of second last 5 rows.



Wednesday, November 4, 2015

Google Spreadsheet Script for manipulating JSON

The following URL "http://shanghai.anjuke.com/ajax/pricetrend/comm?cid=10052", has the JSON data:

Now to fetch this data in Google Spreadsheet, use the following script:

/////////////////////////////////////////////////

function extract(url){
  var response = UrlFetchApp.fetch(url);
  var data = JSON.parse(response.getContentText());
  var comm = data.comm;
  
  var keyVal = function(o) {
    var key = Object.keys(o)[0];
    return [parseInt(key),parseInt(o[key])];
  } ;
  
  var result = new Array();
  var count=0;
  for (i in comm){
    result[count] = new Array();
    result[count] = keyVal(comm[count]);
    count++;
  }

  return result;
};

/////////////////////////////////////////////////

save the above script code in the script editor of your Spreadsheet and then you can call the custom function "extract" directly from your spreadsheet.

Have a look at the following screenshot:


I have the URL in cell A1, and the following formula in cell B1:
=transpose(extract(A1))