Search code examples
imagegoogle-apps-scriptgoogle-sheetsurldata-extraction

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


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.

https://github.com/tanaikech/ImgApp#getsize

But I am very new to this scenario and wondering what exactly I should use in the script editor. If I follow the above I can't get the results.

Please help.

Thanks


Solution

  • When I saw your provided sample Spreadsheet, it seems that the Spreadsheet and your script are as follows.

    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: