how to create custom functions in class range

2019-08-03 16:53发布

问题:

I want to create a custom function i.e. myrange.ckeck_if_included(row,col,numrow,numcol) that returns a boolean that says me if the range given in input is included into myrange. Is it possible to create this function in class range? And how to refer my function to myrange? Thanks franko

回答1:

You can always use a proxy object to do this. Here's a general proxy-making utility:

function proxy(obj) {
  var x = {};
  Object.keys(obj).map(function(k) { x[k] =
      function() { return obj[k].apply(obj, [].slice.call(arguments, 0)); }})
  return x;
}

In your case you could then do

r=proxy(SpreadsheetApp.openById("...").getSheetByName("Sheet1").getActiveRange());
r.myFunction=function () { ... }

And more generally you can even replace top-level objects with proxies:

GmailApp = proxy(GmailApp);
GmailApp.myFunction = ...


回答2:

So you want to extend an Object/Class of the Google-Services? This does not work, you get an exception "Object does not allow properties to be added or changed" if you try like this:

r=SpreadsheetApp.openById("...").getSheetByName("Sheet1").getActiveRange();
r.myFunction=function () { ... }