0

有人可以告诉我如何使添加到 flextable 的最后一个项目直接位于现有项目的下方吗?

function doGet() {
  var app = UiApp.createApplication();
  var listBox = app.createListBox();
  listBox.addItem("item 1").addItem("item 2").addItem("item 3").setName("myListBox");

  var handler = app.createServerHandler("buttonHandler");
  // pass the listbox into the handler function as a parameter
  handler.addCallbackElement(listBox);

  var table = app.createFlexTable().setId("myTable");

  var button = app.createButton("+", handler);
  app.add(listBox);
  app.add(button);
  app.add(table);
  return app;
}

function buttonHandler(e) {
  var app = UiApp.getActiveApplication();
  app.getElementById("myTable").insertRow(0).insertCell( 0, 0).setText( 0, 0, e.parameter.myListBox);
  return app;
}
4

2 回答 2

0

这个想法是将您写入的行的索引保存在内存中。可以使用多种方法来实现这一点,但最直接的可能是在表格或 listBox 标记中写入此行索引,如下所示(我没有测试代码,但希望我没有出错):


编辑:标签解决方案似乎不起作用,所以我改为hidden widget解决方案。(经过测试)

function doGet() {
  var app = UiApp.createApplication();
  var listBox = app.createListBox();
  listBox.addItem("item 1").addItem("item 2").addItem("item 3").setName("myListBox");
  var hidden = app.createHidden().setName('hidden').setId('hidden')
  var handler = app.createServerHandler("buttonHandler");
  // pass the listbox into the handler function as a parameter and the hidden widget as well
  handler.addCallbackElement(listBox).addCallbackElement(hidden);

  var table = app.createFlexTable().setId("myTable");

  var button = app.createButton("+", handler);

  app.add(listBox).add(button).add(table).add(hidden);// add all widgets to the app
  return app;
}

function buttonHandler(e) {
  var app = UiApp.getActiveApplication();
  var pos = e.parameter.hidden;// get the position (is a string)
   if(pos==null){pos='0'};// initial condition, hidden widget is empty
   pos=Number(pos);// convert to number
  var table = app.getElementById("myTable")
  table.insertRow(pos).insertCell( pos, 0).setText(pos, 0, e.parameter.myListBox);// add the new item at the right place
  ++pos ;// increment position
  app.getElementById('hidden').setValue(pos);// save value
  return app;// update app
}
于 2012-11-20T06:57:35.177 回答
0

以下代码执行相同的操作,但使用了 ScriptProperties 方法。没有什么特别喜欢它,就像一个魅力:

function doGet() {
  var app = UiApp.createApplication();

  // create listBox and add items
  var listBox = app.createListBox().setName("myListBox")
    .addItem("item 1")
    .addItem("item 2")
    .addItem("item 3");

  // set position
  ScriptProperties.setProperty('position', '0'); 

  // create handle for button
  var handler = app.createServerHandler("buttonHandler").addCallbackElement(listBox);

  // create flexTable and button
  var table = app.createFlexTable().setId("myTable");
  var button = app.createButton("+", handler);

  // add all to application
  app.add(listBox)
    .add(button)
    .add(table);

  // return to application
  return app;
}

function buttonHandler(e) {
  var app = UiApp.getActiveApplication();

  // get position
  var position = parseInt(ScriptProperties.getProperty('position'),10);

  // get myTable and add 
  app.getElementById("myTable").insertRow(position).insertCell(position, 0).setText(position, 0, e.parameter.myListBox);

  // increment position
  var newPosition = position++;

  // set position  
  ScriptProperties.setProperty('position', newPosition.toString()); 

  // return to application
  return app;
}

请调试代码,以便启动授权过程。

参考:脚本和用户属性

于 2012-11-20T19:53:47.743 回答