Google电子表格 - 如何改进密码生成器代码

时间:2014-04-01 07:22:12

标签: google-apps-script google-sheets

如何调整或改进此密码生成代码,以便它不会在输出中重复字符。

目前它生成如下密码:b5 $ b1#q2 @ w6 @ R1 @ K&

我希望它看起来像这样:%5!L4j @ vd4y!x7JJ

代码:

function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menu = [
{name: "Password Generator", functionName: "password_generator"}
];
ss.addMenu("Disc Activity Menu", menu);
}

function password_generator( len ) {
var length = (len)?(len):(16);
var string = "abcdefghijklnopqrstuvwxyz"; //to upper
var numeric = '0123456789';
var punctuation = '!@#$%&?';
var password = "";
var character = "";
var crunch = true;

while(password.length<length) {
entity1 = Math.ceil(string.length * Math.random()*Math.random());
entity2 = Math.ceil(numeric.length * Math.random()*Math.random());
entity3 = Math.ceil(punctuation.length * Math.random()*Math.random());
hold = string.charAt( entity1 );
hold = (entity1%2==0)?(hold.toUpperCase()):(hold);
character += hold;
character += numeric.charAt( entity2 );
character += punctuation.charAt( entity3 );
password = character;
}
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var activeRow = ss.getActiveRange().getRowIndex();
sheet.getRange("B"+activeRow).setValue(password);
}

1 个答案:

答案 0 :(得分:2)

这个怎么样?

Generate a string of 5 random characters in Javascript

var length = 9;

function makeid()
{
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%&?";

    for( var i=0; i < length; i++)
        text += possible.charAt(Math.floor(Math.random() * possible.length));

    return text;
}