GUI表单不显示

时间:2013-12-19 22:25:39

标签: google-apps-script google-sheets

我被卡住了...我的代码不想向我展示我的GUI表单。

如果我在sh=SpreadsheetApp.getActive()之前尝试添加此行:sh.show(app) - 脚本在脚本编辑器中正常工作。但!如果我尝试部署为Web应用程序 - 脚本不起作用。

function doGet() {
  var app = UiApp.createApplication().setTitle('test online').setHeight(400).setWidth(600);
  var dFormat = UiApp.DateTimeFormat.DATE_LONG
  var sh = SpreadsheetApp.openById(spreadsheetID);
  var settings = sh.getSheetByName('Name');
  var lastrowTime = sh.getSheetByName('Name').getLastRow();
  var settings = settings.getRange("A2:A" + lastrowTime).getValues();

  var main2 = app.createGrid(1, 4);
  var status = app.createLabel().setId('status').setWidth('200');
  var card = app.createTextBox().setName('card').setId('card').setWidth('50');

  var main1 = app.createGrid(6, 3);
  var placeA = app.createTextBox().setId('placeA').setName('placeA').setWidth('400');
  var placeB = app.createTextBox().setId('placeB').setName('placeB').setWidth('400');
  var phone = app.createTextBox().setId(('phone')).setName('phone').setWidth('200');
  var timeTo = app.createListBox(false).setWidth(200).setName('timeTo').addItem("...").setVisibleItemCount(1);

   for (var i = 0; i < settings.length; i++) {
   timeTo.addItem(settings[i]);
  }

  var main = app.createGrid(4, 5);
  var date = app.createDateBox().setName('date').setFormat(dFormat);
  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 refresh = app.createButton('Refresh')
  var button = app.createButton('Submit')

  var main3 = app.createGrid(1,3);
  var price = app.createLabel().setId('price').setWidth('400');
  var finalStatus = app.createLabel().setId('finalPrice').setWidth('400');

  main2.setWidget(0,0, app.createLabel('Client card: ')).setWidget(0,1, card).setWidget(0,3, status);

  main1.setWidget(1,0, app.createLabel('From')).setWidget(1,1,placeA);
  main1.setWidget(2,0, app.createLabel('To')).setWidget(2,1,placeB);
  main1.setWidget(4,0, app.createLabel('Mobile')).setWidget(4,1,phone);
  main1.setWidget(5,0, app.createLabel('Make a call?')).setWidget(5,1,timeTo);

  main.setWidget(1,0,app.createLabel('Data')).setWidget(1,1,app.createLabel('hour')).setWidget(1,2,app.createLabel('min'))
  main.setWidget(2,0,date).setWidget(2,1,hour).setWidget(2,2,min)
  main.setWidget(2,3,refresh).setWidget(2,4, button)

  main3.setWidget(0,0, price);
  main3.setWidget(0,1, finalStatus);

  var serverHandler = app.createServerHandler('show').addCallbackElement(main).addCallbackElement(main1).addCallbackElement(main2).addCallbackElement(main3);
  button.addClickHandler(serverHandler)

  var handler1 = app.createServerHandler('refresh').addCallbackElement(main).addCallbackElement(main1).addCallbackElement(main2).addCallbackElement(main3);
  refresh.addClickHandler(handler1)

  var handler2 = app.createServerHandler('checkDate').addCallbackElement(main).addCallbackElement(main1).addCallbackElement(main2).addCallbackElement(main3);
  date.addValueChangeHandler(handler2)

  app.add(main2)
  app.add(main1)
  app.add(main)
  app.add(main3)

 sh.show(app)
}

1 个答案:

答案 0 :(得分:1)

用于显示UI的方法专门用于电子表格容器。你可能已经读过这篇文章了,但要重新阅读Creating User Interface Elements in UI Service,尤其是doGet()的例子。

function doGet() { // A script with a user interface that is published as a web app
                   // must contain a doGet(e) function.
  ...
  return myapp;
}

在函数结束时,您只需要return您的UI App实例。无需致电show,也无需参考电子表格。