-1

Is it possible to add a picker box for both Date AND Time within a GUI?

If not, is it possible to combine two variables, one for date (picker) and the other for time (number) into one variable to be used as a Start Time variable for calendar?

Thanks!

user1989097
  • 1
  • 1
  • 1
  • 2
  • There is no combined widget for date and time but what you suggest is perfectly possible. Would you like to use a text box for time or would you prefer a couple of list boxes? – Serge insas Jan 18 '13 at 06:23

1 Answers1

2

here is a working solution using listBoxes, test it in a spreadsheet(make a copy to use)

function listBoxVersion() {
  var app = UiApp.createApplication().setTitle('Time Picker');
  var main = app.createGrid(2, 4);
  var date = app.createDateBox().setName('date');
  var hour = app.createListBox().setName('hour').setWidth('100');
  var min = app.createListBox().setName('min').setWidth('100');
  for (h=0;h<24;++h){
  if(h<10){var hourstr='0'+h}else{var hourstr=h.toString()}
  hour.addItem(hourstr)
  }
  for (m=0;m<60;++m){
  if(m<10){var minstr='0'+m}else{var minstr=m.toString()}
  min.addItem(minstr)
  }
  var button = app.createButton('validate')
  main.setWidget(0,0,app.createLabel('Choose Date')).setWidget(0,1,app.createLabel('Choose Hours')).setWidget(0,2,app.createLabel('Choose minutes'))
  main.setWidget(1,0,date).setWidget(1,1,hour).setWidget(1,2,min)
  main.setWidget(1,3,button)
  var handler = app.createServerHandler('show').addCallbackElement(main)
  button.addClickHandler(handler)
  app.add(main)
  ss=SpreadsheetApp.getActive()
  ss.show(app)
}

function show(e){
  ss=SpreadsheetApp.getActive()
  ss.getRange('A1').setValue(Utilities.formatDate(e.parameter.date,'GMT+02:00','MMM-dd-yyyy')+'  @ '+e.parameter.hour+':'+e.parameter.min)
  var date = new Date(e.parameter.date);
  date.setHours(e.parameter.hour,e.parameter.min,0)
  ss.getRange('A2').setValue(date)
  }

enter image description here

EDIT : and here is another version with an ordinary textBox :(available in the same test sheet)

  function textVersion() {
  var app = UiApp.createApplication().setTitle('Time Picker');
  var main = app.createGrid(2, 4);
  var date = app.createDateBox().setName('date');
  var hour = app.createTextBox().setName('time').setWidth('150');
  var button = app.createButton('validate')
  main.setWidget(0,0,app.createLabel('Choose Date')).setWidget(0,1,app.createLabel('Enter Hours:minutes'))
  main.setWidget(1,0,date).setWidget(1,1,hour);
  main.setWidget(1,3,button)
  var handler = app.createServerHandler('show2').addCallbackElement(main)
  button.addClickHandler(handler)
  app.add(main)
  ss=SpreadsheetApp.getActive()
  ss.show(app)
}

function show2(e){
  ss=SpreadsheetApp.getActive()
  var time = e.parameter.time.split(':')
  var hour = time[0]
  var min = time[1]
  ss.getRange('A1').setValue(Utilities.formatDate(e.parameter.date,'GMT+02:00','MMM-dd-yyyy')+'  @ '+hour+':'+min)
  var date = new Date(e.parameter.date);
  date.setHours(hour,min,0)
  ss.getRange('A2').setValue(date)
  }

enter image description here

Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • Ok. This is exactly what I am looking for. Just one last thing. I am new to Google Scripts, and my plan is to use this in a Web GUI. So how would I add this to what I have so far? Here is a link to the doc with the code. https://docs.google.com/document/d/1ywUWNer085bPj3H6p2d6Rkzi-fbBE2m_uyxxsmIVDQs/edit – user1989097 Jan 18 '13 at 13:08
  • just keep everything you don't have yet, i.e. main and the handler and add main to your panel somewhere you would like it to appear... if you meet some difficulties please note in you online doc the place where it should appear in your UI and I'll do it for you. The hanler function must be copied "as it is" – Serge insas Jan 18 '13 at 14:30
  • did you choose the listBox version or the other one ? – Serge insas Jan 18 '13 at 14:30
  • by web GUI did you mean the GUI graphic builder ? if so you can refer to [this post](http://stackoverflow.com/questions/13425999/is-it-possible-to-embed-a-gui-built-with-gui-builder-in-another-panel/13434092#13434092) where I explain how to combine GUI elements with script designed UI – Serge insas Jan 18 '13 at 14:47
  • The script that I am using I found online and enabled you to create calendar events using a GUI, create a cal event and then log it to a spreadsheet. I decided to go with the List Boxes above. I am now just unsure how to add it to the script. I made a note in the Google Doc above on where I would like it within the Gui. The GUI I am using was not made with GUI editor, it was copied from another forum. Thanks again Serge! – user1989097 Jan 18 '13 at 14:55
  • [here is your script](https://script.google.com/d/1mW9owEzn__eYhPkBdhyItdyGk_1k3bc8OFpSJh10GZIOp6eoddjA_mJS/edit) Don't forget to accept the response. – Serge insas Jan 18 '13 at 15:17
  • 2
    The methods & APIs used in this particular solution are now deprecated. Does someone have an updated version of the time picker ? – roneo Oct 27 '15 at 16:54
  • 2
    I tried to make a copy of the sheet, but the picker doesn't show. – beruic Apr 04 '16 at 09:32
  • I just checked and it is still working. did you get an authorization request message ? – Serge insas Apr 04 '16 at 09:41