2014-04-07 67 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);