当前位置: 动力学知识库 > 问答 > 编程问答 >

google apps script - how to create custom functions in class range

问题描述:

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

网友答案:

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 = ...
网友答案:

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 () { ... }
分享给朋友:
您可能感兴趣的文章:
随机阅读: