How do I use momentsjs in Google Apps Script?

前端 未结 5 2310
时光说笑
时光说笑 2020-12-15 18:09

I\'m trying to utilize the momentjs library in Google Apps Script but I\'m not clear on how to do so. I\'m not sure how to add the library, so obviously running something l

5条回答
  •  隐瞒了意图╮
    2020-12-15 18:57

    You can add moment and moment.tz to app scripts by creating a new Script file and adding the following code:

    var cacheExpire = 3600;
    var momentCache = "momentCache";
    var momentUrl = "https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"
    
    var momentTzCache = "momentTzCache";
    var momentTzUrl = "https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.16/moment-timezone-with-data-2012-2022.min.js"
    
    useCachedOrLive(momentCache,momentUrl);
    useCachedOrLive(momentTzCache,momentTzUrl);
    
    function useCachedOrLive(cacheToCheck, url){
    
      var cache = CacheService.getUserCache();
      var cachedData = cache.get(cacheToCheck);
      console.log(cacheToCheck);
      if(cachedData !== null){
        console.log("using cached " + cacheToCheck)
        eval(cachedData);
      }
      else
      {
        console.log("getting live " + cacheToCheck);
        var response = UrlFetchApp.fetch(url).getContentText();
        cache.put(cacheToCheck, response, cacheExpire);
        eval(response);
    
      }
    }
    

    This uses the cache service to reduce round trip calls and you can modify it to include a subset of data if you want.

    Thanks to apadana for getting me started!

提交回复
热议问题