I’ve created a simple UI, just a few textboxes and a couple of buttons. The concept is basically that of a voting app for a spreadsheet I maintain. As the script runs it will present each row of the spreadsheet into the textboxes and allow the user to vote on that item. I created the UI by hand:
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);
}
The issue is that when I call my NextIdea function I get an error:
function NextIdea() {
var app = UiApp.getActiveApplication();
... Get the next row of the spreadsheet ...
app.getElementById("txtCat").setText("TestValue"); // <--- This is the error line
On the line where I attempt to set the text in the text box I receive an error “Object does not allow properties to be added or changed.” In the debug window it gives me an object number, but there are no properties and trying .getText returns as undefined.
Both of these are fired by my ProcessVotes function, which is called by a custom menu item in the spreadsheet:
function ProcessVotes() {
BuildUI();
NextIdea();
}
This is my first attempt at creating a GAS, not sure what I’m missing. I do see a lot of examples using return app; at the end of a function, but I am under the impression that is only for use when the script is published as a service. I’m trying to use this inside a spreadsheet (using onOpen instead of onGet, etc.)
in this part :
What is nextRowCat ? is it supposed to be a string (then you forgot the ”) ? is it a variable with a a string value ?
Anyway, it should be a string 😉
EDIT following your comment : did you try your code with this new version exactly (
.setText("TestValue"))?Also I don’t see any show(app) in your main function (the one where you create your UI instance app)… I guess it would be easier to help if you show the whole code or at least a simplified but ‘runnable’ code where you get the error.
Concerning
getText()that returns undefined, this is a normal behavior : there is no such method on textBox…(see docs) The usual way to retrieve its value is by usinge.parameter.textBoxNamein a handler function.