无法使用Apps脚本将图像从Google云端硬盘添加到Google表单 [英] Cannot add image from Google Drive to a Google Form using Apps Script

查看:98
本文介绍了无法使用Apps脚本将图像从Google云端硬盘添加到Google表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式将Google云端硬盘中的图片添加到Google表单中.通过使用Google示例(例如:

I am trying to programmatically add an image from my Google Drive to a Google form. I can get it to work using a reference to a static image URL by using the google example such as:

var img = UrlFetchApp.fetch('https://www.google.com/images/srpr/logo4w.png');
form.addImageItem().setImage(img);

这行得通但我想引用我的Google云端硬盘帐户中的图片.

This works o.k. but I want to refer to an image in my Google Drive account.

如果我尝试使用图像共享链接到Google云端硬盘图像,则会收到错误消息:

If I try and use the image share link to a Google Drive image I get an error:

var img = UrlFetchApp.fetch("https://drive.google.com/open?id=0B_u2iAm1LaoZSHd1TTExcDFrbUU");
form.addImageItem().setImage(img);

错误:"Blob对象为此必须具有图像内容类型 操作."

ERROR: "Blob object must have an image content type for this operation."

如果我尝试通过其ID打开文件,则会收到另一个错误:

And if I try and open the file by it's ID I get a different error:

var img = DriveApp.getFileById("0B_u2iAm1LaoZSHd1TTExcDFrbUU");
form.addImageItem(img);

错误:无法添加图片,请稍候,然后重试."

ERROR: "Could not add image, please wait a minute and try again."

我似乎找不到任何有关如何实现此目标或做错事情的文档.我尝试将Google云端硬盘中图片上的共享设置更改为公开.而且,我尝试为Google云端硬盘中的图片获取静态图片网址,但似乎唯一可用的网址是共享网址,该网址会转到图片的保留/显示页面,而不是实际的图片本身.

I can't seem to find any documentation about how I can achieve this or what I am doing wrong. I tried changing the sharing settings on the image in my Google Drive to be publicly available. And I have tried to get a static image URL to the image in the Google Drive but the only URL that seems to be available is the share URL which goes to a holding/display page for the image and not the actual image itself.

推荐答案

对不起,我认为我发布了错误的代码段,所以实际上我在做的是:

Sorry, I think I posted the wrong code segment, so actually what I am doing is:

var img = DriveApp.getFileById("a_valid_drive_file_id_here");
var imageItem = form.addImageItem();
imageItem.setImage(img);

并显示无法添加图像,请稍候,然后重试."错误.

And getting the "Could not add image, please wait a minute and try again." error.

但是,我想我已经回答了我自己的问题,上面的示例(通过ID获取文件并调用setImage)确实适用于存储在Google云端硬盘中的图像,但是如果图像太大,则会出现错误.不幸的是,API似乎没有指定任何大小限制,并且错误消息不是很有用,并且也没有说出图像太大.

However, I think I have now answered my own question, the example above (getting the file by ID and calling setImage) DOES work for images stored in Google Drive, however you get the error if the image is too large. Unfortunately the API does not seem to specify any size limits and the error message is not very useful and again does not say anything about the image being too large.

因此,我不知道以这种方式添加的图像的大小限制是多少,但是如果其他任何人有相同的错误,请尝试减小图像文件的大小.

So I don't know what the size limit is for images added in this way, but if anyone else has the same error, try reducing the size of the image file.

这篇关于无法使用Apps脚本将图像从Google云端硬盘添加到Google表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