EasyCron Library

The easyCron service is a cloud based scheduler for recurrent jobs. Google Apps Script has it's own scheduler for triggering tasks, but you may prefer the management capabilities with easyCron. You have to sign up, but there is a free plan too. 

The library

You'll find it here  if you want to fork it, or you can include this resource MjbiRDgq07ZNFaXn0OEKX-yz3TLx7pV4j

The API

EasyCron has a fairly straightforward API. This library provides a simple interface to it from GAS. I personally don't use it, but following a few posts on the subject in the Google Apps Script community I thought I may as well create this for others to use. Please provide feedback via Google Apps Script community if you find it useful or otherwise. 

Example

Before you start you should store your API key in your property service, as the example below. Here is how to schedule running a GAS webapp everyday at 12:30. It uses cron expressions which you can read about here.
function myFunction() {
  // some gas webapp
  var sc = "https://script.google.com/macros/s/AKfycbyNxrJg2SbjoKWJQgGxqjECkcA-A57xaoRQWzsJkTPbVyTWbCDi/exec";
  
  // get my apiKey, which Im storing in my userproperties
  var easyCron = new EasyCron(JSON.parse(PropertiesService.getUserProperties().getProperty('easyCronKeys')).restAPIKey);
  
  // schedule it at 12:30 every day (see https://www.easycron.com/faq/What-cron-expression-does-easycron-support)
  var result = easyCron.add("myGASWebapp",sc,"30 12 * * *");
  if(!result.ok) {
    throw (JSON.stringify(result));
  }
  
}

and here's what it looks like on the easyCron console

Here's some more methods available in the library

function test () {

// some other things
  var easyCron = new EasyCron(JSON.parse(PropertiesService.getUserProperties().getProperty('easyCronKeys')).restAPIKey);

  // add something
  var id = easyCron.add("testcron","https://www.easycron.com/rest/list?token=4cf0ff7a668a141d5a81e2b6242952dc&sortby=name&page=1","0 0 * * 3").id;

  Logger.log(easyCron.enable(id));
  
    
  // list all current cron jobs
  var jobs = easyCron.listAll();

  jobs.forEach (function(d) {
    Logger.log(easyCron.disable(d[0].cron_job_id));
  });
  Logger.log(easyCron.disable(id));
  

 // remove it
  Logger.log(easyCron.remove(id));
  
}


Code from library


For help and more information join our forum,follow the blog or follow me on twitter . For more stuff like this see Google Apps Scripts snippets

You want to learn Google Apps Script?

Learning Apps Script, (and transitioning from VBA) are covered comprehensively in my my book, Going Gas - from VBA to Apps script, available All formats are available now from O'Reilly,Amazon and all good bookshops. You can also read a preview on O'Reilly

If you prefer Video style learning I also have two courses available. also published by O'Reilly.
Google Apps Script for Developers and Google Apps Script for Beginners.




Comments