我正在尝试使用Google App脚本构建网站,在该网站中,我从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添加到我的网站的源代码中。谷歌应用程序脚本中有没有一种方法可以向我获取图像/视频的网址。
[进一步研究了您的问题后,我发现我可以使用从表单图像中获取的Blob onject创建一个驱动器文件,然后可以将其用于获取其webContentLink
网址以在网络应用中使用使用Drive API。
要在您的Apps Scrip脚本中使用Drive API,请在脚本编辑器中转到Resources-> Advanced Cloud Services,然后在弹出的对话框中启用Drive API。
这是我用来通过自我解释性注释实现目标的代码(这是一个简化的Web应用程序,仅专注于实现您的目的:]
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>
我希望这对您有所帮助。让我知道您是否还需要其他什么,或者您是否不了解。 :)