I have a problem with my script, I would like it to search all folders and subfolders for files with this word in them. But it only stops at the first folder. Thank you for your help
function doGet(){
return HtmlService.createHtmlOutputFromFile('search').setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}
function idevlupSearch(sparams) {
if (sparams.length==0){
return `<em style='color:blue'> Champs de saisie vide`;
};
//Folder start
var folderName = "0-CentreDeRessources" ;
var listSearch = [];
var folder = DriveApp.getFoldersByName(folderName);
while (folder.hasNext())
{
Logger.log(folder);
var files = folder.next().searchFiles('title contains "' sparams '" or fullText contains "' sparams '"');
while (files.hasNext())
{
var file = files.next();
listSearch.push("<li> <a href='" file.getUrl() "'>" file.getName() "</a>" file "</li>");
}
}
Logger.log(listSearch);
if (listSearch.length > 0) {
listSearch.unshift("<ul>");
listSearch.shift("</ul>");
return listSearch;
}else{
return `<em style='color:red'> Pas de document trouvé !`;
}
}
CodePudding user response:
I believe your goal is as follows.
- From the discussions in the comment, you don't want to create the path including all subfolder names. You just want to search the files under the subfolders and want to retrieve the searched filenames.
In this case, how about the following modification?
Modified script:
function idevlupSearch(sparams) {
if (sparams.length == 0) {
return `<em style='color:blue'> Champs de saisie vide`;
};
var folderName = "0-CentreDeRessources";
var folder = DriveApp.getFoldersByName(folderName).next();
var searchFlies = (folder, res = []) => {
var files = folder.searchFiles(`(title contains '${sparams}' or fullText contains '${sparams}') and trashed=false`);
while (files.hasNext()) {
var file = files.next();
res.push({name: file.getName(), url: file.getUrl()});
}
var folders = folder.getFolders();
while (folders.hasNext()) searchFlies(folders.next(), res);
return res;
}
var listSearch = searchFlies(folder);
return listSearch.length > 0 ? listSearch.reduce((s, {name, url}) => s = `<li><a href='${url}'>${name}</a></li>`
, "<ul>") "</ul>" : `<em style='color:red'> Pas de document trouvé !`;
}
Note:
- I cannot understand that how the function
idevlupSearchis run. But, in your script, Web Apps is used. In this case, when you modified the Google Apps Script, please modify the deployment as a new version. By this, the modified script is reflected in Web Apps. Please be careful this. - You can see the detail of this in the report of "Redeploying Web Apps without Changing URL of Web Apps for new IDE".
About some unclear points, I guessed as follows.
- I couldn't understand
fileof</a>" file "</li>in your script. Because in your script,fileis an object of Class File. So in this modification, I modified to<li><a href='${url}'>${name}</a></li>. - I couldn't understand whether there are several same folder names of "0-CentreDeRessources". So in this modification, I guessed that the folder name of "0-CentreDeRessources" is only one folder in your Google Drive.
- I couldn't understand the output from the function
idevlupSearch. In your script, a text or an array is returned from the functionidevlupSearch. But I cannot know about the function which callsidevlupSearch. So in this modification, a text is returned.
If my modification was not in the direction you expect, please modify the above script for your actual situation.
