zaterdag 23 mei 2020

My first addon: draft mails in Gmail & events in Calendar

At my workplace 'project' names (names of dossiers) are anchors for automating tasks. They are used for merging, time tracking, (gmail)labels, folder names, file names, calendar events, emails etcetera.

My first addon is another way to use 'project' names in Google calendar to add an event and in Gmail to make a draft email. The 'project' names are stored in a Google spreadsheet and used in a dropdown menu in the addon. The same goes for specified event locations and event descriptions.

CardService Class SelectionInput want the input as an object.

function getObject(column,ssid,sheetname){
  
  var ss = SpreadsheetApp.openById(ssid);
  var sheet = ss.getSheetByName(sheetname);
  var array = sheet.getRange(column + "1:" + column).getValues().map(function(row){return row[0]}).filter(function(row){return row!== ""}).sort();
  
  var object = {"key":""};
  
  for(var a=0;a<array.length;a++){
    object["key"+ a]=array[a];
  }
  return object;
}


function getWidgetDropDown(title,fieldname,object){

  var widget = CardService.newSelectionInput()
  .setType(CardService.SelectionInputType.DROPDOWN)
  .setTitle(title)
  .setFieldName(fieldname);
  
  for(var o in object){
    widget.addItem(object[o],object[o],false); 
  }
    
  return widget;
  
}

Other widgets I am using are the DateTimePicker and the TextButton

function getWidgetDateTimePicker(title,fieldname){
  
  var widget = CardService.newDateTimePicker()
  .setTitle(title)
  .setFieldName(fieldname);
  
}


function getWidgetTextButton(bgcolorrgb,text,functionname){
  
  var widget = CardService.newTextButton()
  .setTextButtonStyle(CardService.TextButtonStyle.FILLED)
  .setBackgroundColor(bgcolorrgb)
  .setText(text)
  .setOnClickAction(CardService.newAction().setFunctionName(functionname));
  
  return widget;
  
}

At last but not least the functions with which the addon card, the draft email and the event are created.
 
function buildCard(){
  
 var card=CardService.newCardBuilder();
  
 var widgetProjectName = getWidgetDropDown("Kies een zaaknaam","project_name",getObject("A",IDGMAKWRK(),"HLP_ZAAKNAMEN"));
 card.addSection(CardService.newCardSection().addWidget(widgetProjectName));

 var widgetDraftMail = getWidgetTextButton("#ff0000","Concept email","createDraftMail");
 card.addSection(CardService.newCardSection().addWidget(widgetDraftMail));
 
 
 var widgetEventStartDateTime = CardService.newDateTimePicker().setTitle("Kies startdatum").setFieldName("event_start");
 var widgetEventDuration = CardService.newTextInput().setTitle("Kies duur in minuten").setFieldName("event_duration"); 
 var widgetEventLocation = getWidgetDropDown("Kies een locatie","event_location",getObject("A",IDGMAKWRK(),"HLP_LOCATIE"));
 var widgetEventDescription = getWidgetDropDown("Kies een beschrijving","event_description",getObject("A",IDGMAKWRK(),"HLP_EVENT"));
 var widgetEvent = getWidgetTextButton("#ff0000","Afspraak","createEvent");
 card.addSection(CardService.newCardSection().addWidget(widgetEventStartDateTime).addWidget(widgetEventDuration).addWidget(widgetEventLocation).addWidget(widgetEventDescription).addWidget(widgetEvent)); 
 
 return card.build();
  
}


function createDraftMail(e){
  
  var projectName = e.formInput.project_name;
  
  if(projectName == undefined){
    
    return CardService.newActionResponseBuilder().setNotification(CardService.newNotification().setText("Zaaknaam niet ingevuld")).build();
    
  }
  else{
  
  CardService.newComposeActionResponseBuilder().setGmailDraft(GmailApp.createDraft("",projectName,"")).build();
    
  }
  
}


function createEvent(e){
  
  var projectName = e.formInput.project_name;
  var startDate =e.formInput.event_start.msSinceEpoch;Logger.log(startDate)
  var eventDuration = e.formInput.event_duration;
  var endDate = startDate + (eventDuration * 60 * 1000); Logger.log(endDate)
  var location = e.formInput.event_location;
  var description = e.formInput.event_description;
    
  if(projectName == undefined || startDate == undefined || eventDuration == undefined || startDate > endDate){
    
    return CardService.newActionResponseBuilder().setNotification(CardService.newNotification().setText("Zaaknaam en/of email en/of tijd en/of duur niet [goed] ingevuld")).build();
    
  }
  else{
    
   var calendar = CalendarApp.getCalendarById(MIJNEMAIL());
   calendar.createEvent(projectName, new Date(startDate), new Date(endDate),{description:description,location: location}); 
    
  }
    
}

