【问题标题】:How to fetch the URL of an image/video question posted in google form using app script?如何使用应用脚本获取以谷歌形式发布的图像/视频问题的 URL?
【发布时间】:2020-08-09 19:10:33
【问题描述】:

我正在尝试使用 Google App Script 构建一个网站,我从我的 Google 表单中复制所有元素并将其显示在我自己的网站上。我能够使用:https://developers.google.com/apps-script/reference/forms 获取所有元素的详细信息,但 Image 元素除外。

此链接没有获取图片 URL 的信息: https://developers.google.com/apps-script/reference/forms/image-item

我能够获取 BLOB,但无法获取图像 URL。 例如在屏幕截图中,我需要在我的表单中突出显示图像的 URL:https://lh6.googleusercontent.com/uZtoCzoraW7vkkrN2289pX83Y6wwaRmMjpgBySWHaT3Vm2p9DVAA7Voy4CclYp2hve6c6zzzLkh-rF1yAX9fFPTTd940WMjwVtjcyMF2XCbdQ7YKQhhCfYxSUyKztcKacWCitDy4C31f9lQ

我需要将图片的 URL 添加到我网站的源代码中。 谷歌应用脚​​本中是否有一种方法可以获取图像/视频的 URL。

【问题讨论】:

  • 你能显示代码和表格吗?
  • 表单:docs.google.com/forms/d/… 获取图像 BLOB:case FormApp.ItemType.IMAGE: { var imgBlob = item.asImageItem().getImage(); var URL = imgBlob.getContentUrl();我想获取图像项的 URL。
  • 嗨!所以根据您提供的内容,我相信您已成功从您的图像项目中获取图像 Blob,对吗?在这种情况下,您的问题将是如何使用此 Blob 相应地显示图像,对吗?如果是这种情况,请查看这个Stack Overflow 答案,让我知道这是否有效。 :D
  • @MateoRandwolf Google App 脚本不支持 window.URL ||窗口.webkitURL;我需要使用 Google 应用脚本函数来获取我无法找到的图片 URL。
  • 嗨!然后我认为不可能从图像 blob 中获取图像 URL。您是如何在表单中插入此图像的?你是用脚本插入的吗?如果是这样,那么您必须在某处拥有图像 URL。另外,获取此 URL 的一种方法是,我不确定它是否符合您的需要,即转到表单,右键单击,在新选项卡中打开图像并复制图像地址。然后使用它来获取 HTML 中的图像。

标签: google-apps-script google-api google-forms google-apps-script-addon


【解决方案1】:

解决方案

在进一步查看您的问题后,我发现我可以使用您从表单图像中获得的 Blob onject 创建一个驱动器文件,然后可以使用该文件获取其webContentLink url 以在网络中使用应用程序使用Drive API

为了能够在您的 Apps Scrip 脚本中使用 Drive API,请在您的脚本编辑器中转到 Resources -> Advanced Cloud Services 并在弹出的对话框中启用 Drive API。

这是我用来通过自解释 cmets 实现您的目标的一段代码(这是一个简化的网络应用程序,只专注于实现您的目的):

code.gs 文件

// Apps SCript function to serve the HTML file in the web
function doGet() {
  return HtmlService.createHtmlOutputFromFile('test');
}

// Function to be called in the HTML that returns the URL to be used by the image tag
function getUrl() {
  var form = FormApp.getActiveForm();
  // Get image blob
  var image = form.getItems()[0].asImageItem().getImage().getAs('image/jpeg');
  // Use the image blob for creating a new drive file
  var file = DriveApp.createFile(image);
  // Use the Drive API get method to get the property webContentLink which will return the link
  // of the image to be used in a website
  var fileURL = Drive.Files.get(file.getId()).webContentLink;
  
  return fileURL;
}

index.html

<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body id="body">
<h1>MY IMAGE</h1>

<script>

// Get what our function getURL returns
google.script.run.withSuccessHandler(function(URL) {
// Create HTML image tag
var img = document.createElement('img'); 
// Set its source to our file's sharable URL
img.src =  URL; 
// Attach image element to the body
document.getElementById('body').appendChild(img); 
}).getURL();

</script>

</body>
</html>

我希望这对您有所帮助。让我知道您是否需要其他任何内容或者您是否不理解某些内容。 :)

【讨论】:

  • 您发布的代码运行良好。但我不想创建新文件或询问用户访问其驱动器的权限(以防我将应用程序部署为 gsuite 插件)。因此,如果有办法获得可以直接使用的图像/视频链接,那就更好了。感谢@mateo的回答
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-25
  • 2014-09-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多