Question

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 like the following results in "Reference Error: 'moment' is not defined":

var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
var difference = a.diff(b);
Était-ce utile?

La solution 5

Using external Javascript library is not so easy... Depending on the context in which you want to use it (a webapp of a document embedded script) the approach will be different.

I didn't try it in client JavaScript and I'm not sure caja will allow it but I found this post that shows a possible way to include it using a Google Script Library that a user has build and if I read the post it seems to work...

The "user" is a Google developper so he knows for sure what he is talking about ;) please update here if it worked for you.

Autres conseils

Most people try to use the library with the key ending in 48. That library is pretty dated (it is version 2.9 which is pretty old).

Using eval and UrlFetchApp.fetch moment.js or any other external library can be used easily in google app scripts.

function testMoment() {
  eval(UrlFetchApp.fetch('https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js').getContentText());
  var date = moment().format("MMM Do YY");
  Logger.log(date)
}

You may either host the moment.js on your own server, or use a CDN like cloudflare CDN to reference the library.

For cloudflare, here is the page which shows moment.js versions and their urls:

https://cdnjs.com/libraries/moment.js/


As of writing this post 2.18.1 is the latest version.

For the example posted by OP it will look like this:

function testMomentDifference() {
  eval(UrlFetchApp.fetch('https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js').getContentText());
  var a = moment([2007, 0, 29]);
  var b = moment([2007, 0, 28]);
  var difference = a.diff(b);
  Logger.log(difference);
}

The moment script ID for the Google Apps Script IDE has changed. It is now "15hgNOjKHUG4UtyZl9clqBbl23sDvWMS8pfDJOyIapZk5RBqwL3i-rlCo"

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!

There is a better and best way to use moment

Do not use UrlFetchApp, to avoid quota exceeded, caching, and server issues

Download moment.min.js and momemt-timzone.min.js in last versions

and integrate the full files in apps script like the below screen

Moment files in apps script directly

Moment source code

There is no problems in long run with this approach, just update the files any time when you need.

After adding the two files, just publish a new version and include it in any other script

For example:

  1. I will create a script with name "MomentAPI" and include the two files mentioned, and publish a new version.
  2. in other script with name "myScript" I will include the library "MomentAPI" with its script id as known

then will use it like the below examples

const moment = MomentAPI.moment; // init the library
  
const start = moment().startOf('day').toDate(); // Dec 06 00:00:00
const end = moment().endOf('day').toDate(); // Dec 06 23:59:59
const d = moment(1767139200000).tz('America/New_York').format('ha'); // 7am EST

You can use Moment.js library in Google Apps Script by following these steps:

  1. Open the Google Apps Script editor by going to "Tools" > "Script editor" in your Google Sheets/Docs/Slides.

  2. In the script editor, click on "Resources" > "Libraries".

  3. In the "Add a Library" field, enter the following script ID for Moment.js: "15hgNOjKHUG4UtyZl9clqBbl23sDvWMS8pfDJOyIapZk5RBqwL3i-rlCo"

  4. Select the latest version of Moment.js from the "Version" dropdown.

  5. Click "Add" to add the Moment.js library to your script.

  6. You can now use the Moment.js functions in your code. For example, to get the current date and time, you can use the following code:

var now = Moment.moment();
Logger.log(now.format('MMMM Do YYYY, h:mm:ss a'));
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top