Also this time I should have started with reading the documentation on the Card Service first ;-).

dinsdag 5 mei 2020

Contacts, enums for extended contact fields, enums for enums for extended contact fields & Google Apps Script. HELP

It took some time before I understood how I had to do it, adding extended fields to a contact in Google Contacts:

function pieterPukOne(){
 
  ContactsApp.getContact("puk@pieterpuk.xyz").addCustomField(ContactsApp.ExtendedField.SENSITIVITY,"CONFIDENTIAL");
  ContactsApp.getContact("puk@pieterpuk.xyz").addCustomField(ContactsApp.ExtendedField.GENDER,"MALE");
  
}

The first problem is dat I can not see the newly added custom fields in the contact information of the contact Pieter Puk.

The second problem is the result of 'searching for' contacts by custom field:

function pieterPukTwo(){
 
  Logger.log(ContactsApp.getContact("puk@pieterpuk.xyz").getCustomFields().map(function(customField){return customField.getValue()}));
  Logger.log(ContactsApp.getContact("puk@pieterpuk.xyz").getCustomFields(ContactsApp.ExtendedField.GENDER)[0].getValue());
  Logger.log(ContactsApp.getContactsByCustomField("puk pieter#no problem at all","Zaaknaam").map(function(iets){return iets.getFullName()}));
  Logger.log(ContactsApp.getContactsByCustomField("CONFIDENTIAL","SENSITIVITY").map(function(customField){return customField.getFullName()}));
  Logger.log(ContactsApp.getContactsByCustomField("MALE","GENDER").map(function(customField){return customField.getFullName()}));
 
}

Strange:


Contact Pieter Puk has three custom fields but the only 'custom field' returned with ContactsApp.getContactsByCustomField is 'Zaaknaam'.

I do not understand what I am doing wrong

The third problem is that I do not know what can be done with for example the extended field SENSITIVITY.CONFIDENTIAL.

Does it limit the way contact information can be used?


maandag 4 mei 2020

How to make a Projects & Tasks Manager with Google Apps Script



I borrowed 'my' Projects & Tasks Manager from Ilya Mikhelson because of his use of the Google Spreadsheet's formula Sparkline() for the timeline of the manager.  The formula is not the easiest one. The 'onion method' of Ben Collins helped me to understand it.

=SPARKLINE({INT(B4)-INT($B$2);INT(C4)-INT(B4)};
    {"charttype"\"bar";"color1"\"white";"color2"\IF(E4="Done";"LemonChiffon";
        IF(today()>C4;"OrangeRed";
            IF(AND(today()>B4;today()<C4;E4="To do");"Orange";
                IF(AND(today()>B4;today()<C4);"Yellow";"Chartreuse"))));
                    "max"\INT($C$2)-INT($B$2)})

'My' Projects & Tasks Manager makes a folder and a (gmail) label on spreadsheet and sheet (project) level. It also makes a contact group on sheet (project) level.

Every project (sheet tab, some sheet rows, folder) has its own color.

The Projects & Tasks Manager can be downloaded via this link. Make a copy and check the script before you use it (via the menu for which a trigger must be added)

My 'learning moments':

Other sources of inspiration:

UPDATED 05-05-2020



maandag 20 april 2020

Importing a (part of a) PDF schedule into Google Calendar with #GoogleAppsScript


Twice a year I get a schedule like the schedule above. It is part of a PDF document. Every row is a 'shift' in which asylum applications are processed at Airport Schiphol in the so called border procedure. In every 'shift' there are 5 days for a lawyer.

How to get all these days into my calendar.

I copy the schedule and paste it into a mail to myself with $$$$$ in the subject.

An then:

