I have a custom function that works searching Drive. I am trying to get the web app to pass the searchterm to the custom function and return the results.
Currently, when I test the function in the logger it works and returns the files.
In the example below I have a hello world working- but as mentioned earlier I haven't been able to pass the searchterm from the input form.
CODE.GS
function SearchFiles() {
var searchterm ="'YOUR SEARCHTERM'"; // this would be the variable that is passed from the form on index.html
var searchFor ="title contains " + searchterm;
var owneris ="and '[email protected]' in Owners";
var names =[];
var fileIds=[];
Logger.log(searchFor + " " + owneris);
var files = DriveApp.searchFiles(searchFor + " " + owneris);
while (files.hasNext()) {
var file = files.next();
var fileId = file.getId();// To get FileId of the file
fileIds.push(fileId);
var name = file.getName();
names.push(name);
}
for (var i=0;i<names.length;i++){
Logger.log(names[i]);
Logger.log("https://drive.google.com/uc?export=download&id=" + fileIds[i]);
}
}
function doGet(e) {
var template = HtmlService.createTemplateFromFile('Index');
return template.evaluate()
.setTitle('Hello World')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
function parseDataFromAppscript()
{
return "Hello World!";
}
function processForm(formObject) {
Logger.log('I was called!');
// here is where I would like to display results of searchterm.
}
INDEX.HTML
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script>
function displayMessage()
{
google.script.run.withSuccessHandler(helloWorld).parseDataFromAppscript();
}
function helloWorld(stringText)
{
document.writeln(stringText);
}
</script>
</head>
<body>
<input type="text" name="search">
<input type="button" value="submitButton" name="submitButton" onclick="displayMessage()"/>
</body>
</html>
Add an id to your input text box, then use the DOM to get the value. Put the search term in the parenthesis of the gs function being called.
Index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script>
function displayMessage() {
var searchTerm;
searchTerm = document.getElementById('idSrchTerm').value;
console.log('searchTerm: ' + searchTerm );
google.script.run
.processForm(searchTerm);
}
</script>
</head>
<body>
<input type="text" id="idSrchTerm" name="search">
<input type="button" value="submitButton" name="submitButton" onclick="displayMessage()"/>
</body>
</html>
Code.GS
function SearchFiles() {
var searchTerm = "";
var searchFor ="title contains " + searchTerm;
var owneris ="and '[email protected]' in Owners";
var names =[];
var fileIds=[];
Logger.log(searchFor + " " + owneris);
var files = DriveApp.searchFiles(searchFor + " " + owneris);
while (files.hasNext()) {
var file = files.next();
var fileId = file.getId();// To get FileId of the file
fileIds.push(fileId);
var name = file.getName();
names.push(name);
var endResult, fromSearchingFiles;
fromSearchingFiles = SearchFiles(searchTerm);//Run SearchFiles function
//and pass the search term to the function
endResult = parseDataFromAppscript(fromSearchingFiles);
return endResult;
}
for (var i=0;i<names.length;i++){
Logger.log(names[i]);
Logger.log("https://drive.google.com/uc?export=download&id=" + fileIds[i]);
}
}
function doGet(e) {
var template = HtmlService.createTemplateFromFile('Index');
return template.evaluate()
.setTitle('Hello World')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
function processForm(searchTerm) {
var resultToReturn;
Logger.log('processForm was called! ' + searchTerm);
// here is where I would like to display results of searchterm.
resultToReturn = SearchFiles(searchTerm);
Logger.log('resultToReturn: ' + resultToReturn)
return resultToReturn;
}