Creating a "Computer Boot and Shutdown Tracker" to attempt an employee timeclock in Google sheets and cannot get past an error, looking for assistance

44 views
Skip to first unread message

Josh Deering

unread,
Apr 2, 2023, 2:36:30 AM4/2/23
to Google Apps Script Community
  I am running into "User" error issues within the script, it will not find the active user, nor can I figure out how to assign a specific users. 
 I am attempting to make a "timeclock" through google, so when employees log in / log out it is documented in google sheets. 

 Each employee has their own computer, 
Each employee has their own individual login to google drive on boot daily, 

 Here is the script;  

  • function onOpen() {
      // Get the current date and time.
      var now = new Date();

      // Create a new spreadsheet.
      var ss = SpreadsheetApp.create('Computer Boot and Shutdown Tracker');

      // Create a new sheet in the spreadsheet.
      var sheet = 'Sheet1';

      // Get the current user.
      var user = User.getActiveUser();

      // Add a column for the date and time of the boot or shutdown.
      sheet.appendRow([now]);

      // Add a column for the reason for the boot or shutdown.
      sheet.appendRow(['']);

      // Add a column for the user who initiated the boot or shutdown.
      sheet.appendRow([user.getEmail()]);

      // Add a column for the notes about the boot or shutdown.
      sheet.appendRow(['']);

      // Set the onOpen property of the spreadsheet to this function.
      ss.setOnOpen(onOpen);
        }

        // Import the GoogleAppsScript module.
        import { User as _User } from 'google-apps-script';

        // Import the User class.
        const User = _User;

        // Define the onBoot function.
        function onBoot() {
       // Get the current date and time.
       var now = new Date();

       // Add a row to the spreadsheet with the current date and time, the reason for the boot, the user        who initiated the boot, and any notes about the boot.
      sheet.appendRow([now, 'Boot', User.getActiveUser().getEmail(), '']);
        }

        // Define the onShutdown function.
        function onShutdown() {
       // Get the current date and time.
        var now = new Date();

       // Add a row to the spreadsheet with the current date and time, the reason for the shutdown, the   user who initiated the shutdown, and any notes about the shutdown.
      sheet.appendRow([now, 'Shutdown', User.getActiveUser().getEmail(), '']);
        }
    `
The error I get back is:
Syntax error: SyntaxError: Cannot use import statement outside a module line: 31 file: Computer Boot and Shutdown Tracker.gs

I am reaching out to see what can be changed within the script to find the user that boots up the computer (logs into google drive) and shuts down the computer (logs out of google drive). We could even go as far as listing the specific user that owns the computer if needed.

I much appreciate any input into this!

cwl...@gmail.com

unread,
Apr 2, 2023, 9:49:50 AM4/2/23
to Google Apps Script Community
Google and Apps script operate on google's servers, so has no way of doing anything until someone interacts with some Google interface.

Otherwise, you would have to develop some app that runs on the local machine and "pushes" info to your sheet.

If the trigger is simply to see who opens the spreadsheet, than that is pretty straightforward and you have most of the code already.

Nerio Villalobos

unread,
Apr 11, 2023, 4:20:57 AM4/11/23
to google-apps-sc...@googlegroups.com
It looks like the error is being caused by the import statement at the beginning of your script. Google Apps Script does not support the use of import statements, so you will need to remove that line.

To get the active user in Google Apps Script, you can use the Session.getActiveUser() method instead of User.getActiveUser(). Here is an updated version of your script that should work:

function onOpen() {
  // Get the current date and time.
  var now = new Date();

  // Create a new spreadsheet.
  var ss = SpreadsheetApp.create('Computer Boot and Shutdown Tracker');

  // Create a new sheet in the spreadsheet.
  var sheet = ss.getActiveSheet();


  // Get the current user.
  var user = Session.getActiveUser();


  // Add a column for the date and time of the boot or shutdown.
  sheet.appendRow([now]);

  // Add a column for the reason for the boot or shutdown.
  sheet.appendRow(['']);

  // Add a column for the user who initiated the boot or shutdown.
  sheet.appendRow([user.getEmail()]);

  // Add a column for the notes about the boot or shutdown.
  sheet.appendRow(['']);

  // Set the onOpen property of the spreadsheet to this function.
  ss.setOnOpen(onOpen);
}

// Define the onBoot function.
function onBoot() {
  // Get the current date and time.
  var now = new Date();

  // Get the active spreadsheet.
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  // Get the active sheet.
  var sheet = ss.getActiveSheet();

  // Get the active user.
  var user = Session.getActiveUser();


  // Add a row to the spreadsheet with the current date and time, the reason for the boot, the user who initiated the boot, and any notes about the boot.
  sheet.appendRow([now, 'Boot', user.getEmail(), '']);

}

// Define the onShutdown function.
function onShutdown() {
  // Get the current date and time.
  var now = new Date();

  // Get the active spreadsheet.
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  // Get the active sheet.
  var sheet = ss.getActiveSheet();

  // Get the active user.
  var user = Session.getActiveUser();


  // Add a row to the spreadsheet with the current date and time, the reason for the shutdown, the user who initiated the shutdown, and any notes about the shutdown.
  sheet.appendRow([now, 'Shutdown', user.getEmail(), '']);
}

Note that this script will only log the boot and shutdown events when the onBoot and onShutdown functions are manually triggered, either by clicking a button or by running the functions from the script editor. It will not automatically detect when the computer is booted or shut down.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/0641fa80-fd97-4160-89f8-dcf4e4f960dan%40googlegroups.com.


--
__________________________
Nerio Enrique Villalobos Morillo
Buenos Aires, Argentina
Reply all
Reply to author
Forward
0 new messages