Integration with Google Sheets

GIRITON attendance also offers REST api for all work with data which companies can use to integrate GIRITON into other in-house systems.

Google Sheets can use REST api, making it easy to connect data from GIRITON to your Google Sheet. For example, you can retrieve a list of currently present people from GIRITON, list them in a sheet of your spreadsheet and continue working with that data using your own Excel formulas - each time you open a Google Sheet.

Implementation of GIRITON REST api integration into Google Sheets

Open your Google Sheet into which you want to integrate GIRITON data. Select Tools - Script Editor in the top Menu. In the window you see, you can use Google App Script to write simple functions, create custom menus, buttons in Google Sheet, respond to various events, work with data from the sheet and also download data from REST api of other services. You can run the script, debug it, view the values of individual variables, etc., from here. We enclose a code sample that downloads the currently present people and writes them to the sheet of a spreadsheet.

//function onOpen is called automatically each time you open Google Sheet
function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('GIRITON Integrace') //Addition of a graphical Menu to Google Sheet
      .addItem('Synchronizovat směny','refreshDataFromGiriton')
      .addToUi();
}
 
function refreshDataFromGiriton() {
  try{
    var urlOptions = {
     "method" : "GET",
     "headers" : {
       "giriton-token" : "vasRestApiToken", //REST api token generated in the application
       "cache-control": "no-cache"
     }
    };
    
    var urlAddress = "https://rest.giriton.com/system/api/attendance/userActivity";//Address REST api
    var response = UrlFetchApp.fetch(urlAddress, urlOptions);
    var respDataText = response.getContentText();
    var respData = JSON.parse(respDataText);
    
    var output = [];
    
    respData.forEach(function(elem,i) {
      var inProgressActivity = elem["inProgressActivity"];
      var inprogressActivityName = "";
      if(inProgressActivity){
        inprogressActivityName = inProgressActivity["name"];
      }
      output.push([elem["person"]["firstName"], elem["person"]["lastName"], inprogressActivityName ]);
    });
    
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data z GIRITON REST api");
    sheet.clearContents();
    sheet.getRange(1,1,output.length,3).setValues(output);
  } catch(error) {
    Browser.msgBox(error); //If an error occurs, we will display it to the user
    throw error;
  }
}

When opening Google sheet, the code sample adds a custom menu called “GIRITON Integration”, inserting the “Synchronize Data” sub-menu. When you click on this sub-menu, the refreshDataFromGiriton () function is called which downloads the data from the GIRITON REST api, loads this data into the output field, deletes all content from the sheet called “Data from the GIRITON REST api” and saves the loaded data into this sheet of Google sheet. Rest api is called to download the currently present people, in this particular case.

You can run, debug and step through the script (or its selected method) at the top of the Google Script Editor window. Google will ask you if you trust the script and allow it to access your Google Docs the first time you run it. Allow the access.

Please note that if an error occurs during the execution of the script thanks to the line Browser.msgBox (error), this error will be displayed in the window with the Google Sheet itself (a different window than the one in which you are currently editing the App Script). The script will still run and not finish until you click-out the error in the window with the table.

Automatic loading of data when you open Google Sheet

If you do not want to click on the menu in your Google Sheet to restore the data but instead want to load the data automatically when you open Google Sheet, in Google Sheet click on Tools - Script Editor - Edit - Current project’s triggers. In the new window click “Add trigger” below and set the option as follows: Select function to run -> refreshDataFromGiriton, select implementation to run -> Head, Select event source -> Select event type from the table -> When opened. Click save eventually. Every time you open your document, the data from the REST api is automatically loaded from now on. Please note that calling REST api may take a few seconds.

Examples of AppScript for Google Sheets

A collection of various AppScript for integration with GIRITON Attendance is here.

Further examples of using Google App Script to download data into Google Sheets via REST api can be found here