A Calendar subscriber app

An interesting utility for people or organisations with a lot of shared calendars that helps users to subscribe and unsubscribe to these shared calendars.

The spreadsheet itself is easy to create, you only have to keep 2 columns like this :



Column A will hold the Calendar names, column B will hold the IDs ( there is an utility in the script to get all the names & IDs of the calendars you have subscribed to).

 And the app  with a couple of example is online here to test it  (you will have to authorize the script)



Let's start with some global variables with the sheet ID, a reminder of the webapp url and the last row number and user email definition.

//Link to webapp : https://script.google.com/macros/s/AKfycbwdkugkdK5_DQLUlCKPvhyOB984PYwurLwXbYc95KATjiETDFTB/exec
//
//
//  Global variabless
//----------------------------------------------------------------
  var key="spreadsheet ID key"
  var ss = SpreadsheetApp.openById(key);
  SpreadsheetApp.setActiveSpreadsheet(ss);
  ss.setActiveSheet(ss.getSheets()[0]);
  var sh = SpreadsheetApp.getActiveSheet();
  var last =  ss.setActiveSheet(ss.getSheets()[0]).getLastRow();
  var email = String(Session.getUser().getUserLoginId());
 

The Ui is simple and sober, I used a grid to position the widgets and a client handler to show a small message when a time consuming operation is called.
I used some Style for colors and padding...

function doGet(){  
  var app = UiApp.createApplication().setHeight(400).setWidth(650).setStyleAttribute('background', 'beige')
  .setStyleAttribute('margin-top', '10px').setStyleAttribute('margin-left', '10px').setStyleAttribute('padding', '5').setTitle("Calendar (Un)Subscribe Utility");
  var panel = app.createVerticalPanel()
  var grid = app.createGrid(6, 2).setWidth('550').setId('grid');
  var Slist= app.createListBox(true).setName("slb").setId("slb").setVisibleItemCount(16).setWidth("180");
  var Ulist= app.createListBox(true).setName("ulb").setId("ulb").setVisibleItemCount(16).setWidth("195");
  var subs = app.createButton("Subscribe to selected calendars").setWidth("180");
  var unsubs = app.createButton("Unsubscribe from selected calendars").setWidth("195");  
  var quit = app.createButton("Quit (and delete personal data from this application)");  
  var avert = app.createLabel("Your personal calendars and any calendar that is not included in the list are not shown in this list",true).setWidth('250');
  var comment = app.createLabel("You can select one or more calendars in both lists",true).setWidth('250');
  var wait = app.createLabel("****  PLEASE WAIT  ****",true).setWidth('250').setStyleAttribute('borderWidth', '2').setStyleAttribute('background','yellow').setId('wait').setVisible(false);
  var ok = app.createLabel("Your subscription to the selected calendars has been updated",true).setWidth('250').setVisible(false).setId('ok');
  var log = app.createLabel("You are connected with the email adress : "+email,true).setWidth('250').setStyleAttribute('background','white').setStyleAttribute('padding', '5');
  var end = app.createHTML("<BR><BR>Thanks for using this application,<BR><BR>You may close this window ...<BR><BR>© serge insas 2012").setId('end').setVisible(false) ;  
  var anchor = app.createAnchor('Link to your Calendars', 'https://www.google.com/calendar/render?tab=oc');
  grid.setWidget(0, 0, Slist)
  grid.setWidget(0, 1, Ulist)
  grid.setWidget(1, 0, subs)
  grid.setWidget(1, 1, unsubs)
  grid.setWidget(2, 1, avert)
  grid.setWidget(2, 0, comment)
  grid.setWidget(3, 0, wait)
  grid.setWidget(4, 0, log)
  grid.setWidget(5, 0, quit)  
  grid.setWidget(4, 1, anchor)    
  grid.setWidget(5, 1, ok)  
  panel.add(grid).add(end)
  app.add(panel)  
  var list = sh.getRange(1,1,last,2).getValues(); 
    for(cc=0;cc<list.length;++cc){
      Slist.addItem(list[cc][0]);
      }
  var Slistvalid = new Array();
  var insaslist = list.join()
  var Slist = CalendarApp.getAllCalendars();  
    for(cc=0;cc<Slist.length;++cc){
//      Logger.log(Slist[cc].getName())
// create a list of valid calendars(that are in the list on the spreadsheet)
      if(insaslist.match(Slist[cc].getName())==Slist[cc].getName()){Slistvalid.push(Slist[cc].getName())} 
    }
//Logger.log(Slistvalid)
  for(cc=0;cc<Slistvalid.length;++cc){
        Ulist.addItem(Slistvalid[cc]);
      }
  var handler = app.createServerHandler("calsub")
      handler.addCallbackElement(grid)
      subs.addClickHandler(handler)
  var handler = app.createServerHandler("calunsub")
      handler.addCallbackElement(grid)
      unsubs.addClickHandler(handler)
  var handler = app.createServerHandler("quitte")
      handler.addCallbackElement(grid)
      quit.addClickHandler(handler)
  var Chandler = app.createClientHandler()
      .forTargets(wait).setVisible(true)
    subs.addClickHandler(Chandler)
  var Chandler = app.createClientHandler()
      .forTargets(wait).setVisible(true)
    unsubs.addClickHandler(Chandler)
    
    
return app
}


