0
votes

I use the following script to extract the image name and image URL of all the images inside a google drive folder. It extracts this info into a google sheets doc. Is it possible to add the image dimensions (image height and width) into the script so it pulls into the doc aswell?

Script:

// replace your-folder below with the folder for which you want a listing
function listFolderContents() {
  var foldername = ‘FolderName1’;
  var folderlisting = 'listing of folder ' + foldername;
  
  var folders = DriveApp.getFoldersByName(foldername)
  var folder = folders.next();
  var contents = folder.getFiles();
  
  var ss = SpreadsheetApp.create(folderlisting);
  var sheet = ss.getActiveSheet();
  sheet.appendRow( ['name', 'link'] );
  
  var file;
  var name;
  var link;
  var row;
  while(contents.hasNext()) {
    file = contents.next();
    name = file.getName();
    link = file.getUrl();

    sheet.appendRow( [name, link] );     
  }  
};
2

2 Answers

2
votes

Explanation:

Your goal is to retrieve the width and the height of the pictures that are stored in a specific folder in Google Drive.

  • This info is located in imageMediaMetadata in the properties imageMediaMetadata.width and imageMediaMetadata.height.

  • To retrieve this metadata you need to enable the Drive API and pass the fileId of your file as an argument:

    Drive.Files.get(fileId).imageMediaMetadata;
    
  • Also store the data in an array and set the values directly to the sheet so you won't have to use appendRow iteratively which is very inefficient.

Last but not least, it is a good idea to check whether the file is an image regardless of each extension (jpeg,png). Otherwise, you will get an error if you the folder contains other types of files (spreadsheet documents etc.).

You can use includes() to check if the getMimeType contains the word image:

file.getMimeType().includes("image")

and only for these files, find the width and height.

Solution:

function listFolderContents() {
  var foldername = 'FolderName1';
  var folderlisting = 'listing of folder ' + foldername;
  var folders = DriveApp.getFoldersByName(foldername);
  var folder = folders.next();
  var contents = folder.getFiles();
  var ss = SpreadsheetApp.create(folderlisting);
  var sheet = ss.getActiveSheet();
  var data = [['name', 'link', 'width', 'height']];

  while(contents.hasNext()) {
    let file = contents.next();
    if( file.getMimeType().includes("image") ){
      let name = file.getName();
      let link = file.getUrl();
      let fileId = file.getId();
      let dims = Drive.Files.get(fileId, {supportsAllDrives: true}).imageMediaMetadata
      let width = dims.width;
      let height = dims.height;

    data.push( [name, link, width, height] );
    }     
  }  
  sheet.getRange(1,1,data.length,data[0].length).setValues(data);
};

Be careful!

Make sure to enable Drive API from:

  • Resources => Advanced Google Services (Legacy editor)

  • Services (New Editor)

0
votes

You may use the ImgApp library to retrieve the width and height of any image file in Google Drive.

var blob = DriveApp.getFileById(fileId).getBlob();
var res = ImgApp.getSize(blob);
var width = res.width;
var height = res.height;