2

Desired Outcome: To be able to enter a search term in a Google Form (presumably but not necessarily; could be a form in a standard web page) and have the relevant data retrieved from a Google Sheet and displayed in Google Site web app.

I learnt how to retrieve data from a parameterized URL and display in a Google Site in this question: How to include data in a URL for a Google Apps Script web app to read?

So the "tech" for retrieving and displaying spreadsheet data is there but I don't know where to start when it comes to pulling the data from a online form rather than a URL. Perhaps on submit, read the form values somehow, create a parameterized URL and go to that page to display the data?

MrGreggles
  • 6,113
  • 9
  • 42
  • 48

1 Answers1

3

How about this sample? This is a very simple sample script. Please modify it to your environment. This sample retrieves data on Spreadsheet using the search text, and displays the matched row. In order to use this sample, please carry out as follows.

  1. Copy and paste the following scripts to your script editor.
  2. Input spreadsheet ID and sheet name which is used for searching data.
  3. Deploy Web Apps and run script.
  4. Input search text and push "ok" button.

Script :

Google Apps Script : code.gs

function doGet() {
  return HtmlService.createTemplateFromFile('index').evaluate();
}

function getData(e) {
  var id = "### Spreadsheet ID ###";
  var sheetname = "### Sheet name ###";
  var data = SpreadsheetApp.openById(id).getSheetByName(sheetname).getDataRange().getValues();
  var ar = [];
  data.forEach(function(f) {
    if (~f.indexOf(e.searchtext)) {
      ar.push(f);
    }
  });
  return ar;
}

HTML : index.html

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<form>
<input type="text" name="searchtext">
<input type="button" value="ok" onClick="getData(this.parentNode)" />
</form>
<pre id="disp"></pre>

<script>
function dispData(e) {
  $('#disp').text(JSON.stringify(e));
}
function getData(e) {
  google.script.run.withSuccessHandler(dispData).getData(e);
}
</script>

Sample spreadsheet :

enter image description here

Result :

enter image description here

If I misunderstand your question, I'm sorry.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thanks heaps for this. Unfortunately when I publish it then copy the URL into a new tab and hit enter, I get this message: "Sorry, unable to open the file at this time. Please check the address and try again." – MrGreggles Oct 13 '17 at 07:56
  • @MrGreggles I'm sorry for the inconvenience. Now I confirmed that this script works fine, again. After the script is modified, the project version has to be updated. By this, the latest script can be used by Web Apps. If you want to know how to get the URL, please confirm [here](https://developers.google.com/apps-script/guides/web#deploying_a_script_as_a_web_app) or [here](https://stackoverflow.com/questions/46717644/how-to-use-google-script-run/46719679#46719679). – Tanaike Oct 13 '17 at 13:10
  • @MrGreggles If it didn't work, please create a new project and copy and paste scripts and run it. Even if you did this, if it didn't work, can I ask you about the detail flow that you did? – Tanaike Oct 13 '17 at 13:11
  • Thank you for your kind responses. I got it to work and then it didn't work; then it worked then it didn't. I'll take a look at it again when I'm better rested. But it looks very promising and I really appreciate your efforts here! – MrGreggles Oct 15 '17 at 09:35
  • @MrGreggles I'm sorry for being still the inconvenience. I would like to solve your problem. So if you can permit, can I confirm your script? I think that when the part of problem which is script or configuration is found, it approaches the solution. – Tanaike Oct 15 '17 at 11:55