Search code examples
google-apps-scriptgoogle-sheetsgoogle-sheets-query

using a variable in searchFiles Google script


I have a third party program that pulls daily data into CSV files with the filename starting with the date it ran e.g. 17072017filename.csv I need a seperate spreadsheet that will take a date input from the user, which will then search through the files in drive until it matches the date on the CSV file. Once i find the file i want i can then use getFileID() and getRange() to copy various values from that CSV file. This is what i have at the moment (where cell A2 in the spreadsheet is where the user can type the date they want e.g. 17072017).

Issue seems to be that i can't pass the date (which will change everyday) as a variable in searchFiles(). Is this even possible? This is what i have so far

function myfunction()
{
    var inputDate = SpreadsheetApp.getActiveSheet().getRange("A2").getValue();
    var files = DriveApp.searchFiles('title contains 'inputDate'');
    while (files.hasNext()) 
    {
        var file = files.next();
        Logger.log(file.getName());
    }
}

Solution

  • How about a following modification?

    From :

    var files = DriveApp.searchFiles('title contains 'inputDate'');
    

    To :

    var files = DriveApp.searchFiles("title contains '" + inputDate + "'");
    

    If I misunderstand your question, I'm sorry.