我创建了一个简单的 UI,只有几个文本框和几个按钮。这个概念基本上是我维护的电子表格的投票应用程序。当脚本运行时,它会将电子表格的每一行显示到文本框中,并允许用户对该项目进行投票。我手动创建了 UI:
function BuildUI() {
var app = UiApp.createApplication().setTitle("Voting Machine").setWidth(700);
var doc = SpreadsheetApp.getActiveSpreadsheet();
var buttonWidth = "125px";
// Panels
var mainPanel = app.createVerticalPanel();
var horzPanel = app.createHorizontalPanel();
var buttonGrid = app.createGrid(1,3);
// Textboxes
var txtCat = app.createTextBox().setName("txtCat").setId("txtCat");
var txtIdea = app.createTextBox().setName("txtIdea").setId("txtIdea");
// Handlers
var handler = app.createServerHandler();
handler.setCallbackFunction("recordVote");
handler.addCallbackElement(txtCat).addCallbackElement(txtIdea);
// Buttons
var voteNo = app.createButton("No").setId("voteNo")
.addClickHandler(handler).setWidth(buttonWidth);
var voteMaybe = app.createButton("Maybe").setId("voteMaybe")
.addClickHandler(handler).setWidth(buttonWidth);
var voteYes = app.createButton("Yes").setId("voteYes")
.addClickHandler(handler).setWidth(buttonWidth);
horzPanel.add(txtCat).add(txtIdea)
buttonGrid.setWidget(0, 0, voteNo)
.setWidget(0, 1, voteMaybe)
.setWidget(0, 2, voteYes);
mainPanel.add(horzPanel).add(buttonGrid);
app.add(mainPanel);
doc.add(app);
}
问题是当我调用 NextIdea 函数时出现错误:
function NextIdea() {
var app = UiApp.getActiveApplication();
... Get the next row of the spreadsheet ...
app.getElementById("txtCat").setText("TestValue"); // <--- This is the error line
在我尝试在文本框中设置文本的行中,我收到错误消息“对象不允许添加或更改属性”。在调试窗口中,它给了我一个对象编号,但没有属性并且尝试.getText
返回未定义。
这两个都由我的 ProcessVotes 函数触发,该函数由电子表格中的自定义菜单项调用:
function ProcessVotes() {
BuildUI();
NextIdea();
}
这是我第一次尝试创建 GAS,不确定我缺少什么。我确实看到很多return app;
在函数末尾使用的示例,但我的印象是仅在脚本作为服务发布时使用。我正在尝试在电子表格中使用它(使用onOpen
而不是onGet
等)