3

I am new to the world of Google's Apps Script, and I am trying to create a basic UI for my end user to query data (stored in google spreadsheets) and display the data in a formatted / user friendly way.

Since I want to start off simple and add in components as I learn Apps Script I decided to create a basic form that will allow me to enter text in a text box, then assign that value to a label (what I thought would be a simple exercise in creating basic form components, and retrieving / assigning values).

Unfortunately the stumbling block I ran into is that there is no getText() or TextBox.getValue() function. I tried searching through online forums / google etc to find out the way around this, but nothing I try seems to work (previous searched led me totry and work this out in an event handler).

My question is this. If I have a textBox ('textBox1') and a button ('button1') and a label ('label1'). How to I get my web app to assign the value I enter in textBox1 to label1 when I click button1?

I realize this is probably the most basic of questions, and I know it has been asked before....but no matter how hard I dig through these old posts I can't seem to figure what should be an easy bit of code out.

Thanks in advance.

1
  • You didn't search very well... a simple search on this forum with the gas tag returned dozens of results... for example this one : simple and clear! Commented Feb 11, 2013 at 16:47

1 Answer 1

2

Suppose you have code that looks like this:

var textbox = app.createTextBox().setName("textboxName");
var buttonHandler = app.createServerHandler("updateLabelText").addCallbackElement(textbox);
var button = app.createButton("Click me").addClickHandler(buttonHandler);
var label = app.createLabel().setId("label");

Then in your function:

function updateLabelText(e) {
  var app = UiApp.getActiveApplication();
  app.getElementById("label").setText(e.parameter.textboxName);
  return app;
}

So the things to note:

  1. The button handler is given the name of a function that you define somewhere else in your code.
  2. The button handler also must be given a "callback element". This is required if you want to read the value of that element in your function. You can add a panel as a callback element and anything that's on that panel will be added to the callback.
  3. Values of callback elements are passed back through e.parameter. The property name is the name of the element. Example: e.parameter.textboxName.
  4. The label needs an ID so that you can reference it from your other function.
Sign up to request clarification or add additional context in comments.

8 Comments

In response to your "stumbling block," apps scripts require that you explicitly pass the value back to the script. The scripts are run on Google's servers so they don't know the value of the text boxes unless you tell them what it is. Read the documentation on it here.
Thank you for the quick response! I tried what I thought was that approach but my issue is that the text value I am getting back is coming in as "Undefined". Here is my current code:
function doGet() { var app = UiApp.createApplication(); app.add(app.loadComponent("studentLookup01")); var handler = app.createServerClickHandler('testGetFunction'); handler.addCallbackElement(app.getElementById('sNameEntryBox')); app.getElementById('Button1').addClickHandler(handler); return app; }
function testGetFunction(e) { var app = UiApp.getActiveApplication(); var sEnteredName = e.parameter.sNameEntryBox; var sDisName = app.getElementById('sNameDisplayLabel'); sDisName.setText(sEnteredName); app.close(); return app; }
sNameEntryBox = the ID of my textBox sNameDisplayLabel = the label I want to display my entered text in. When I click the button, my label displays the text "undefined". I'm confused as to what I'm doing wrong. :/
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.