1

我正在尝试创建一个应用程序脚本以在具有搜索文本框的 Google 站点内工作,该搜索文本框应该引用电子表格并从该搜索中吐出结果。我相信这个问题存在于代码的后半部分,我试图根据输入到下面代码提供的框中的文本来引用电子表格。

function doGet(e) {
var doc = SpreadsheetApp.openById(SPREADSHEET_ID_GOES_HERE);
var app = UiApp.createApplication().setTitle('New app');

 // Create the entry form, a 1 x 2 grid with text boxes for name, age, and city that is then added to a vertical panel
var grid = app.createGrid(1, 2);
grid.setWidget(0, 0, app.createLabel('Name:'));
grid.setWidget(0, 1, app.createTextBox().setName('userName').setId('userName'));


 // Create a vertical panel and add the grid to the panel
 var panel = app.createVerticalPanel();

panel.add(grid);

var buttonPanel = app.createHorizontalPanel();

var button = app.createButton('submit');
var submitHandler = app.createServerClickHandler('submit');
submitHandler.addCallbackElement(grid);
button.addClickHandler(submitHandler);
buttonPanel.add(button);

  var closeButton = app.createButton('close');
  var closeHandler = app.createServerClickHandler('close');
  closeButton.addClickHandler(closeHandler);
  buttonPanel.add(closeButton);

// Create label called statusLabel and make it invisible; add buttonPanel and          statusLabel to the main display panel.
var statusLabel = app.createLabel().setId('status').setVisible(false);
panel.add(statusLabel);

panel.add(buttonPanel);

app.add(panel);
return app;
}


function close() {
var app = UiApp.getActiveApplication();
app.close();

return app;
}

问题存在于下面的代码中。我正在尝试使用输入到文本框中的文本来搜索电子表格。我不确定如何实现这一目标。

// function called when submit button is clicked
function submit(e) {

// Write the data in the text boxes back to the Spreadsheet
var cell = getValue(e.parameter.submit);

var doc = SpreadsheetApp.openById('SPREADSHEET_ID_GOES_HERE');
var ss = doc.getSheets()[0];
var lastRow = doc.getLastRow();
var data = ss.getRange(2, 1, 2, 4).getValues();

for(nn=0;nn<data.length;++nn){
 if (data[nn][1]==cell){break} ;// if a match in column B is found, break the loop
  }

 // Make the status line visible and tell the user the possible actions
app.getElementById('status').setVisible(true).setText(data[nn][1]);
return app;
}​
4

1 回答 1

1

提交表单时,您的处理函数submit(e)会收到一个事件,e. 如果我们记录它的值,Logger.log(Utilities.jsonStringify(e));我们会看到如下所示:

{"parameter":
  {
    "clientY":"52",
    "clientX":"16",
    "eventType":"click",
    "ctrl":"false",
    "meta":"false",
    "source":"u146139935837",
    "button":"1",
    "alt":"false",
    "userName":"Lucy",
    "screenY":"137",
    "screenX":"16",
    "shift":"false",
    "y":"14",
    "x":"12"
  }
}

我们可以通过名称访问输入框的值,例如e.parameter.userName.

另一项调整是,我们需要为app. 这是您的工作处理程序的样子:

// function called when submit button is clicked
function submit(e) {
  var app = UiApp.getActiveApplication();

  Logger.log(Utilities.jsonStringify(e));  // Log the input parameter (temporary)

  // Write the data in the text boxes back to the Spreadsheet
  var cell = e.parameter.userName;

  var doc = SpreadsheetApp.openById('SPREADSHEET-ID');
  var ss = doc.getSheets()[0];
  var lastRow = doc.getLastRow();
  var data = ss.getRange(2, 1, 2, 4).getValues();

  var result = "User not found";
  for (nn = 0; nn < data.length; ++nn) {
    if (data[nn][1] == cell) {
      result = data[nn][1];
      break
    }; // if a match in column B is found, break the loop
  }

  // Make the status line visible and tell the user the possible actions
  app.getElementById('status').setVisible(true).setText(result);
  return app;
}
于 2013-05-18T10:33:41.750 回答