This is a big project for google-apps-script beginner, I have an inventory list in an array (columnValues) this needs to be searched through for what the user is focused on (ingredient). Right now it is just looking for exact matches but I'm going to go back and make it smarter (with some help), for now though I'm having trouble getting my array of matches (matches) to display in my sidebar. I'd like to eventually give the user a list of potential matches that is smarter (like searching poached chicken will result in chicken breast, chicken wings, and poached salmon). When clicked I'd like this to write values to some cells relative to the users focus (two cells over).
So should I make it a list or a table to display it in the sidebar based on what I want to do? How could I fix the function that makes the list? Any advice on making the search smarter would be welcome. Also any tips for a beginner would be welcome.
JSFiddle link : https://jsfiddle.net/amreinj/fk1a5to7/
GoogleDocs link : https://docs.google.com/spreadsheets/d/1Xnbvy4tg2jNhYqL8QKIPJ28AcJ0whygygKzrQ51cGvQ/edit?usp=sharing
I'm not using jQuery and this answer uses it, could use a translation: How to return an array (Google apps script) to a HTML sidebar?
Code.gs
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Costing Tools')
.addItem('Search menu', 'showSidebar')
.addToUi();
}
function showSidebar() { // Brings up side bar to do inventory search
var html = HtmlService.createHtmlOutputFromFile('page')
.setTitle('Inventory search')
.setWidth(300);
SpreadsheetApp.getUi()
.showSidebar(html);
}
function onSearch() { //When the search button is pressed it loops through the inventory list and finds exact matches eventually I'd like it to be smart
var ui = SpreadsheetApp.getUi();
var ingredient = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getActiveCell().getValue(); //user focus, they click on the item they want to search
Logger.log('i ' + ingredient); //debug
var isheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("INVENTORY");
var columnValues = isheet.getRange("G:G").getValues(); //inventory list
var i = columnValues.length,
array, flag = false,
matches = [];
while (i--) { //walk through columnValues
array = columnValues[i]; //get the ingredient in the li
if (-1 !== array.indexOf(ingredient)) { //-1, string not found
flag = true;
matches.push(array);
}
}
if (!flag) {
ui.alert('No match found.');
} else {
Logger.log('m ' + matches); //debug
function makeUL(arr) { //This function not working
var list = document.createElement('ul'); // Create the list element
Logger.log(list); //debug
for (var i = 0; i < arr.length; i++) { //loop through the array to make the list
var item = document.createElement('li'); // Create the list item
item.appendChild(document.createTextNode(arr[i])); // Set its contents
Logger.log(item); //debug
list.appendChild(item); // Add it to the list
}
Logger.log(list); //debug
return list;
}
document.getElementById('foo').appendChild(makeUL(matches)); //this doesn't work
}
}
page.html
<!DOCTYPE html>
<html>
<head>
<script >
</script>
<base target="_top">
</head>
<body>
<p>Click on the ingredient you want to find in inventory and then click search.</p>
<button onclick="google.script.run.onSearch()">Search</button>
<br>
<br>
<div id="foo">Results</div>
</body>
</html>