Doodle for your domain using Google Apps Script

I was looking on this post on Google Apps Developer blog, I thought, why not to develop the same functionality using Google Apps Script which will be easy to maintain and run. 

So I made a Spreadsheet Application which will does the same for you.

In this application, you just need to write the URL of the doodle and Date for that Doodle logo.

Services used in this application:

Here is the step by step instructions to use this Spreadsheet Application Doodle for your domain.

Note: This application can be used by Google Apps Administrator only.

Now your Doodle is setup. Once a doodle has been used, it will be archived in the second spreadsheet.

Code:

  ///////////////////////////////////////////////////////////////////////

 //Setup a time driven trigger (Day Timer) to run this function/////////

///////////////////////////////////////////////////////////////////////

function getTodaysDoodle(){

  try{

    var ss = SpreadsheetApp.getActiveSpreadsheet();

    var doodleSheet = ss.getSheets()[0];

    var timeZone = Session.getTimeZone();

    var dataRange = doodleSheet.getRange(2, 1, doodleSheet.getLastRow()-1, 3);

    var data = dataRange.getValues();

    var today = Utilities.formatDate(new Date(), timeZone, 'MM/dd/yyyy');

    var logo = {};

    var doodleRow = 2;

    for(var i=0; i<data.length; i++){

      var date = Utilities.formatDate(data[i][1], timeZone, 'MM/dd/yyyy');

      //if doodle exists, then get that doodle name and url

      if(date == today){

        logo.name = data[i][0];

        logo.url = data[i][2];

        doodleRow = i+2;

        dataRange.clearFormat();

        break;

      }

    }

    //If no doodle exists, get the domain default logo

    if(logo.name == null){

      logo.name = data[0][0];

      logo.url = data[0][2];

      dataRange.clearFormat();

      doodleSheet.getRange(2, 1, 1,3).setBackground('gold');

    }

    updateDoodle(logo);

    //Archive the doodle in second sheet if it is not the default Logo

    if (doodleRow !=2){

      var archiveSheet = ss.getSheets()[1];

      doodleSheet.getRange(doodleRow, 1, 1,4)

.moveTo(archiveSheet.getRange(archiveSheet.getLastRow()+1, 1, 1));

      doodleSheet.deleteRow(doodleRow);

    }

  }

  catch(e){

    //If there occurs any error, Mail it to yourself

    GmailApp.sendEmail(Session.getEffectiveUser(), 'Error occured while updating' 

+'the Logo', 'Error'+e);

  }

}

//This function will update the logo, called from ' getTodaysDoodle()'

function updateDoodle(logo){

  var logoBData = UrlFetchApp.fetch(logo.url).getContent();

  var logoB64encodedData = Utilities.base64Encode(logoBData);

  var xmlRaw = "<atom:entry xmlns:atom='http://www.w3.org/2005/Atom'" +"xmlns:apps='http://schemas.google.com/apps/2006'>"

      +"<apps:property name='logoImage' value='"+logoB64encodedData+"' />"

      +"</atom:entry>";

  var base = 'https://apps-apis.google.com/a/feeds/domain/';

  var fetchArgs = googleOAuth_("myDoodle", base);

  fetchArgs.method = 'PUT';

  fetchArgs.payload = xmlRaw;

  fetchArgs.contentType = 'application/atom+xml';

  var myDomain = UserManager.getDomain();

  var url = base+'2.0/'+myDomain+'/appearance/customLogo';

  var urlFetch = UrlFetchApp.fetch(url, fetchArgs); 

  var status = urlFetch.getResponseCode();

  

}

//Google oAuthConfig.. Called from 'updateDoodle(logo)'

function googleOAuth_(name,scope) {

  var oAuthConfig = UrlFetchApp.addOAuthService(name);

  oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);

  oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");

  oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");

  oAuthConfig.setConsumerKey("anonymous");

  oAuthConfig.setConsumerSecret("anonymous");

  return {oAuthServiceName:name, oAuthUseToken:"always"};

}

If you see any bug in this application or would like to give any feedback/comment, please drop your comment through this form.

Search Keywords: Doodle for your domain, Custom Logo for Google Apps, Doodle using GAS, Doodle using Google Apps Script, Google Apps Script tutorial Doodle, Doodle for your Google Apps Domain