简体   繁体   中英

Google sheet insertImage function stopped working for Google Apps Script: verify image error

I have been using the insertImage function to insert images saved on my Drive into Google sheets daily for several months. Today it has stopped working with the following error message:

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

The procedure that has always worked was to get the file ID, retrieve the link, fetch the image from the link and insert the image. This is illustrated in the code below:

  var blob = DriveApp.getFileById(fileID).getBlob()
  var width = 340; 
  var link = Drive.Files.get(fileID).thumbnailLink.replace(/\=s.+/, "=s" + width);
  var blob2 = UrlFetchApp.fetch(link).getBlob()
  var the_image = TemplateSheet.insertImage(blob2, 3, 13, 110, 0);

When I visit the "link" I am directed to the image as expected. I can also save "blob2" as an image and use it in other ways without any errors. Can anyone assist with a solution to the error I am receiving?

Try this:

function myFunction() {
  var blob = DriveApp.getFileById("fileid");
  var width = 340; 
  //Insert and resize image 
  SpreadsheetApp.getActiveSheet().insertImage(blob, 3, 13, 110, 0).setWidth(width);
}

Reference:

insertImage(url, column, row, offsetX, offsetY)

OverGridImage:setWidth()

Although in your case it was (possibly?) a malformed sheet as per your comment on the selected answer - it was not in mine, and so this answer is for others who stumble here.

I found that the insertImage call is simply unreliable (for some unknown and undetermined reason(s)) and will inevitably fail from time to time. It rarely if ever fails on the first usage (I've never seen it), but subsequent uses especially in loops are bound to receive this error.

I surrounded the insertImage call in a try catch and then surrounded that with a while. Although hacky and potentially infinite loop causing (though google app scripts have a hard 6 min kill timer - so no big whoop) it gets the job done and I was tired of struggling with it. Feel free to put in a for loop instead to try only a certain amount of times, or debugger command to see if you can debug any further.

var insertSuccess = false;
var insertImageErrorMessage = "";

while (! insertSuccess )
{
  try
  {
    *** insertImage call goes here ***
    insertSuccess = true;
  }
    catch (e)
  {
    console.log(e.message);
    insertImageErrorMessage=e.message;
  }
}

EDIT WARNING!!!

It seems that this is caused by some sort of "spam protection" on google, serverside. I tried inserting timed delays at first to avoid the received error message and only moved on to this hack solution after that was unsuccessful. Either the delay I tried was too small, or google is simply upset by some arbitrary number of imageInsert requests made - possibly.

The solution above worked just fine for a while, and then suddenly (as described in this thread as well Script suddenly stops working but works in other sheets ) ALL requests to insertImage were refused with that same error message. As far as I can tell, once this happens - it is permanent.

Thankfully it seems to be associated with the document ID - so making a new copy (or possibly reverting to a previous version, the solution used by the OP in the linked thread above) solves the problem - at least until you hit this again.

If there is a "right" / "approved way to do this, I do not know what it is and found nothing in the documentation about it. Comment here if you know something!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM