3

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());
    }
}
Komal12
  • 3,340
  • 4
  • 16
  • 25
Gramps
  • 35
  • 1
  • 5

1 Answers1

3

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.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • That seems to work fine. What is the 'plus' doing for the variable inputDate? I think im getting really confused with single quotes and double quotes here, but thanks that works for me – Gramps Jul 18 '17 at 04:13
  • @Gramps Welcome. Thank you, too. Using ``+``, the string of ``inputDate`` and ``title contains ''`` are summed. – Tanaike Jul 18 '17 at 04:42
  • Why don't you have to escape the ' in those strings i.e. "title contains\'" + inputDate + "\'" ? – Robert M. Apr 13 '23 at 21:51