Search code examples
google-apps-scriptgoogle-sheetshyperlinkgoogle-drive-api

Names and links to images in multiple folders on Google Drive


Does anyone know how I can create a google sheet that is within multiple folders on Google Drive with columns of folder name, image name, and shareable link to image?

Using something similar to this


Solution

  • I believe your goal is as follows.

    • You want to retrieve the file list from the specific folder including the subfolders.

    In this case, how about the following sample script?

    Sample script:

    function sample() {
      const folderId = "###"; // Please set the top folder ID.
    
      const folder = DriveApp.getFolderById(folderId);
      const getFlieList = (folder, res = []) => {
        const files = folder.getFiles();
        while (files.hasNext()) {
          const file = files.next();
          res.push([file.getParents().next().getName(), file.getName(), file.getUrl()]);
        }
        const folders = folder.getFolders();
        while (folders.hasNext()) getFlieList(folders.next(), res);
        return res;
      }
      const header = ["folder", "name", "URL"];
      const values = [header, ...getFlieList(folder)];
      SpreadsheetApp.getActiveSheet().clear().getRange(1, 1, values.length, values[0].length).setValues(values);
    }
    

    Note:

    • From Is there a possibility of only getting the names and links for image files (.jpg) and ignoring other file types?, in this case, please modify the above script as follows.

      • From

          const files = folder.getFiles();
        
      • To

          const files = folder.getFilesByType(MimeType.JPEG);