0
votes

I have this script that runs very slowly and always times out on its 15 minuet triggers. It is basically the example script that Google have for displaying the contents of a drive folder in a portal site (https://developers.google.com/apps-script/articles/embedding_docslist_in_sites), but with a bit at the beginning so that it only really runs during office hours and to send an email if there is a new file. There are only 10-15 max files in the drive so shouldn't take too long.

I am doing this because our fax machine saves an incoming fax to a folder on the server, I then sync that with Google drive and I want to share that within the organisation and send an alert email.

I put in some logger entries showing the time at the end of each loop and it varied from between 30 seconds per loop to 2-3 minuets. Any ideas

function officeHours(){
var nowH=new Date().getHours();
var nowD=new Date().getDay();
if(nowH>18||nowH<8||nowD==6||nowD==0){return}{ 
  var done = false; 
  var emaillist = "[email protected]";
  var myDate=new Date();
  myDate.setMinutes(myDate.getMinutes()-15); 
  while(!done){
    try{
      var files = DocsList.getFolderById("0Byg20FZrPmcHUUhlOGVrZlRuM28").getFiles();     
      var page = SitesApp.getPageByUrl("https://sites.google.com/a/ringtail.co.uk/portal/fax-list");     
      var listItems = page.getListItems();     
      for(a in listItems){       
        listItems[a].deleteListItem();     
      }     
      for(i in files){     
        var title = "<a href=\'"+files[i].getUrl()+"\'>"+ files[i].getName() +"</a>";     
        var lastUpdatedinctime = files[i].getLastUpdated();     
        if (lastUpdatedinctime > myDate){
          MailApp.sendEmail(emaillist, "New Fax", "",{htmlBody:"<br/><br/>A new fax has arrived<br/><br/><a href='"+files[i].getUrl()+"'>Click to view</a><br/><br/><a href='https://sites.google.com/a/ringtail.co.uk/portal/fax-list'>Click to see full fax list"});
        }
        page.addListItem([title, files[i].getType(), Utilities.formatDate(files[i].getLastUpdated(), "GMT", "yyyy-MM-dd")]);
      }    
      done = true;    
    }     
    catch(e){ 
    } 
  }
 }
}
2

2 Answers

1
votes

I see you have put your entire code in a while loop. There is a chance that you are hitting an exception and the while loop never ends. Have you tried logging the exception ? If so, did you see any exceptions being thrown ?

In addition, there are multiple calls made to getType(), getUrl() and getLastUpdated(). If you cache these values in a local variable the first time you want to use them, then your script will run faster. Having said that the unending while loop looks to be the reason of the time out.

0
votes

I found this quite interesting and Srik suggestions where quite pertinents so I gave it a try, using spreadsheet as 'storage device' and it executes in a couple of seconds ;-) I show below my project that you should re-modify to suit your needs with the right urls and column setup. (I added some comments to make it clear (hopefully))

    function officeHours(){
      var sh = SpreadsheetApp.getActiveSheet();
      var emaillist = "[email protected]";
          var files = DocsList.getFolderById("xxxxxxxxxxxxxxxxxxxxxxx").getFiles();  
          var page = SitesApp.getPageByUrl("https://sites.google.com/site/appsscriptexperiments/home/xxxxxxxx");     
          var listItems = page.getListItems();  
          Logger.log(listItems.length+"  =?  "+files.length )
          var siteList=new Array();
      for(a in listItems){ 
    //        Logger.log(listItems[a].getLastUpdated())
      siteList.push([listItems[a].getLastUpdated(),listItems[a].getValueByIndex(3)]);// choose what you save on the sheet  
          }     
      sh.getRange(1,1).setValue('Site List')  
      sh.getRange(2,1,siteList.length,siteList[0].length).setValues(siteList)

// ***************************************
        if(listItems.length<files.length){;// new file added in doc list , that is the main condition !!!
// ***************************************
          var fileList=new Array()
                for(var n in files){
                  fileList.push([files[n].getLastUpdated(),files[n].getName(),files[n].getId()]);// choose what you save on the sheet
                    }
          Logger.log(n) ;// n is the last index
      fileList.sort(function(x,y){
      var xp = x[0];// choose on which item to sort so that the last item is the last added to the folder
      var yp = y[0];
      return xp == yp ? 0 : xp < yp ? -1 : 1;// sort mode
    });
      sh.getRange(1,3).setValue('File List')  
      sh.getRange(2,3,fileList.length,fileList[0].length).setValues(fileList)
     //       MailApp.sendEmail(emaillist, "New Fax", "",{htmlBody:"<br/><br/>A new fax has arrived<br/><br/><a href='"+fileList[n][2]+"'>Click to view</a><br/><br/><a href='https://sites.google.com/a/ringtail.co.uk/portal/fax-list'>Click to see full fax list"});
            page.addListItem([fileList[n][0],'new',fileList[n][1],"<a href=\'"+fileList[n][2]+"\'>"+fileList[n][0]+"</a>"]);
                }   
       }    

note that I removed the 'timer trick' in this test, you should add it as it was.(happy to see it served you well ;-)