#JP39 Menampilkan Url dan Informasi Semua File Pada Folder di Spreadsheet


Menampilkan Url dan Informasi Semua File Pada Folder di Spreadsheet

1. Buatlah Spreadsheet

2. Buatlah lembar kerja Apps Script dengan cara klik menu Ektensi lalu pilih Apps Script.


3.Terdapat file default bernama Code.gs

4. Copy dan pastekan script di bawah ini ke Code.gs

//by www.javabitpro.com
function onOpen() {
  var SS = SpreadsheetApp.getActiveSpreadsheet();
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Tampilkan List Files/Folders')
    .addItem('List Semua Files and Folders', 'listFilesAndFolders')
    .addToUi();
};

function listFilesAndFolders(){
  var folderId = Browser.inputBox('Masukkan folder ID', Browser.Buttons.OK_CANCEL);
  if (folderId === "") {
    Browser.msgBox('Folder ID Tidak Valid');
    return;
  }
  getFolderTree(folderId, true); 
};


function getFolderTree(folderId, listAll) {
  try {
    // Folder ID
    var parentFolder = DriveApp.getFolderById(folderId);
    
    // Initialise the sheet
    var file, data, sheet = SpreadsheetApp.getActiveSheet();
    sheet.clear();
    sheet.appendRow(["Full Path", "Nama File","Tipe" ,"Waktu", "URL", "Terakhir Update", "Deskripsi", "Ukuran File","Pemilik Email"]);
    
    // Get files and folders
    getChildFolders(parentFolder.getName(), parentFolder, data, sheet, listAll);
  } catch (e) {
    Logger.log(e.toString());
  }
};

// Get the list of files and folders and their metadata in recursive mode
function getChildFolders(parentName, parent, data, sheet, listAll) {
  var childFolders = parent.getFolders();
 
  // List folders inside the folder
  while (childFolders.hasNext()) {
    var childFolder = childFolders.next();
    var folderId = childFolder.getId();
    data = [ 
      parentName + "/" + childFolder.getName(),
      childFolder.getName(),
      "Folder",
      childFolder.getDateCreated(),
      childFolder.getUrl(),
      childFolder.getLastUpdated(),
      childFolder.getDescription(),
      childFolder.getSize()/1024,
      childFolder.getOwner().getEmail()
    ];
    // Write
    sheet.appendRow(data);
    
    // List files inside the folder
    var files = childFolder.getFiles();
    while (listAll & files.hasNext()) {
      var childFile = files.next();
      data = [ 
        parentName + "/" + childFolder.getName() + "/" + childFile.getName(),
        childFile.getName(),
        "Files",
        childFile.getDateCreated(),
        childFile.getUrl(),
        childFile.getLastUpdated(),
        childFile.getDescription(),
        childFile.getSize()/1024,
        childFile.getOwner().getEmail(),
      ];
      // Write
      sheet.appendRow(data);
    }
    // Recursive call of the subfolder
    getChildFolders(parentName + "/" + childFolder.getName(), childFolder, data, sheet, listAll);  
  }
};


5. Klik ikon Save lalu klik Jalankan/Run





6. Setelah eksekusi selesai, kembali pada spreadsheet. Pada menu di atas akan ada menu baru "Tampilkan List Files/Folders".


NB : Apabila tidak ada menu baru setelah apps script di jalankan, maka refresh/relaad halaman spreadsheetnya.

7. Klik menu "Tampilkan List Files/Folders" lalu isikan dengan ID Folder yang akan di tampilkan.


NB : ID Folder yang digunakan adalah Folder paling luar (Bukan folder di dalam folder)

SELESAI!!!






Previous Post Next Post

Promo