This is the same question asked here but I'm unable to get the solution to work. I'm sorry to ask such a dumb question.
I need to search for files in a folder based on a cell value.
function aFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('mysheet');
var folder = DriveApp.getFolderById('1VOnXdSd12XMCMedf4gwdOl8g_CL7VeEYo');
var cell = sheet.getRange("A1");
var value = cell.getValue();
var files = folder.searchFiles("name contains '" + value + "'");
while (files.hasNext()) {
var file = files.next();
Logger.log(file.getName);
}
}
I get
Error
Exception: Invalid argument: q
on the start of the while loop. I tried making my query string in a single variable like:
var searchstring = "\"name contains '" + value + "'\"";
Logger.log(searchstring);
var files = folder.searchFiles(searchstring);
That particular searchstring logs as:
"name contains '20210328-0003-00'"
20210328-0003-00 is the value of the particular cell where I'm getting the value.
I also tried the following variations:
var searchstring = "'name contains \"" + value + "\"'";
//produces 'name contains "20210328-0003-00"'
var searchstring = "\"name contains \"" + value + "\"\"";
//produces "name contains "20210328-0003-00""
var searchstring = "'name contains '" + value + "''";
//produces 'name contains '20210328-0003-00''
....and a few others. Same result for all.
This might not be relevant, but I tried using getFilesByName like:
var files = folder.getFilesByName('*' + value + '*');
That doesn't crash, but it also doesn't find any of the files that I can see with my own eyes are right there.
I'm probably doing something stupid. I'm probably stupid. Please don't hurt me.