Thursday, September 5, 2013

Google Spreadsheet Script to check if one cell is similar to the other

Question:

( by Alexander Paul )


Hey I am new to scripting for Google Spreadsheet. I want to know how to add a function to a script, that checks, if e.g. Cell B4 is similar to B3 or B2.

I don't expect full code (except someone already did it and wants to share).

Many thanks in advance.
(I don't know why this should be necessary, but I am using WIN7 with Chrome)

Solution:

You can check the values of the cells either by hard coding the cell reference in the script or by passing the values in the functions parameter.

Have a look at the following screenshot:



I have the following formula in Cell C3:
=check()

I have the following formula in Cell C5:
=compare(B5,B4)

Have a look at the following code:

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

// This script has been developed by Kishan
// For more visit: iGoogleDrive.blogspot.com

function check() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getSheetByName("Sheet1");
  var cell_b3 = s.getRange("B3").getValue();
  var cell_b4 = s.getRange("B4").getValue();
  
  if( cell_b3 == cell_b4 ) return "B3 is equal to B4";
  else return "B3 is not equal to B4";
};

function compare(var1,var2) {
  if( var1 == var2 ) return "Both are equal";
  else return "Both are not equal";

};

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

put the above code in your script editor, and then you can use it directly in your Google Spreadsheet.



And If you are not much familiar with scripts then check out the following link:http://igoogledrive.blogspot.com/2012/08/how-to-write-script-in-google.html 

I hope the above solution will help you, and if you need more help then please do comment below on this blog itself, I will try to help you out.


I also take up private and confidential projects:
If this blog post was helpful to you, and if you think you want to help me too and make my this blog survive then please donate here: http://igoogledrive.blogspot.com/2012/09/donate.html 

Thanks,

No comments:

Post a Comment