0

我创建了一个简单的 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等)

4

1 回答 1

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 实例应用程序的那个)中没有看到任何显示(应用程序).​​.....我想如果您显示整个代码或至少一个简化但'出现错误的可运行代码。

关于getText()返回未定义,这是一种正常行为:在 textBox 上没有这样的方法...(请参阅文档)检索其值的常用方法是e.parameter.textBoxName在处理函数中使用。

于 2012-09-05T16:54:54.207 回答