The calsub and calunsub are very similar, they take care of the real action of this application and handle the messages too 


function calsub(e){ ;
var app = UiApp.getActiveApplication();
if(e.parameter.slb ==''){return}
var Ulist = app.getElementById('ulb')
var calnamesub = e.parameter.slb.split(',');
var calname = sh.getRange(1,1,last,2).getValues();
var calsubID = new Array();
for (n=0;n<calnamesub.length;++n){
Logger.log(calnamesub[n]);
for (cc=0;cc<calname.length;++cc){
if(calname[cc][0]==calnamesub[n]){
calsubID.push(calname[cc][1].toString());
}
}
}
Logger.log(calsubID)
try{
for(nn=0;nn<calsubID.length;++nn){
CalendarApp.subscribeToCalendar(calsubID[nn])
}
}
catch(err){
Logger.log(err)
app.getElementById('ok').setValue(" Error while accessing Google Calendar Service, please try again /"+err)
}
var USlistvalid = new Array();
var insaslist = calname.join()
var USlist = CalendarApp.getAllCalendars();
Ulist.clear();// clear list before additems
for(cc=0;cc<USlist.length;++cc){
if(insaslist.match(USlist[cc].getName())==USlist[cc].getName()){USlistvalid.push(USlist[cc].getName())}
}
for(cc=0;cc<USlistvalid.length;++cc){
Ulist.addItem(USlistvalid[cc]);
}
app.getElementById('ok').setVisible(true)
app.getElementById('wait').setVisible(false)
return app
}
//
//
function quitte(){
var app = UiApp.getActiveApplication();
var grid = app.getElementById('grid').setVisible(false);
var end = app.getElementById('end').setVisible(true);
return app
}
//
//
function calunsub(e){ ;
var app = UiApp.getActiveApplication();
if(e.parameter.ulb ==''){return}
var Ulist = app.getElementById('ulb')
var calnameunsub = e.parameter.ulb.split(',');
var calname = sh.getRange(1,1,last,2).getValues();
var calunsubID = new Array();
for (n=0;n<calnameunsub.length;++n){
Logger.log(calnameunsub[n]);
for (cc=0;cc<calname.length;++cc){
if(calname[cc][0]==calnameunsub[n]){
calunsubID.push(calname[cc][1].toString());
}
}
}
Ulist.clear();
try{
for(nn=0;nn<calunsubID.length;++nn){
CalendarApp.getCalendarById(calunsubID[nn]).unsubscribeFromCalendar()
Logger.log(calunsubID[nn])
}
}
catch(err){
Logger.log(err)
app.getElementById('ok').setValue(" Error while accessing Google Calendar Service, please try again /"+err)
}
app.getElementById('ok').setVisible(true)
app.getElementById('wait').setVisible(false)
var USlistvalid = new Array();
var insaslist = calname.join()
var USlist = CalendarApp.getAllCalendars();
for(cc=0;cc<USlist.length;++cc){
if(insaslist.match(USlist[cc].getName())==USlist[cc].getName()){USlistvalid.push(USlist[cc].getName())}
}
for(cc=0;cc<USlistvalid.length;++cc){
Ulist.addItem(USlistvalid[cc]);
}
return app
}
// serge 07-2012- ok

A small utility to get you calendar names and ID

function Callist(){
  var sh = SpreadsheetApp.getActiveSheet();
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var last = ss.getLastRow();
  sh.getRange(1, 1,last,2).clearContent();                      
  var list = new Array();
  list = CalendarApp.getAllCalendars()
    for (n=0;n<list.length;++n){
      var name = list[n].getName() ;                     
      var id = list[n].getId() ;                     
      sh.getRange(n+1,1).setValue(name)  ;                   
      sh.getRange(n+1,2).setValue(id)  ;                   
    }                 
}

Č
ċ
cal_list utility.rtf
(1k)
Serge Gabet,
Jul 5, 2012, 3:18 PM
ċ
calsub.rtf
(7k)
Serge Gabet,
Jul 5, 2012, 3:03 PM
Comments