By following the instruction of this website, I made a web app using Google App Script (GAS) which will display the search when a user enters a search term (e.g. order number, status, price) in the search window and the search is successful (The screenshot below represents what I expected). However, my app always returns 'Data not found!' even if the search term exists in the data. What am I missing, then?
Components of the app
Data
A spreadsheet that contains the exactly same contents of the original spreadsheet (i.e. I copied the original contents and pasted them to my own spreadsheet to prepare the data)
Index.html
I copied the code from https://gist.github.com/bpwebs/dbbd96f8262e486c8f4c321ea0d3a95a#file-index-html and pasted it to 'Index.html' in my own GAS project.
Code.gs
I modified the original code on
https://gist.github.com/bpwebs/dbbd96f8262e486c8f4c321ea0d3a95a#file-code-gs
since the original code has an undefined variable (Sheets on ln.19) and cannot open the spreadsheet. I replaced the relevant code with SpreadsheetApp.openById() and range.getValues().
function doGet() {
return HtmlService.createTemplateFromFile('Index').evaluate();
}
/* PROCESS FORM */
function processForm(formObject){
var result = "";
if(formObject.searchtext){//Execute if form passes search text
result = search(formObject.searchtext);
}
return result;
}
//SEARCH FOR MATCHED CONTENTS
function search(searchtext){
// var spreadsheetId = '1p7fKVM7HA4Ikl8gYC2WesmHcIqAqJQWTfIuiqB4SyN4'; //** CHANGE !!!
// var dataRage = 'Data!A2:Y'; //** CHANGE !!!
var ss = SpreadsheetApp.openById('The URL to my spreadsheet');
var range = ss.getRange('Data!A2:Y');
var data = range.getValues();
// var data = Sheets.Spreadsheets.Values.get(spreadsheetId, dataRage).values;
var ar = [];
data.forEach(function(f) {
if (~f.indexOf(searchtext)) {
ar.push(f);
}
});
return ar;
}
