Pierre-Brice LEVY
Pierre-Brice LEVY

Reputation: 1

G-script : List and Filter G-Drive Files from Variables Stored in a G-Sheet

I try to list files from G-Drive with filters. Filters variables are stored in a G-Sheet.

Here comes the code. It does not work. But it does when I put text string instead of variables.

Thanks a lot,

function Lister_fichiers() {
    // Log the name of every file in the user's Drive that modified after date = 'Var!'A1,
    // whose name contains "MARQUE = 'Var!'B1".
    var app = SpreadsheetApp;
    var classeur = app.getActiveSpreadsheet();

    const Formsheet = classeur.getSheetByName('Var');
    const CsvM = classeur.getSheetByName('CsvM');

    var Date1 = Formsheet.getRange('A1').getValue();
    var Date2 = new Date(Date.UTC(Date1));

    var Marque = Formsheet.getRange('B1').getValue();
    var Pays = Formsheet.getRange('C1').getValue();

    // this line here does not work and causes an error
    var sffiles = DriveApp.searchFiles("modifiedDate > '"+Date2+"' and title contains '"+Marque+"' "); 
}

Also, it's without errors but still not working without the date filter.

In the sheet, A1 = "2019-01-01" and B1 = "Marque1"

// Lines below works fine

// var sffiles = DriveApp.searchFiles('modifiedDate > "2019-01-01" and title contains "Marque1" and mimeType = "application/vnd.google-apps.spreadsheet" and trashed=false');


while (sffiles.hasNext()){
        var sffile = sffiles.next();
        var sfname =  sffile.getName();
        var sfdate =  sffile.getLastUpdated(); 
        var sfsize =  sffile.getSize();
        var sfurl =  sffile.getUrl();
        var fileParents = sffile.getParents();
          while (fileParents.hasNext()) {
            var sffolder = fileParents.next();
             Logger.log(sffolder.getName());
          }
        var sfid =  sffile.getId();
  Logger.log(sffile.getId());
  Logger.log(sffile.getName());

  var sflist = [sfname,sfdate,sfsize,sfurl,sfid,sffolder];
  CsvM.appendRow(sflist);
  }
CsvM.appendRow(["A-Name", "Date", "Size", "URL", "ID", "Dossier Parent"]);
    CsvM.sort(1);
}

Upvotes: 0

Views: 71

Answers (1)

rsomji
rsomji

Reputation: 150

What is the exact error message? I suspect the issue is with this line:

var Date2 = new Date(Date.UTC(Date1));

If var Date1 A1 is "2019-01-01", you should just be able to do:

var Date2 = new Date(Date1)

Which gives you the datetime object.

I think the confusion is that you can pass a string like "2019-01-01" to new Date() [0] but not to Date.UTC() [1]

[0] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC

Upvotes: 1

Related Questions