我创建了一个简单的用户界面,只有几个文本框和几个按钮。这个概念基本上是我维护的电子表格的投票应用程序的概念。当脚本运行时,它会将电子表格的每一行显示在文本框中,并允许用户对该项目进行投票。我手工创建了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
等。)
答案 0 :(得分:0)
在这一部分:
function NextIdea() {
var app = UiApp.getActiveApplication();
... Get the next row of the spreadsheet ...
app.getElementById("txtCat").setText(nextRowCat) // <--- This is the error line
nextRowCat是什么?它应该是一个字符串(然后你忘了'')?它是一个带字符串值的变量吗?
无论如何,它应该是一个字符串; - )
编辑跟随您的评论:您是否准确地尝试了这个新版本的代码(.setText("TestValue")
)?
此外,我没有在您的主要功能(您创建UI实例应用程序的那个)中看到任何节目(应用程序)...我想如果您显示整个代码或至少一个代码会更容易帮助你得到错误的简化但“可运行”的代码。
关于返回undefined的getText()
,这是正常行为:textBox上没有这样的方法...(see docs)检索其值的常用方法是使用{{1}在处理函数中。