VLOOKUP style app script using UiApp and textBox form to reference a spreadsheet

别等时光非礼了梦想. 提交于 2019-12-01 13:17:28

When the form is submitted, your handler function submit(e) receives an event, e. If we log the value of it with Logger.log(Utilities.jsonStringify(e)); we'll see that looks like this:

{"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"
  }
}

We can access the value of our input boxes by name, for example e.parameter.userName.

One other tweak, we need to get a value for app. Here's what your working handler looks like:

// 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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!