简体   繁体   English

Google 工作表 insertImage function 停止为 Google Apps 脚本工作:验证图像错误

[英]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.几个月来,我一直在使用insertImage function每天将保存在我的云端硬盘中的图像插入到 Google 表格中。 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.一直有效的过程是获取文件 ID、检索链接、从链接中获取图像并插入图像。 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.我还可以将“blob2”保存为图像并以其他方式使用它而不会出现任何错误。 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) insertImage(url, 列, 行, offsetX, offsetY)

OverGridImage:setWidth() 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.我发现 insertImage 调用根本不可靠(由于某些未知和未确定的原因)并且不可避免地会时不时地失败。 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.我在 try catch 中包围了 insertImage 调用,然后用一段时间包围了它。 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.尽管会导致 hacky 和潜在的无限循环(尽管 google app 脚本有一个 6 分钟的硬终止计时器 - 所以没什么大不了的)它完成了工作并且我厌倦了与它作斗争。 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.随意放入一个 for 循环,而不是只尝试一定次数,或者使用调试器命令来查看是否可以进一步调试。

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.我首先尝试插入定时延迟以避免收到错误消息,并且在不成功之后才转向这个 hack 解决方案。 Either the delay I tried was too small, or google is simply upset by some arbitrary number of imageInsert requests made - possibly.要么我尝试的延迟太小,要么谷歌只是对任意数量的 imageInsert 请求感到不安 - 可能。

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.上面的解决方案在一段时间内工作得很好,然后突然(如该线程中所述, 脚本突然停止工作但在其他工作表中工作)所有对 insertImage 的请求都被拒绝,并显示相同的错误消息。 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.值得庆幸的是,它似乎与文档 ID 相关联 - 所以制作一个新副本(或者可能恢复到以前的版本,OP 在上面的链接线程中使用的解决方案)解决了这个问题 - 至少直到你再次点击它。

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!如果有一个“正确的”/“批准的方法来做到这一点,我不知道它是什么并且在文档中找不到任何关于它的东西。如果你知道的话请在这里评论!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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