Skip to main content

Apps Script

Google Apps Script je skriptovací platforma vyvinutá společností Google pro vývoj lehkých aplikací na platformě Google Workspace. Google Apps Script byl původně vyvinut Mikem Harmem jako vedlejší projekt během jeho práce jako vývojář na Google Sheets.

Skripty

Odstranění nikdy nepřihlášených a deaktivovaných uživatelů (Never logged in, suspended)

Postup:

  1. Přejít na script.google.com.
  2. Vytvořit nový projekt, třeba "Termináthor".
  3. Zkopírovat skript níže (klidně do Code.gs pokud to bude jediný skript v projektu).
  4. V nabídce vlevo vybrat [Services +] a přidat Admin SDK API, verze directory_v1 a identifikátor AdminDirectory
  5. Doplnit si svou doménu místo <YOUR-PRIMARY-DOMAIN>.
  6. Pod řádkem // Specific actions for our accounts. Skip specific accounts from deletion. si poupravit názvy které se mají přeskočit (v našem případě účty cvut-\*, lvice\*, vedecka-rada\*).
  7. Uložit skript a spustit funkci terminate pomocí tlačítka [➡️ Run]
// Done with help from https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/adminSDK.gs
// 2025 Lukas Balonek (balonluk@cvut.cz)

function terminate() {

  // Define variables for page content and token for next page
  let pageToken;
  let page;

  // Get the full list of users, paginated
  // !!! domain parameter must be set !!!
  // It is a shame that we cannot get only those we want to remove (there is no query for that)
  do {
  
    page = AdminDirectory.Users.list({
      domain: '<YOUR-PRIMARY-DOMAIN>',
      orderBy: 'email',
      maxResults: 500,
      pageToken: pageToken
    });

    // Get only users from page obtained (it may contain other bs's like nextPageToken)
    const users = page.users;

    // If it's empty, no users were found :-( Hope we'll never get there.
    if (!users) {
      console.warning('No users found.');
      return;
    }
  
    // Process user one by one
    for (const user of users) {
    
      // Specific actions for our accounts. Skip specific accounts from deletion.
      if (user.primaryEmail.match('cvut-') || user.primaryEmail.match('lvice') || user.primaryEmail.match('vedecka-rada')){
        // If you want to debug skipped accounts, uncomment line below
        //console.info("Skipping: ", user.primaryEmail)
      }
      else if (user.lastLoginTime=="1970-01-01T00:00:00.000Z" && user.suspended==true){
        console.log("Removing user (Never logged in, not skipped, suspended):", user.primaryEmail)
        AdminDirectory.Users.remove(user.primaryEmail);
      }

    }

    // The page we want to get is defined by field nextPageToken in response variable <page>
    pageToken = page.nextPageToken;

    // Do it while we have some page token (the last page will have empty field)
  } while (pageToken);

}