Google Product Forums

Object.prototype causing error


cjg500 May 5, 2012 4:35 AM
Posted in group: Google Apps Script
Hi all,
 
This may not be the correct forum for this as it is probably more specifically a javascript issue than a Google Apps Script one, but I would be grateful if
anyone could enlighten me.
 
I have been trying to integrate a suggestion box into a Google Apps Script UI app. I got the code for this from here (courtesy of the excellent James Ferreira) and it works
just fine and dandy in a script on its own. However, when I try to import it in to the UI script I am building I get the following error when I try to run the site:
 
Syntax error: Invalid argument: function (key) { var searchMatch = []; for (j = 0; j < this.length; j++) { if (this[j][0].toString().toLowerCase().search(key.toString().toLowerCase()) != -1) { searchMatch.push([this[j][0], this[j][1]]); } } return searchMatch; } line: ? (line 7)
 
i.e. it doesn't like the function declaration. I am guessing it is something to do with the prototyping, but not being a javascript guru am struggling to identify
precisely what the problem is.
 
This is the full function declaration:
 
Object.prototype.findCells = function(key) {  
  var searchMatch = [];
    for (j = 0; j < this.length; j++){
      if (this[j][0].toString().toLowerCase().search(key.toString().toLowerCase()) != -1){
      //if (this[j][5].toString().toLowerCase().substr(0, key.length).search(key.toString().toLowerCase()) != -1){
        searchMatch.push([this[j][0],this[j][1]]);
      }
    }
  return searchMatch;
}
 
Thanks in advance
Chris