简体   繁体   中英

Google Drive Mapping Script returning error

I'm currently developing a short little script whose end result is that it returns the file and folder structure of my Google Drive or some folder within it. I have a basic framework taking shape that is beginning to function reasonably as it should:

function folderMap() {
var rootFolder=DriveApp.getFolderById("arbitraryFolderId");
var rootFiles=[];
var rootFolders=[];
var root=[rootFolders,rootFiles];
var i;  
getChildren(rootFolder,root);
Logger.log(root);
 if(rootFolders!==null){
  for(i=0;i<rootFolders.length;i++){
   var tempName=rootFolders[i].getName();
   eval("var "+tempName+"Object=nextObject("+tempName+");");
   getChildren(rootFolders[i],eval(tempName+"Object"));
   Logger.log(tempName); 
   }
 }
}

function getChildren(parent,parentObject){
var folders= parent.searchFolders('');
while (folders.hasNext()) {
 var folder = folders.next();
 parentObject[0].push(folder); 
}
var files= parent.searchFiles('');
while (files.hasNext()) {
var file = files.next();
parentObject[1].push(file); 
}  
}

function nextObject(name){
var dynamic= name;
eval("var "+dynamic+"Folders=[];");
eval("var "+dynamic+"Files=[];");
var dummy=eval("var "+dynamic+"=["+dynamic+"Folders,"+dynamic+"Files];");
return dummy;  
}

This is, however, returning the error SyntaxError: Missing ; before statement. (line 12, file "Code") SyntaxError: Missing ; before statement. (line 12, file "Code") SyntaxError: Missing ; before statement. (line 12, file "Code") I have tried putting additional semi-colons in prior to this line, adjusting parentheses and generally fiddling with the code but nothing I do seems to solve this error.

Is there something very simply, staring me in the face that I'm missing?

Any help would be greatly appreciated.

Thank you

EDIT: For clarification, arbitraryFolderId in line 2 is replaced by the string Id for the folder, however, my works domain administrator have asked we do not publicly share file or folder Id's; regardless of their sharing permissions.

I tried your code, and it seems that you need to provide a valid folder ID instead of arbitraryFolderId in line 2 to make that error gone. I hope it helps you ;)

So I'm not entirely sure why this fixed the issue I was having, but I changed the way the return statement returned the result of the nextObject function and it has rectified the issue I was having. The new working code is as below:

function folderMap() {
var rootFolder=DriveApp.getFolderById("folderId");
var rootFiles=[];
var rootFolders=[];
var root=[rootFolders,rootFiles];
var i;  
getChildren(rootFolder,root);
if(rootFolders!==null){
for(i=0;i<rootFolders.length;i++){
  var tempName=rootFolders[i].getName();
 eval("var "+tempName+"Object=nextObject('"+tempName+"');");
 getChildren(rootFolders[i],eval(tempName+"Object")); 
}
}
}

function getChildren(parent,parentObject){
var folders= parent.searchFolders('');
while (folders.hasNext()) {
var folder = folders.next();
parentObject[0].push(folder); 
}
var files= parent.searchFiles('');
while (files.hasNext()) {
var file = files.next();
parentObject[1].push(file); 
}   
}

function nextObject(name){
var dynamic=name
eval("var "+name+"Folders=[];");  
eval("var "+name+"Files=[];");
return eval("["+name+"Folders,"+name+"Files];"); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM