部署UI Web应用程序

时间:2012-06-29 19:43:37

标签: google-apps-script

首次在GAS中部署UI Web应用程序。在尝试查看已部署的脚本URL后,获取“无法调用方法”getSheets“null”TypeError:

https://script.google.com/macros/s/AKfycbzuQNbkTeBpRPz75Q4dRiVLfEYtuLiuBKnWeA5CbD0/dev

这是电子表格:

https://docs.google.com/spreadsheet/ccc?key=0AnJTymuE8awLdHZ6QzhNVXpjeFphM3pMV1cxU0daZ2c

脚本代码:

// https://developers.google.com/apps-script/class_listbox - How to create listBox
function doGet() {
   //var ss = SpreadsheetApp.openById("0AnJTymuE8awLdHZ6QzhNVXpjeFphM3pMV1cxU0daZ2c");
   var ss = SpreadsheetApp.getActiveSpreadsheet();
   var sheet = ss.getSheets()[1];  
   var app = UiApp.createApplication().setTitle('App to show how ListBox data can update');
   var panel = app.createVerticalPanel();
   var lb = app.createListBox(true).setId('myLBid').setName('myLBname');

   // how many items to show
   lb.setVisibleItemCount(10);

   // get sorted names
   var rows = sheet.getDataRange();
   var numRows = rows.getNumRows();
   var values = rows.getValues();

   for (var i = 0; i <= numRows - 1; i++) {
     var row = values[i];
     lb.addItem(row);
   }

   panel.add(lb);
   var button = app.createButton('Submit');
   var handler = app.createServerClickHandler('respondToSubmit').addCallbackElement(panel);
   button.addClickHandler(handler);
   panel.add(button);
   app.add(panel);
   ss.show(app);
 }

// http://youtu.be/5VmEPo6Rkq4 - How to have sumbit write data to ss
function respondToSubmit(e) {
   var app = SpreadsheetApp.getActiveSpreadsheet();
   //reference widget name to capture what user selected
   var listBoxValue = e.parameter.myLBname;
   var ss = SpreadsheetApp.getActiveSpreadsheet();
   var sheet = ss.getSheets()[2];
   var lastRow = sheet.getLastRow()+1;
   var lastCell = sheet.getRange("A"+lastRow);
   lastCell.setValue(listBoxValue);
   return app.close();
}

/**
 * Adds a custom menu to the active spreadsheet, containing a single menu item
 * for invoking the readRows() function specified above.
 * The onOpen() function, when defined, is automatically invoked whenever the
 * spreadsheet is opened.
 * For more information on using the Spreadsheet API, see
 * https://developers.google.com/apps-script/service_spreadsheet
 */
function onOpen() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var entries = [{
    name : "Names",
    functionName : "doGet"
  }];
  sheet.addMenu("Script Menu", entries);
};

从编辑器运行时效果很好。

想要了解我需要在代码中修改我的Web应用程序才能运行。我希望UI能够像在ss中运行时一样出现在脚本URL中。这不是应该发生的事吗?

1 个答案:

答案 0 :(得分:3)

编辑:哎呀,抱歉,我没有注意到其他一些错误,请查看有关differences between webApps and spreadsheet UIs的文档并在此之后阅读我的回答。

你为什么评论这一行?

SpreadsheetApp.openById("0AnJTymuE8awLdHZ6QzhNVXpjeFphM3pMV1cxU0daZ2c");

作为网络应用运行时,您应始终按ID打开电子表格,因为没有有效的电子表格(从电子表格查看没有用户已打开电子表格,只有脚本有...)

另外:你的意思是什么“我希望用户界面出现在脚本网址”

当您访问将在部署时创建的URL时,您的Ui将显示为独立应用程序。(您还必须保存它的版本,但在执行此操作时可以很好地解释)