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!