function shiftToCalendar(){
  
  var kindOfShiftDays = [[0,"dagmineen"],[1,"a&c eerste gehoor"],[2,"a&c nader gehoor"],[3,"zienswijze"],[4,"beschikking"]]
  var threads = GmailApp.search("newer_than:1d from:me subject:$$$$$");
  var message = threads[0].getMessages()[0];
  
  if(message){
    
  var shifts = message.getBody().split(/A11608/g); 
    
    for(var d=1;d<shifts.length;d++){
      
      var shift = shifts[d];
      
      if(/AC /.test(dienst) && shift.match(/\d{1,2}\-\d{1,2}\-\d{4}/gm) !== null){
            
        var shiftDays = shift.match(/\d{1,2}\-\d{1,2}\-\d{4}/gm);
        
            for(var e=0;e<shiftDays.length;e++){
             
              var hlpShiftDay = shiftDays[e].split(/\-/g);
              var shiftDay = new Date(hlpShiftDay[2],hlpShiftDay[1]-1,hlpShiftDay[0]); 
              
              for(var f=0;f<kindOfShiftDays.length;f++){
                  
                if(kindOfShiftDays[f][0] == e){var what = kindOfShiftDays[f][1]}
                 
                  }
            
              CalendarApp.getCalendarById(MIJNKALENDER()).createAllDayEvent(what,shiftDay).setColor(d);
            
            }
        }
    }  
  
    message.moveToTrash();
  }  
}

vrijdag 17 april 2020

Get the number of the week with #GoogleAppsScript

Javascript does not have a method to get the number of the week.

It seems that the fourth of January always is in the first week of a year. If that is correct GAS can help to get the number of the week we are living in.

Very interesting to know.


function getWeekNumber(){
  
  var thisYear = (new Date()).getFullYear();
  var fourthDayOfTheYear = new Date(thisYear,0,4);
  
  var dayFirstMonday = fourthDayOfTheYear.getDate() + 8 - fourthDayOfTheYear.getDay();
  var dateFirstMonday = new Date(thisYear,0,dayFirstMonday);
  var msFirstMonday = dateFirstMonday.getTime(); 
  
  var msDateToday = (new Date()).getTime(); 
  var msOneWeek = 7*24*60*60*1000; 

  for(var i=0;i<52;i++){
    
    var msNextMonday = msFirstMonday + (i * msOneWeek);
    var msComingNextMonday = msNextMonday + msOneWeek;
    
    if(msNextMonday <= msDateToday && msDateToday < msComingNextMonday){
      
      var weekNumber = i+2;
      
    }
  }
  return weekNumber;
}  



vrijdag 10 april 2020

Combining Google Meet and Google Voice outside the US, Canada



In times of social distancing communicating with a detained person for the communication with whom an interpreter is needed is difficult. Because of rules a detained person can not be called but has to call me and an interpreter can not call me but has to be called.

With Google Meet someone can call in into a (non) video meeting but with Google Meet it is not possible to call a number that is not a number from the US or Canada.

Luckily there is Voicemeeter Banana an Advanced Audio Mixer Application endowed with Virtual Audio Device used as Virtual I/O to mix and manage any audio sources from or to any audio devices or applications (in their words).

With the audio mixer you can mix-minus (yourself and) Google Meet and (a softphone like) Google Voice. And it works!

Google Meet audio settings:



Google Voice audio settings:






zondag 15 maart 2020

A Walk A Day Keeps The Doctor Away #GoogleAppsScript



function aWalkADayKeepsTheDoctorAway(){

 var file = DriveApp.getFilesByName("yyyyy").next(); //csv file with latitude/longitude points
 var fileBlob = file.getAs("text/csv");

 var fileData = Utilities.parseCsv(fileBlob.getDataAsString(), ",");
 var points = fileData.filter(function(row,index){return index>2}).map(function(row){return row[4].split(/geo:/)[1].split(",")}); 
  
 var directions = Maps.newDirectionFinder().setMode(Maps.DirectionFinder.Mode.WALKING).setOrigin(points[0]).setDestination(points[points.length-1]);
  for(var p=1;p<points.length;p++){
    directions.addWaypoint(points[p]);
  }
  
  var route = directions.getDirections().routes[0];
  var map = Maps.newStaticMap().addPath(route.overview_polyline.points);
 
  var distance  = route.legs.map(function(leg){return leg.duration.value}).reduce(function(accumulator,currentValue){return accumulator + currentValue;})/1000;
  
  var email = Session.getActiveUser().getEmail();
  MailApp.sendEmail
  (email,
  'A walk a day keeps the doctor away: ' + distance + " km",'Please open: ' + map.getMapUrl() + '&key=YOUR_API_KEY', 
    {
      htmlBody: '<br/><img src="cid:mapImage">',inlineImages: {mapImage: Utilities.newBlob(map.getMapImage(), 'image/png')                                                                }
    }
  );
  
}