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.
PSST, HEY, YOU
(YEAH, YOU!)
Want in on insightful videos, the latest tech developments, and epic exclusive content? Get all this and more as a member of our mailing list.
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('');
}