2014-04-07 66 views
0

我的密码生成器按钮是我的UI形式。我想要将生成的值添加到文本框中。任何帮助将非常感激。Google Apps脚本 - 在UiForm的文本框中添加值

代码:

function doGet(e) { 
var app = UiApp.createApplication() 
.setTitle('Password Generator') 
.setHeight(200) 
.setWidth(300); 

var panel = app.createFormPanel(); 
var grid = app.createGrid(4, 3) 
.setId('formGrid'); 

//Password Label and Textbox 
var passLabel = app.createLabel('Password'); // Label Password 
passLabel.setStyleAttributes({fontFamily: "Arial", fontWeight: "bold", color: "#0040FF"}); 

var passTextbox = app.createTextBox() // Text Box Password 
.setId('Password') 
.setWidth('150px') 
.setName('password'); 

生成密码按钮

var passwordGen = app.createButton("Generate Password") 
.setId("passwordGen"); 
passwordGen.addClickHandler(app.createServerHandler("passGenFunction")); 
app.add(passwordGen); 

grid.setWidget(0, 1, passLabel) 
.setWidget(1, 1, passTextbox) 
.setWidget(1, 2, passwordGen) 

panel.add(grid) 
app.add(panel); 
SpreadsheetApp.getActiveSpreadsheet().show(app); 
} 

密码生成器功能部分

function password_generator() { 
var confirm; 
var length = 16; 
var pass = ""; 
var possible = "[email protected]#$%&?"; 

for(var i=0; i < length; i++) 
pass += possible.charAt(Math.floor(Math.random() * possible.length)); 
//Missing code: generator button clicked. Results placed into passTextbox// 
return app; 
} 

回答

0

答案更简单添加$textBoxId$pass:在password_generator功能,一个单行:

return UiApp.getActiveApplication().getElementById('Password').setText(pass);

+0

你好哔叽,谢谢你的一行代码,但是当我把在'$ pass + = possible.charAt(Math.floor(Math.random()* possible.length));'并且移除'var app = UiApp.createApplication();'它生成器不起作用后, – WallyG

+0

https://docs.google.com/spreadsheet/ccc?key=0Ag8NytPhOo00dEFjT0JIMDZsTmxlN1JLSFJ0TnJLNUE&usp=sharing – WallyG

0

想出答案。

我需要在$function passGenFunction()下定义$var app = UiApp.createApplication();。然后在$app.getElementById("Password").setText(pass);