google-apps-scriptgoogle-sheetsgoogle-sheets-apigoogle-workspace

Log users visiting a Google Spreadsheet


I'm trying to track visitors from my organization to my Spreadsheet. I've been researching scripts that can do this and I finally seem to have one that allows to track edits. However, some of the people visiting my spreadsheet, won't make any edit. And I still want to retrieve their email address (or at least handle).

Seems like it would be a very simple script but I haven't found anything. Anyone know how we can do this?


Solution

  • Playing around, I found the solution. Here's the script:

    function onOpen(e)
    { 
      try
      {
        var ui = SpreadsheetApp.getUi();
        ui.createMenu('Action')
        .addItem('Add Data', 'addData')
        .addToUi()  
    
        var sheet =SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Login Details");
        sheet.appendRow([Session.getActiveUser().getEmail(),new Date()]);
    
      }
      catch(e)
      {
        Logger.log(e);
      }
    }