Home > Software design >  Error retrieving image from URL with SpreadsheetApp.newCellImage() builder
Error retrieving image from URL with SpreadsheetApp.newCellImage() builder

Time:07-03

My application is trying to insert images to google drive sheet using google app script.

It works fine... but it hang up intermittently with the response error from google script:

Exception: Error retrieving image from URL or bad URL: https://drive.google.com/uc?id=1uvjg9_ZZg2sI5RYbPMEn6xXJhhnwuFyq&export=download.

The code is :

    var fpm_mon_image = file_from_folder(id_folder_images , image_AR ) ;   // get "image_AR" from folder
    var url_mon_image = fpm_mon_image.getDownloadUrl() ; 
    var image = SpreadsheetApp.newCellImage().setSourceUrl(url_mon_image).setAltTextTitle(titre).toBuilder().build() ;    
     Utilities.sleep(1000);   // for testing ...
     SpreadsheetApp.flush();
     Utilities.sleep(1000); 
     var rangeIma= fpm_sp.getRange(scal_li_SP  1, 2) ; 
     rangeIma.setValue(image).setVerticalAlignment('bottom')  ; // stop here with error above 

It works fine 5, 10 times then it hang up 2, 3, 5 times and then works fine again.... (I start loosing my hairs ;-))

I tried :

 var srcfile = DriveApp.getFileById(id_mon_image);
 srcfile.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
 var image = fpm_sp.insertImage(srcfile.getBlob(), my_col , my_row); 

but the image in not inserted in a cell...

Could you help please ? Many thanks.

CodePudding user response:

Unfortunately, I cannot replicate your situation of Exception: Error retrieving image from URL or bad URL:. So, although I'm not sure about your actual situation, how about the following modification?

Modified script:

Before you use this script, please enable Drive API at Advanced Google services.

var fileId = "###"; // Please set the file ID of your image.

var url = Drive.Files.get(fileId).thumbnailLink.replace(/\=s. /, "=s512");
var title = "sample title";
var image = SpreadsheetApp.newCellImage().setSourceUrl(url).setAltTextTitle(title).build();
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange("A1").setValue(image);
  • When this script is run, a thumbnail link is retrieved from the image file using Drive API. And, the image size of the image is changed. And, the image is put to the cell "A1" of the active sheet.

  • In this case, if the issue of image size occurs, this issue can be removed. And, if the issue of sharing the file occurs, this issue can be also removed. By this, I thought that your issue might be able to be removed.

  • Related