pfkbn172
pfkbn172

Reputation: 1

Function to insert an image on a spreadsheet doesn't work well

I wrote code using GAS to summarize data and draw a graph. I run this process every month and it had been working fine until now, but starting today it suddenly started giving errors. The error message is

Exception: Failed to insert image. Please make sure the image is valid and try again.

I tried to identify the problem through debugging, and found that an error was occurring in the insertImage method, which inserts the graph image.

I didn't make any changes to the original file, code and settings, and even with simple test code, insertimage doesn't work properly, so I think it's more likely that there's a specification change or a problem on the server side rather than a technical problem with the code.

I wrote simple debug code to narrow down the problem and check the behavior of insertimage, but the result was the same - it doesn't work properly.

function insertImageDebugTest(){
 const ss = SpreadsheetApp.getActiveSpreadsheet();
 const summarySheet = ss.getSheetByName('forDebug');

 const fileId = "1qOFsxyR2GBjuryL889GiLQq165xAjNvj"; // Specify Google Drive file ID
 const file = DriveApp.getFileById(fileId);
 const blob = file.getBlob();
 Logger.log("MIME Type: " + blob.getContentType());

 summarySheet.insertImage(blob, 1, 1).setWidth(323).setHeight(140);
}

[Error message]

Exception: The image could not be inserted. Please verify it is valid and try again.

Additional information, I've confirmed below.

Upvotes: 0

Views: 38

Answers (1)

Wicket
Wicket

Reputation: 38391

I tested the OP script using a PNG image taken from Wikipedia, and uploaded to Google Drive. The image size is 219 KB and 800 x 600 pixels. The script worked fine. Considering this, the problem might be a glitch, so rerun your script.

If the problem still happens, try a couple of variations of your script, i.e., run your script on more spreadsheets, create by you stored in your Drive and in a Shared try with different images,

PNG image taken from Wikipedia

Image source: https://es.wikipedia.org/wiki/Portable_Network_Graphics#/media/Archivo:PNG_transparency_demonstration_1.png

Upvotes: 0

Related Questions