Thursday, 24 August 2023

Error retrieving image from URL in Google Apps Script but only on some spreadsheets

I have Google Sheets spreadsheets that perform a lot of (small file-size) image retrieval using SpreadsheetApp.newCellImage() via code similar to the below:

function myFunction() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  const image = SpreadsheetApp.newCellImage()
    .setSourceUrl("https://static-00.iconduck.com/assets.00/perspective-dice-random-icon-469x512-mm6xb9so.png")
    .setAltTextTitle("Image")
    .build();

  sheet.getRange("A1").setValue(image);
}

Just recently I noticed that these spreadsheets would get an error when trying to apply the image to a cell:

Exception: Error retrieving image from URL or bad URL

This would happen with ANY image I try and on ANY cell. It would fail at the setValue(image) call.

Here's the kicker. If I tried running the exact function above in a NEW spreadsheet, it would work with no issues. But running it in my existing spreadsheets would fail.

If I try and duplicate my existing spreadsheet (File > Make a copy) it would also fail on the duplicated spreadsheet. I also looked at usage limits for Google Sheets but didn't find anything relevant to my issue.

Video reproduction of the issue: https://imgur.com/a/qJfCC4N

Note how I run the above function in a completely new spreadsheet and it works, but I copy-paste it into an existing spreadsheet that is causing errors and it fails on that spreadsheet.

Any ideas?



from Error retrieving image from URL in Google Apps Script but only on some spreadsheets

No comments:

Post a Comment