As a beginner, I wrote a script to populate all my Google Drive folders into Google Sheets. Even though the script works, I'm getting Error: Maximum execution time
exceeded, and my folder results list isn't complete yet.
I can't figure out how to handle/fix execution timeouts. I've searched multiple sources but haven't found anything that can help me speed up my script.
My expectation is that the script will execute super fast and the list of "possibly" thousands of folders will be populated in 1 minute at most.
function generateFolderList() { var rootFolder = DriveApp.getRootFolder(); var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); var sheetName = Utilities.formatDate(new Date(), "GMT+1", "yyyy-MM-dd HH:mm:ss"); var newSheet = spreadsheet.insertSheet(sheetName); newSheet.getRange("A1:N1").setValues([["structure_level", "parent_folder", "folder_name", "folder_description", "folder_id", "folder_url", "owner_email", "sharing_access", "sharing_permissions", "mime_type", "starred_status", "trashed_status", "date_created", "last_updated"]]); listFolders(rootFolder, "", newSheet, 1); } function listFolders(folder, parentName, sheet, level) { var folderName = folder.getName(); var description = folder.getDescription(); var folderId = folder.getId(); var url = folder.getUrl(); var owner = folder.getOwner().getEmail(); var sharingAccess = folder.getSharingAccess(); var sharingPermission = folder.getSharingPermission(); var mimeType = "folder"; var starred = folder.isStarred(); var trashed = folder.isTrashed(); var dateCreated = folder.getDateCreated(); var lastUpdated = folder.getLastUpdated(); var rowData = [level, parentName, folderName, description, folderId, url, owner, sharingAccess, sharingPermission, mimeType, starred, trashed, dateCreated, lastUpdated]; sheet.appendRow(rowData); var subFolders = folder.getFolders(); level++; while (subFolders.hasNext()) { var subFolder = subFolders.next(); listFolders(subFolder, folderName, sheet, level); } }
Google Sheets with Apps Script is here.
Your application script writes to the spreadsheet as each folder or subfolder appears. Instead, you should collect the information into an array and then write the entire array to a spreadsheet. Try this, although initially some of the cells' values are objects rather than strings, so it won't work until you decide which property of those objects you want to capture. I just used
toString()
.References