Turn Excel into a Password Generator


In this Excel Training video, we’ll explore a practical and efficient way to create a random password generator right within Microsoft Excel. Whether you’re an individual looking to enhance your digital security or a team seeking a streamlined approach to password generation, this tutorial will equip you with the knowledge to build a customized solution using a custom Office script.

Resources

Copy and paste the following Office Script:

function main(workbook: ExcelScript.Workbook) {
// Call the password generator function
const password = generatePassword();

// Print the generated password to the console
console.log(password);

// Optionally, you can write the generated password to a cell in Excel
const selectedSheet = workbook.getActiveWorksheet();
selectedSheet.getRange('A1').setValue(password);
}

function generatePassword(): string {
const lowercase = 'abcdefghijklmnopqrstuvwxyz';
const uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const numbers = '0123456789';
const special = '!@#$%^&*()-_=+[]{}|;:\',.?/';

const requiredLength = 10;
const requiredSpecialCount = 2;

// Generate the password with at least 1 of each required character type
let password = '';
password += getRandomCharacter(lowercase);
password += getRandomCharacter(uppercase);
password += getRandomCharacter(numbers);
password += getRandomCharacter(special);

// Generate the remaining characters
const remainingLength = requiredLength - password.length;
for (let i = 0; i !== remainingLength; i++) {
  const charPool = lowercase + uppercase + numbers + special;
  password += getRandomCharacter(charPool);
}

// Shuffle the characters to make the password more random
password = shuffleString(password);

return password;
}

function getRandomCharacter(source: string): string {
const randomIndex = Math.floor(Math.random() * source.length);
return source.charAt(randomIndex);
}

function shuffleString(str: string): string {
  const arr: string[] = str.split('');
  const length: number = arr.length;
  for (let i: number = length - 1; i !== 0; i--) {
    const j: number = Math.floor(Math.random() * i);
    const temp: string = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
  }
  return arr.join('');
}

John Gleave

John Gleave has been a researcher, content writer, and senior editor at Business Tech Planet since 2022. John was formerly a data analyst and web designer with expertise in several programming languages, such as JavaScript, JQuery, PHP, CSS, SQL, and more! With a passion for writing and technology, he has now focused his skills on crafting tech guides for BTP. You can connect with John on LinkedIn.

Recent Posts