0

I'm sure this is a really dumb question, but I've spent an hour googling, with no luck.

I'm storing spreadsheet data (some text, some dates) in various widgets. At some point, a click handler has to read back the text or the date from the widget, and write it out to another spreadsheet. Currently, I've got the data in a FlexTable (or in a Label widget in the FlexTable). I've now found out (I think) that there's no way to read back this data.

Any ideas how I'm meant to handle this? I just need a widget that will let me store data, display it, and later read it back.

Thanks.

EDIT

I don't think the answers have actually got me any further forward. I appreciate that I can read the value of some widgets if they're passed to the handler as a callback. However, this appears to be restricted to ones with a setName method - is this just TextBox and ListBox? If so, that's no use, because TextBox is for user entry.

So, is there a widget that will let me (the script, not the user) store data, and later read it back? Or am I just being thick?

EML
  • 9,619
  • 6
  • 46
  • 78
  • you can use textBoxes to 'show only' by disabling them... so they are not only for user entries... if you want you can have a look at [this example](https://script.google.com/macros/s/AKfycbyxFoCafXmjs79LsKwKFKydEtXJXg_wnvCHQIiAntiF-0vj56s/exec) where I use data from a spreadsheet shown in grid and textBoxes. code is [here](https://docs.google.com/spreadsheet/ccc?key=0AnqSFd3iikE3dG43N0FHXzN1cVFqZnQyekpSYzRtZ1E) – Serge insas Jan 19 '13 at 22:31

3 Answers3

1

You haven't search very well, there are plenty of examples around here... just one posted today : Date AND Time picker Google App Script

and the documentation, look at the (near) end of the page...

Community
  • 1
  • 1
Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • Ok, think I've got it, thanks. Is this restricted to callbacks? I can't find a method to get the value in any other context. If it is restricted to callbacks, why? It would be nice to do getElementById or something similar instead of addCallBackElement. – EML Jan 18 '13 at 22:23
  • that's how it works... note that the name of the widget is used in the callBackElement as reference, not its ID (as also mentioned by David). This is an important point to remember (and a frequent source of error ;-) – Serge insas Jan 18 '13 at 22:40
  • Actually, this hasn't got me any further forward. The problem with TextBox is that it is just that - a TextBox - and I need somehting to display spreadsheet data, not to hold user input. So I need a general-purpose text holding widget that I set and read myself, and which has a setName method. – EML Jan 18 '13 at 23:47
  • In my opinion this should never be an issue : if the textBox (or any other widget) contains data from a spreadsheet and no one is actually changing it, then you can easily read this data in the spreadsheet itself since there is a one to one relationship between these two elements right ? Well That's how I handle this type of use case... I don't want to convince you ;-) – Serge insas Jan 19 '13 at 00:07
  • Ok, so there's no general answer to the problem? Because widgets are essentially for display, and not reading back? I can live with always reading from the spreadsheet, but I have to make a lot of changes to my code, so I wanted to be sure first. If this is true, I think Google has screwed up here. I noticed there was a 2-year-old tracker issue to allow read-back from FlexTables, but no-one's done anything abut it. Thanks. – EML Jan 19 '13 at 00:26
1

You can actually read the text from a Label widget if you set the text in it's tag as well...

// Get Label text and set it to another label
function doGet() {
  var app = UiApp.createApplication();
  var panel = app.createVerticalPanel();
  var label1 = app.createLabel('hello').setTag('hello').setId('label1');
  Logger.log(label1.getTag()); // works
  var label2 = app.createLabel('').setId('label2');
  var handler = app.createServerHandler('myFunction').addCallbackElement(panel);
  var btn = app.createButton('Get tag', handler);
  panel.add(label1).add(label2).add(btn);
  app.add(panel);
  return app;
}

function myFunction(e) {
  var app = UiApp.getActiveApplication();
  Logger.log(app.getElementById('label1').getTag()) // doesn't work
  var tag = e.parameter.label1_tag; // works
  Logger.log(tag); 
  app.getElementById('label2').setText(tag); // Sets label2 text as label1 tag
  return app;
}

There's no .setName() that you specify with a Label, but it looks like one gets automatically created.

In your case, storing text inside of invisible Text Boxes that coincide with the Labels would be another option.

Bryan P
  • 5,031
  • 3
  • 30
  • 44
0

Have a look at https://developers.google.com/apps-script/uiapp#ServerHandlers Values of widgets are passed to server handler in e.parameter. Don't forget to give widget Name and Id. If you want to get values of widgets that are not the one triggering the event handled then you need to pass the data using the widget tag value

DavidF
  • 1,335
  • 1
  • 15
  • 26
  • Thanks, I'd just been looking at that code. However, the problem is that (1) if I use getElementById then I have no method to *read* the contents of the label in the handler (I think), and (2) labels have no setName so I can't pass the label in as a parameter to the handler. – EML Jan 18 '13 at 23:45