1

So this function gets a folder, gets the subfolders, pushes some information to an array and then pastes the array into my spreadsheet.

It fails at a weird point about 2-5% of the time, and the attempts I have made to error catch it earlier just don't work. why/how!

function getFileCount(gdrive,level,fullpath){
  var folder = DriveApp.getFolderById(gdrive);
  var fileCount = 0;


  //append the subfolders to the sheet 
  var folders = folder.getFolders();
  var result = [];
  while (folders.hasNext()){
    var folder = folders.next();
    var path = fullpath + " > " +folder.getName();
    result.push([(level+1),folder.getId(), path,"?"]);
  }
  if (result == []) return fileCount;
  if (result == undefined) {console.log("Why the hell is it undefined?"+folder.getName()); return fileCount;}
  var sh = SpreadsheetApp.getActiveSpreadsheet();
  var ss = sh.getSheetByName("FoldersPlusCounts");
  var end = ss.getLastRow()+1;  
  try{ss.getRange(end, 1,result.length, result[0].length).setValues(result);}catch(e){console.log(e+" "+gdrive+" ["+result+"]");return -1;}
  if (result.length > 0) SpreadsheetApp.flush();

  return fileCount;
}

the try catch statement is where it fails saying:

TypeError: Cannot read property 'length' of undefined 1dmDV2qdnU2Qqrg6mr-sxD8JaQncTUNqn []

This doesn't occur every time the folder has no files, or every time it has no folders, I don't think.

So how can result sometimes be undefined, when it is clearly defined as result = [], and if it is undefined, why isn't it being captured by the "if result == undefined" line?

Some lines removed for clarity (just the file counting stuff), I wasn't using result up in that part anyway.

J. G.
  • 1,922
  • 1
  • 11
  • 21
  • Your error probably stems from the 4th argument that you pass to your `getRange()` ie. `result[0].length`. It could be that the 0th element in your `result` array does not have a `length` property. – TheAddonDepot Oct 30 '19 at 21:04
  • Ok, I thought to check the length of the array in general and that seeems to be fixing my problem. Why doesn't if (result == []) work? – J. G. Oct 30 '19 at 21:10
  • 2
    In your script, it's `var result = [];`. And `result` is compared with `[]`. At Javascript, in this case, the object is compared as the reference. By this, `result == []` is always `false`. When you want to confirm whether `result` is `[]`, how about modifying to `result.length == 0`? – Tanaike Oct 30 '19 at 22:11
  • Did you mean to do something with `fileCount`? – Cooper Oct 30 '19 at 22:34
  • 1
    You have two declarations for `folder`. `var folder=DriveApp.getFolderById(gdrive);//here var fileCount=0; var folders=folder.getFolders(); var result=[]; while (folders.hasNext()){ var folder = folders.next();//and here` – Cooper Oct 30 '19 at 22:41
  • yes, changing to check for result.length == 0 fixed it for me, Tanaike I didn't know that (result == []) will always be false – J. G. Oct 30 '19 at 23:00
  • Tanaike can you make your answer the answer? I'm sure coopers works as well but yours was the change that fixed it. – J. G. Oct 30 '19 at 23:17
  • Thank you for replying. I posted an answer. Could you please confirm it? – Tanaike Oct 30 '19 at 23:32

2 Answers2

3

In your script, result is declared as var result = [];. And result is compared with [].

At Javascript, in this case, the object is compared as the reference. By this, result == [] is always false. In order to confirm whether result is [] which is empty, how about the following modification?

From:

result == []

To:

result.length == 0

or

!result.length

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
0

This worked for me:

function getFolderCount(){
  var fldr=DriveApp.getFolderById('folderId');
  var subfldrCount=0;
  var subfolders=fldr.getFolders();
  var result=[];
  while (subfolders.hasNext()){
    var subfolder=subfolders.next();
    result.push([++subfldrCount,subfolder.getName(),subfolder.getId()]);
  }
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName("FoldersPlusCounts");
  sh.getRange(sh.getLastRow()+1, 1,result.length, result[0].length).setValues(result);
}
Cooper
  • 59,616
  • 6
  • 23
  • 54