作為一個初學者,我編寫了一個腳本,將所有我的Google Drive資料夾填入Google表格中。即使腳本可以工作,但我遇到了錯誤:超過最大執行時間
,而我的資料夾結果清單還沒有完成。
我無法弄清楚如何處理/修復執行逾時。我已經搜尋了多個來源,但沒有找到可以幫助我加快腳本速度的東西。
我的期望是腳本將超快地執行,並且「可能」的數千個資料夾清單將在最多1分鐘內填充。
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); } }
包含Apps腳本的Google表格在此。
P粉7757887232023-09-08 00:48:56
您的應用程式腳本會在每個資料夾或子資料夾出現時寫入電子表格。相反,您應該將資訊收集到一個陣列中,然後將整個陣列寫入電子表格。嘗試這樣做,儘管一開始有些單元格的值是物件而不是字串,所以在決定要捕獲這些物件的哪個屬性之前,它不會起作用。我只是使用了toString()
。
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); // 注意我创建了一个名为folders的数组,并将其传递给listFolders,而不是newSheet let folders = [["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, "", folders, 1); // 完成后复制到电子表格 newSheet.getRange(1,1,folders.length,folders[0].length.setValues(folders); } function listFolders(folder, parentName, folders, 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().toString(); var sharingPermission = folder.getSharingPermission().toString(); var mimeType = "folder"; var starred = folder.isStarred(); var trashed = folder.isTrashed(); var dateCreated = folder.getDateCreated(); var lastUpdated = folder.getLastUpdated(); folders.push([level, parentName, folderName, description, folderId, url, owner, sharingAccess, sharingPermission, mimeType, starred, trashed, dateCreated, lastUpdated]); var subFolders = folder.getFolders(); level++; while (subFolders.hasNext()) { var subFolder = subFolders.next(); listFolders(subFolder, folderName, folders, level); } }
參考資料