Wednesday, April 9, 2014

Check if Google Apps Users Have Logged in (Google Apps Script)

If you're using Google Apps for Education (with the provisioning API enabled) and have a list of domain users that you want to check if they've logged in or not, here's a quick Spreadsheet script you can try. It queries to see if the user has agreed to the terms or not. Of course you'll need to run this from an account that has admin permissions on your domain.

function onOpen() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var entries = [{name : "Start Checking", functionName : "startLoop"}];
  spreadsheet.addMenu("Check Users", entries);
}

function startLoop() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var activeSheet = sheet.getActiveSheet();
  var maxRows = activeSheet.getMaxRows();
  var result = Browser.msgBox('This script will check ' + maxRows + ' rows worth of data from the currently selected cell.', Browser.Buttons.OK_CANCEL);
  if (result != 'cancel') {
    for (var i=0;i<maxRows;i++) {checkUser();}
  } else {Browser.msgBox('Okay, maybe some other time');}
}

function checkUser() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = SpreadsheetApp.getActiveRange();
  var newRange =  range.offset(0, 1);
  var username = range.getValue();
  var user = UserManager.getUser(username);
  var agreedToTerms = user.getAgreedToTerms();
  newRange.setValue(agreedToTerms);
  var newSelection = range.offset(1, 0);
  newSelection.activate();
}

I'm assuming that you're somewhat familiar with Google Apps Script and using it with Spreadsheets. Let me know in the comments if you need clarification.

No comments: