Home > OS >  Extract width and height from image links via ImgApp Google Sheet Library
Extract width and height from image links via ImgApp Google Sheet Library

Time:06-15

I want to extract the dimensions of the image from the URL in google Sheet. Found this Library that does exactly what I am after.

enter image description here

function myFunction() {

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

}

Modification points:

  • In this case, you put a custom function of =myFunction(A2) to a cell. But your script doesn't retrieve the value.
  • At the custom function, DriveApp.getFileById(fileId) cannot be used because of the limitation.
  • Your script doesn't return the values.

When these points are reflected in the script for achieving your goal, it becomes as follows.

Modified script:

Please replace your current script with the following script and save the script. And, please put a custom function of =myFunction(A2) to a cell. By this, the width and height are returned.

function myFunction(url) {
  const blob = UrlFetchApp.fetch(url).getBlob();
  const { width, height } = ImgApp.getSize(blob);
  return [[width, height]];
}

Testing:

When this modified script is used for your sample Spreadsheet, it becomes as follows.

enter image description here

References:

  • Related