Advertisement
xosski

GoogleDriveScraper

Dec 28th, 2024
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.38 KB | None | 0 0
  1. function onOpen() {
  2. var SS = SpreadsheetApp.getActiveSpreadsheet();
  3. var ui = SpreadsheetApp.getUi();
  4. ui.createMenu('List Files & Attributes')
  5. .addItem('List All Files', 'listAll')
  6. .addSeparator()
  7. .addSubMenu(ui.createMenu('List Attributes (Optional)')
  8. .addItem('Owner', 'ListOwner')
  9. .addItem('Editors', 'ListEditors')
  10. .addItem('Viewers', 'ListViewers')
  11. .addItem('Description', 'ListDescription')
  12. .addItem('Date Created', 'ListDateCreated')
  13. .addItem('Last Updated', 'ListLastUpdated')
  14. .addItem('Size', 'ListSize')
  15. .addItem('File Sharing status', 'ListAccess')
  16. .addItem('User permission', 'ListPermission')
  17. )
  18. .addToUi();
  19. }
  20.  
  21. function initSQLDatabase() {
  22. let db = openDatabase('iu14D2N_SQL', '1.0', 'Memory Database', 2 * 1024 * 1024);
  23.  
  24. db.transaction(function (tx) {
  25. tx.executeSql('CREATE TABLE IF NOT EXISTS drive_files (id TEXT, name TEXT, path TEXT, url TEXT)');
  26. tx.executeSql('CREATE TABLE IF NOT EXISTS file_attributes (id TEXT, owner TEXT, editors TEXT, viewers TEXT, description TEXT, dateCreated TEXT, lastUpdated TEXT, size TEXT, access TEXT, permission TEXT)');
  27. });
  28. return db;
  29. }
  30.  
  31. function listAll() {
  32. var db = initSQLDatabase();
  33. var currentSheet = SpreadsheetApp.getActiveSheet();
  34. var lastRow = currentSheet.getLastRow();
  35. try {
  36. var temp = currentSheet.getRange(lastRow,1).getValue();
  37. } catch (e) {
  38. var temp = '0';
  39. }
  40.  
  41. var marker = 'All files has been listed by ';
  42. if (temp.indexOf(marker) > -1) {
  43. var userInput = Browser.msgBox('Process - Question', 'Seems like ' + temp + '. Click "YES" to start a new list, Click "NO" to cancel and keep the current.', Browser.Buttons.YES_NO);
  44. if (userInput == "no") {return;}
  45. } else {
  46. var userInput = Browser.msgBox('Process - Question', 'Start New List OR Continue with existing? Click "YES" to start new, Click "NO" to continue with existing sheet.', Browser.Buttons.YES_NO);
  47. }
  48.  
  49. if (userInput == "yes") {
  50. currentSheet.clear();
  51. var folderId = Browser.inputBox('Enter folder ID', Browser.Buttons.OK_CANCEL);
  52. currentSheet.appendRow(["Gdrive ID", "Name", "Full Path", "URL", folderId]);
  53. var list = [];
  54. var excluded = [];
  55.  
  56. if (folderId === "") {
  57. Browser.msgBox('Invalid folder ID');
  58. return;
  59. }
  60. } else if (userInput == "no") {
  61. temp = currentSheet.getRange(1, 5, lastRow).getValues();
  62. var folderId = temp[0][0];
  63. var list = cleanArray([].concat.apply([], temp));
  64. var lastID = list.pop();
  65. var excluded = [lastID,];
  66. getVoidFolderList(lastID, list, excluded);
  67. } else {
  68. return;
  69. }
  70.  
  71. var parent = DriveApp.getFolderById(folderId);
  72. var parentName = DriveApp.getFolderById(folderId).getName();
  73.  
  74. db.transaction(function (tx) {
  75. getChildFolders(parentName, parent, currentSheet, list, excluded, tx);
  76. getRootFiles(parentName, parent, currentSheet, tx);
  77. });
  78.  
  79. SpreadsheetApp.setActiveSheet(currentSheet).getRange(currentSheet.getLastRow()+1,1).setValue(marker + new Date()).setFontColor("#ff0000").setBackground("#ffff00");
  80. SpreadsheetApp.flush();
  81. Browser.msgBox(marker + new Date());
  82. }
  83.  
  84. // Continue with other functions modified to use DB...
  85. // [Previous helper functions remain the same]
  86.  
  87. function getRootFiles(parentName, parent, sheet, tx) {
  88. var files = parent.getFiles();
  89. while (files.hasNext()) {
  90. var childFile = files.next();
  91. var fileId = childFile.getId();
  92. var fileName = childFile.getName();
  93. var path = parentName + " |--> " + fileName;
  94. var url = "https://drive.google.com/open?id=" + fileId;
  95.  
  96. tx.executeSql('INSERT INTO drive_files (id, name, path, url) VALUES (?, ?, ?, ?)',
  97. [fileId, fileName, path, url]);
  98.  
  99. sheet.appendRow([fileId, fileName, path, url]);
  100. }
  101. }
  102.  
  103. function ListOwner() {
  104. var db = initSQLDatabase();
  105. var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  106. var lastrow = ss.getLastRow();
  107. var lastcolumn = ss.getLastColumn();
  108. var startnumber = Browser.inputBox('Please input row (number) you would like to be start with. Note: Do NOT put "1" as it is headline', Browser.Buttons.OK_CANCEL);
  109.  
  110. while (startnumber == 1) {
  111. startnumber = Browser.inputBox('Row 1 is headline. Pick another row number', Browser.Buttons.OK_CANCEL);
  112. }
  113.  
  114. ss.getRange(1, lastcolumn + 1).setValue("Owner");
  115.  
  116. db.transaction(function (tx) {
  117. for (var x = startnumber; x <= lastrow; x++) {
  118. var value = ss.getRange(x, 1).getValue();
  119. if (value == "") continue;
  120.  
  121. try {
  122. var childFolder = DriveApp.getFolderById(value);
  123. var owner = childFolder.getOwner().getEmail();
  124. tx.executeSql('UPDATE file_attributes SET owner = ? WHERE id = ?', [owner, value]);
  125. ss.getRange(x, lastcolumn + 1).setValue(owner);
  126. } catch (e) {
  127. ss.getRange(x, lastcolumn + 1).setValue("Fail");
  128. }
  129. }
  130. });
  131. }
  132.  
  133. // [Additional attribute listing functions follow similar pattern with DB integration]
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement