Feature request: Extend GAS classes

93 views
Skip to first unread message

Ben Ronkin

unread,
Dec 6, 2019, 12:59:52 AM12/6/19
to Google Apps Script Community
I know that this is currently not possible, but it would be great if GAS users could extend classes. I,  for one, would love to add my own utility functions to the File Class, to make it easier to move files across folders, etc.

Thank you for your consideration!

Dimu Designs

unread,
Dec 6, 2019, 7:08:39 AM12/6/19
to google-apps-sc...@googlegroups.com
That's unlikely to happen. However, you're not without options. You can leverage either the Adapter or Decorator patterns to wrap the interface of the File class and add your own functionality where needed.

Linked below is an article on Medium that covers the basics on the aforementioned patterns as well as others:

Javascript Design Patterns

Also, these forums are run by members of the Apps Script Community (not necessarily affiliated with Google). So if you want to make a feature request that will stand a chance of getting on Google's radar you need to use Google's issue tracker for Apps Script.

jiit mahe2

unread,
Dec 6, 2019, 7:22:09 AM12/6/19
to google-apps-sc...@googlegroups.com
Hi ,
    I am new to Gmail Add-ons, facing the timeout issue UrlFetchApp.fetch() method. Cross the time limit call particular API in above 60s at this time doesn't handle the exceptions(Attchment), also using the try/catch but doesn't handle.How is it over come this?
   Sorry for my bad English.

On Fri, Dec 6, 2019 at 5:38 PM Dimu Designs <dimud...@gmail.com> wrote:
That's unlikely to happen. However, you're not without options. You can leverage either the Adapter or Decorator pattern to wrap the functionality of the Files class and add your own functionality as needed .

Linked below is an article on Medium that covers the basics on those patterns and others:

Javascript Design Patterns

Also, these forums are run by members in the Apps Script Community (not necessarily affiliated with Google). So if you want to make a feature request that will get on Google's radar you need to use Google's issue tracker.

On Friday, December 6, 2019 at 12:59:52 AM UTC-5, Ben Ronkin wrote:
I know that this is currently not possible, but it would be great if GAS users could extend classes. I,  for one, would love to add my own utility functions to the File Class, to make it easier to move files across folders, etc.

Thank you for your consideration!

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/af21ca71-904b-4843-b9ab-2dc324f06164%40googlegroups.com.

Bruce Mcpherson

unread,
Dec 9, 2019, 4:54:01 AM12/9/19
to google-apps-sc...@googlegroups.com
Extending prototypes is always a danger in any case - for example Google may introduce a method that conflicts with one of yours. Similar effects can be achieved by simply creating a wrapper for the underlying class and using your wrapper instead of the underlying class. For example, this small sample adds zipping/unzipping, object and key hashing to  the CacheService class

function myCache (type, sec) {

  /**
  * @param {[*]} arguments unspecified number and type of args
  * @return {string} a digest of the arguments to use as a key
  */
  function keyDigest () {
    // convert args to an array and digest them
    return  Utilities.base64EncodeWebSafe (
      Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_1,Array.prototype.slice.call(arguments).map(function (d) {
        return (Object(d) === d) ? JSON.stringify(d) : d.toString();
      }).join("-"),Utilities.Charset.UTF_8));
  };

  /**
  * crush for writing to cache.props
  * @param {string} crushThis the string to crush
  * @return {string} the b64 zipped version
  */
 function crush (crushThis) {
    return Utilities.base64Encode(Utilities.zip ([Utilities.newBlob(JSON.stringify(crushThis))]).getBytes());
  };
 
  /**
  * uncrush for writing to cache.props
  * @param {string} crushed the crushed string
  * @return {string} the uncrushed string
  */
  function uncrush (crushed) {
    return Utilities.unzip(Utilities.newBlob(Utilities.base64Decode(crushed),'application/zip'))[0].getDataAsString();
  };
 
  // export these
  this.sec = sec || 5000;
  this.cache =
     (type === 'document' && CacheService.getDocumentCache()) ||
     (type === 'script' && CacheService.getScriptCache()) ||
     (type === 'user' && CacheService.getUserCache())
     
     if(!this.cache) throw new Error('invalid cache type:'+type);

  this.write = function (data,keys) {
    const args = Array.prototype.slice.call (arguments,1);
    this.cache.put(keyDigest.apply(args) , crush(data))
  }
  this.read = function (keys) {
    const args = Array.prototype.slice.call (arguments);
    const crushed = this.cache.get(keyDigest.apply(args));
    return (crushed && JSON.parse(uncrush(crushed))) || null;
  }
}

// examples
function mytest () {
  const script = new myCache ('script');
  script.write ({a:'x',b:1,c:23}, 'x',1);
  const user = new myCache ('user');
  user.write ({a:'y',b:100,name:'john'},{user:'smith'});
 
  Logger.log(user.read({user:'smith'}));
  Logger.log(script.read('x',1));

}

On Fri, 6 Dec 2019 at 05:59, Ben Ronkin <ronk...@gmail.com> wrote:
I know that this is currently not possible, but it would be great if GAS users could extend classes. I,  for one, would love to add my own utility functions to the File Class, to make it easier to move files across folders, etc.

Thank you for your consideration!

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages