Google脚本错误:"遇到错误:发生意外错误"

时间:2015-02-17 22:48:10

标签: google-apps-script

我遇到间歇性错误“遇到错误:在下面运行Google脚本时出现意外错误”。它发生在第一个clickHandler“Next”之后。我无法确定它为什么会发生以及模式是什么。单击“确定”后,它继续到代码的第二部分并完成。该脚本可用,但错误消息很烦人。有人可以帮忙吗?

function updateRecord() {
   var app = UiApp.createApplication().setTitle('Sheet 1').setHeight(1000).setWidth(300);
   var panel = app.createVerticalPanel().setWidth(300).setHeight(900);
   var lb = app.createListBox(true).setHeight(800).setWidth(300).setId('myId').setName('myLbName');

  //add items to ListBox
   lb.setVisibleItemCount(3);
   var ss = SpreadsheetApp.openById('xxx');
   var list = ss.getSheetByName('Sheet 2');
   var stnum = list.getRange(2,11,1,1).getValues()
   var values = list.getRange(2,1,stnum,1).getValues().sort();
   for (var i=0; i<values.length; i++){
   lb.addItem(values[i][0].toString());
   }

   panel.add(lb);
   var button = app.createButton('Next').setStyleAttribute('font-weight','bold');
   var handler = app.createServerClickHandler('next').addCallbackElement(panel);
   button.addClickHandler(handler);
   panel.add(button);

   app.add(panel); 
   SpreadsheetApp.getUi().showSidebar(app);
   return app;
 }

function next(eventInfo) {

   var ss = SpreadsheetApp.openById('xxx');
   var sheet = ss.getSheetByName('Sheet 3');

   //Create menu items
   var mnum = sheet.getRange(2,9,1,1).getValues();
   var matvalues = sheet.getRange(2,1,mnum,1).getValues().sort();
   var pnum = sheet.getRange(2,10,1,1).getValues();
   var platvalues = sheet.getRange(2,2,pnum,1).getValues().sort();

  //Create panels, scroll panels, button, and grid
   var app = UiApp.createApplication().setTitle('Enter Additional Information').setHeight(600).setWidth(500).setStyleAttribute("background-color","light blue").setStyleAttribute('padding','20');
   var scrollPanlel = app.createScrollPanel().setSize('300', '1000');
   var panel = app.createVerticalPanel();
   var buttonPanel = app.createHorizontalPanel();
   var grid = app.createGrid(6, 1);
   var button = app.createButton('Submit').setStyleAttribute('font-weight','bold');//.setStyleAttribute('color','red');
   var submitHandler = app.createServerClickHandler('update');
   submitHandler.addCallbackElement(grid);
   button.addClickHandler(submitHandler);
   buttonPanel.add(button);

  //Add list boxes to the grid     
  grid.setWidget(1, 0, app.createLabel('Value 1:').setStyleAttribute('font-weight','bold').setStyleAttribute('color','purple'));
  var matrix = app.createListBox(true).setHeight(100).setWidth(200).setId('myId1').setName('myLbName1');
  grid.setWidget(2, 0, matrix);   
  for (var i in matvalues){
   matrix.addItem(matvalues[i][0].toString());
   }

  grid.setWidget(3, 0, app.createLabel('Value 2:').setStyleAttribute('font-weight','bold').setStyleAttribute('color','purple'));
  var platform = app.createListBox(true).setHeight(100).setWidth(200).setId('myId4').setName('myLbName4');
  grid.setWidget(4, 0, platform);  
  for (var i in platvalues){
   platform.addItem(platvalues[i][0].toString());
   }

  //Add all elements to the dialog
  app.add(scrollPanlel);
  scrollPanlel.add(panel);
  panel.add(grid);
  panel.add(buttonPanel);

  //Show the dialog window
  SpreadsheetApp.getUi().showSidebar(app);

  return app;
 }

1 个答案:

答案 0 :(得分:1)

这一行:

var stnum = list.getRange(2,11,1,1).getValues()

应该是:

var stnum = list.getRange(2,11,1,1).getValues()[0][0];

getRange()方法返回二维数组。但该行只获得一个单元格的值。它只获得一行和一列。因此,二维数组中的一个值的索引都是零。

我不是说这是你的主要问题,但我已经解决了这个问题。

您可以使用以下代码转换为HTML服务:它不是您代码的完整重构,但它会为您提供一个开始。

gs Code

function showSidebar() {
  var html = HtmlService.createTemplateFromFile('Page')
    .evaluate()
    .setSandboxMode(HtmlService.SandboxMode.IFRAME)
    .setTitle('My custom sidebar')
    .setWidth(300);

  SpreadsheetApp.getUi()
      .showSidebar(html);
}

获取列表框的项目

function getListBoxItems() {
  var ss = SpreadsheetApp.openById('Your ID');
  var list = ss.getSheetByName('Sheet 2');

  var allSheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
  var stnum = list.getRange(2,11,1,1).getValues()[0][0];
  var values = list.getRange(2,1,stnum,1).getValues().sort();

  var htmlSelectOptions = "";
  var thisListItem = "";

  for (var i=0; i<values.length; i++){
    thisListItem = values[i].toString();
    htmlSelectOptions += '<option value="' + thisListItem + '">' + thisListItem + '</option>';
  }

  return htmlSelectOptions;
};

HTML文件:命名页面

<div>
  <br>Text Here:
  <br>
  <select id="myId" name="myLbName">
    <?!= getListBoxItems(); ?>
  </select>

  <br>
  <br>

  <input type="button" value="Next" id="idCopyBtn" onclick="google.script.run.next()">
</div>