Vasim
Vasim

Reputation: 3143

GAS Loop goes indefinite

Problem:- I need to get Name and ID of the latest created document from some Google Drive folders.

Solution:- I wasn’t able to find an easy way to get it, hence, created a loop for files within the folder to get max date - then loop again in folder to match the date and Log the name and date – the below code (when commented as mentioned) works properly for the solution.

Instead of rewriting the whole code with just a different folder name, I tried creating a loop for folder name as well.

However app script goes into infinite loop giving out the Maximum execution time limit message. Any help appreciated.

function Get_lastestdate() {

  //Define Spreadsheet and sheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("PEOPLE");

  //comment from here to work properly
  for (var i =1; i < 4; i++) {

  if (i = 1) { var foldername = "Folder1"}
  if (i = 2) { var foldername = "Folder2"}
  if (i = 3) { var foldername = "Folder3"}
  // till here

  //Define folder and files
  var folder = DocsList.getFolder(foldername); //set folder name in case commenting.
  var lastfile = folder.getFiles();

  //Make a blank array
  var noofdaysarray = []
  var yeararray = []

  //Loop to get all No of days in a year & the year
  for (var i in lastfile) {
  noofdaysarray.push(Utilities.formatDate(lastfile[i].getDateCreated(),"GMT","D"));
  yeararray.push(Utilities.formatDate(lastfile[i].getDateCreated(),"GMT","y"));
  }

  //Get the max date from date and year
  var largestdate = Math.max.apply(Math, noofdaysarray);
  var largestyear = Math.max.apply(Math, yeararray);
  //Get maximum available date
  var matchcriteria = largestdate + largestyear

  //Again loop for matching criteria with the actual date uploaded
  for (var i in lastfile) {
  var lastdate = Utilities.formatDate(lastfile[i].getDateCreated(),"GMT","D");
  var lastyear = Utilities.formatDate(lastfile[i].getDateCreated(),"GMT","y");
  var wholedate = parseInt(lastdate) + parseInt(lastyear); //parseInt is for converting text to number

  //Get doc name if both dates matches
  if (wholedate == matchcriteria) {
  Logger.log(lastfile[i].getId());
  Logger.log(lastfile[i].getName());
  }
  }
 } //comment this as a part of loop

}

Between:- If there's an easier way to do it, please let me know.

Upvotes: 0

Views: 172

Answers (2)

jkhouw1
jkhouw1

Reputation: 7350

Dont use "i" in both inner and outer loops. that will be a problem.

wouldn't it be easier to do something like this so you only do one loop?

var mostRecentDate = new Date(2000,01,01);
var fileName="";
var fileId="";

for(var i = 1; i<5; i++){

   var folder = DocsList.getFolder('Folder'+i);
   var files = folder.getFiles();
   for(var j in files) {
      if(files[j].getDateCreated()>mostRecentDate){  //or getLastUpdated
         mostRecentDate=files[j].getDateCreated();
         fileName=files[j].getName();
         fileId=files[j].getId();
       }

    }
}
Logger.log("File: " + filename + " Id: " + fileId + " Created: " + mostRecentDate);

You may need to do paging if you have a huge number of files & folders to iterate through.

Upvotes: 1

br araujo
br araujo

Reputation: 1872

You can use DocsList to retrieve the last modified files. you told that you want the last created but maybe the last modified can be usefull. Check the code bellow:

function findLastModified() {
     //Retrieve folder
     var folder = DocsList.getFolderById("0B0kQD4hSd4KASUJKb2cya0NET1U");

     //Ask for 5 files
     var numberOfFiles = 5;

     //The first parameter is the search query, in this case we want all files
     var filesResult = folder.findForPaging("", 5);

     //By Default they will be sorted by last modified date
     var files = filesResult.getFiles();

     Logger.log("Found "+files.length+" files");

     //Iterate
     for(var x in files) {
       var file = files[x];
       Logger.log(file.getLastUpdated());
      }
    }

Live version here.

Upvotes: 1

Related Questions