Google apps script VLOOKUP风格的应用程序脚本,使用UiApp和textBox表单引用电子表格

Google apps script VLOOKUP风格的应用程序脚本,使用UiApp和textBox表单引用电子表格,google-apps-script,google-sheets,google-sites,Google Apps Script,Google Sheets,Google Sites,我正在尝试创建一个应用程序脚本,在一个谷歌网站中工作,该网站有一个搜索文本框,用来引用电子表格,并从搜索中吐出结果。我相信,问题存在于代码的后半部分,我试图根据下面代码提供的框中输入的文本引用电子表格 function doGet(e) { var doc = SpreadsheetApp.openById(SPREADSHEET_ID_GOES_HERE); var app = UiApp.createApplication().setTitle('New app'); // Create

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

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;
}​
//单击提交按钮时调用的函数
功能提交(e){
//将文本框中的数据写回电子表格
var cell=getValue(如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();

对于(nn=0;nn当表单提交时,您的处理函数
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;
}
//单击提交按钮时调用的函数
功能提交(e){
var app=UiApp.getActiveApplication();
Logger.log(Utilities.jsonStringify(e));//记录输入参数(临时)
//将文本框中的数据写回电子表格
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=“未找到用户”;
对于(nn=0;nn
提交表单时,处理程序函数
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;
}
//单击提交按钮时调用的函数
功能提交(e){
var app=UiApp.getActiveApplication();
Logger.log(Utilities.jsonStringify(e));//记录输入参数(临时)
//将文本框中的数据写回电子表格
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=“未找到用户”;
对于(nn=0;nn
我认为这与日志有关,我只是不确定如何调用它。此代码运行得很好。谢谢!也许你可以帮我回答另一个问题。在当前的for/if逻辑测试中,如果在电子表格中找不到匹配的用户名,它只会在代码停止后抛出一个错误。我如何添加ele这样它就可以用“找不到用户”来代替setText(data[nn][1])代码?再次感谢编辑,用一个例子说明了一种实现这一点的方法,它可以很容易地扩展到其他字段。哇,这太简单了。谢谢,我现在已经全部完成了。我想这与日志有关。我只是不知道如何调用它。这段代码工作得很好。谢谢!也许你可以帮我解决另一个问题在上。使用当前的for/if逻辑测试,如果在电子表格中找不到匹配的用户名,则在代码停止后只会抛出一个错误。我如何添加一个元素,以使它沿着“User not found”(用户未找到)的行而不是setText(数据[nn][1])代码?再次感谢编辑,用一个例子说明了一种实现这一点的方法,可以很容易地扩展到其他字段。哇,这太简单了。谢谢,我现在已经全部完成了。实际问题是什么?哪一行,什么没有发生?如果抛出错误,请包括错误的详细信息。实际问题是什么?哪一行,如果抛出错误,请包括错误的详细信息。