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){
}
}
}
}