Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function onOpen() {
- var SS = SpreadsheetApp.getActiveSpreadsheet();
- var ui = SpreadsheetApp.getUi();
- ui.createMenu('List Files & Attributes')
- .addItem('List All Files', 'listAll')
- .addSeparator()
- .addSubMenu(ui.createMenu('List Attributes (Optional)')
- .addItem('Owner', 'ListOwner')
- .addItem('Editors', 'ListEditors')
- .addItem('Viewers', 'ListViewers')
- .addItem('Description', 'ListDescription')
- .addItem('Date Created', 'ListDateCreated')
- .addItem('Last Updated', 'ListLastUpdated')
- .addItem('Size', 'ListSize')
- .addItem('File Sharing status', 'ListAccess')
- .addItem('User permission', 'ListPermission')
- )
- .addToUi();
- }
- function initSQLDatabase() {
- let db = openDatabase('iu14D2N_SQL', '1.0', 'Memory Database', 2 * 1024 * 1024);
- db.transaction(function (tx) {
- tx.executeSql('CREATE TABLE IF NOT EXISTS drive_files (id TEXT, name TEXT, path TEXT, url TEXT)');
- 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)');
- });
- return db;
- }
- function listAll() {
- var db = initSQLDatabase();
- var currentSheet = SpreadsheetApp.getActiveSheet();
- var lastRow = currentSheet.getLastRow();
- try {
- var temp = currentSheet.getRange(lastRow,1).getValue();
- } catch (e) {
- var temp = '0';
- }
- var marker = 'All files has been listed by ';
- if (temp.indexOf(marker) > -1) {
- 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);
- if (userInput == "no") {return;}
- } else {
- 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);
- }
- if (userInput == "yes") {
- currentSheet.clear();
- var folderId = Browser.inputBox('Enter folder ID', Browser.Buttons.OK_CANCEL);
- currentSheet.appendRow(["Gdrive ID", "Name", "Full Path", "URL", folderId]);
- var list = [];
- var excluded = [];
- if (folderId === "") {
- Browser.msgBox('Invalid folder ID');
- return;
- }
- } else if (userInput == "no") {
- temp = currentSheet.getRange(1, 5, lastRow).getValues();
- var folderId = temp[0][0];
- var list = cleanArray([].concat.apply([], temp));
- var lastID = list.pop();
- var excluded = [lastID,];
- getVoidFolderList(lastID, list, excluded);
- } else {
- return;
- }
- var parent = DriveApp.getFolderById(folderId);
- var parentName = DriveApp.getFolderById(folderId).getName();
- db.transaction(function (tx) {
- getChildFolders(parentName, parent, currentSheet, list, excluded, tx);
- getRootFiles(parentName, parent, currentSheet, tx);
- });
- SpreadsheetApp.setActiveSheet(currentSheet).getRange(currentSheet.getLastRow()+1,1).setValue(marker + new Date()).setFontColor("#ff0000").setBackground("#ffff00");
- SpreadsheetApp.flush();
- Browser.msgBox(marker + new Date());
- }
- // Continue with other functions modified to use DB...
- // [Previous helper functions remain the same]
- function getRootFiles(parentName, parent, sheet, tx) {
- var files = parent.getFiles();
- while (files.hasNext()) {
- var childFile = files.next();
- var fileId = childFile.getId();
- var fileName = childFile.getName();
- var path = parentName + " |--> " + fileName;
- var url = "https://drive.google.com/open?id=" + fileId;
- tx.executeSql('INSERT INTO drive_files (id, name, path, url) VALUES (?, ?, ?, ?)',
- [fileId, fileName, path, url]);
- sheet.appendRow([fileId, fileName, path, url]);
- }
- }
- function ListOwner() {
- var db = initSQLDatabase();
- var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
- var lastrow = ss.getLastRow();
- var lastcolumn = ss.getLastColumn();
- 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);
- while (startnumber == 1) {
- startnumber = Browser.inputBox('Row 1 is headline. Pick another row number', Browser.Buttons.OK_CANCEL);
- }
- ss.getRange(1, lastcolumn + 1).setValue("Owner");
- db.transaction(function (tx) {
- for (var x = startnumber; x <= lastrow; x++) {
- var value = ss.getRange(x, 1).getValue();
- if (value == "") continue;
- try {
- var childFolder = DriveApp.getFolderById(value);
- var owner = childFolder.getOwner().getEmail();
- tx.executeSql('UPDATE file_attributes SET owner = ? WHERE id = ?', [owner, value]);
- ss.getRange(x, lastcolumn + 1).setValue(owner);
- } catch (e) {
- ss.getRange(x, lastcolumn + 1).setValue("Fail");
- }
- }
- });
- }
- // [Additional attribute listing functions follow similar pattern with DB integration]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement