Google Apps脚本 - 为UniForm中的文本框添加价值

时间:2014-04-07 09:34:01

标签: google-apps-script google-sheets

我的Ui表格中有一个密码生成器按钮。我想将生成的值添加到textBox中。任何帮助将不胜感激。

代码:

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 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%&?";

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;
}

2 个答案:

答案 0 :(得分:0)

找出答案。

我需要在$var app = UiApp.createApplication();下定义$function passGenFunction()。然后在$textBoxId

中添加$pass$app.getElementById("Password").setText(pass);

答案 1 :(得分:0)

答案更简单:在password_generator函数中,只有一行:

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