使用 Google Apps Script 将 Google Drive 图像插入到文档中 [英] Inserting a Google Drive image into a document using Google Apps Script

查看:29
本文介绍了使用 Google Apps Script 将 Google Drive 图像插入到文档中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式将图像插入到文档中.我可以让它与带有 URL 的图像一起正常工作,例如 http://www.geeks-on-wheels.net/wp-content/uploads/apple.jpg.

I am trying to pro grammatically insert an image into a document. I can get this to work fine with an image with a URL such as http://www.geeks-on-wheels.net/wp-content/uploads/apple.jpg.

尝试使用位于我的 Google 云端硬盘中的图像时出现问题.

The problem comes when trying to use an image located on my Google Drive.

网址如下所示:

https://docs.google.com/a/finance-in-motion.com/file/d/0B5sPSMZ9pf-QMG9GOVROakxQYmM/edit

https://docs.google.com/a/finance-in-motion.com/file/d/0B5sPSMZ9pf-QMG9GOVROakxQYmM/edit?usp=sharing.

脚本似乎不知道如何处理它并引发错误.任何帮助将不胜感激.

The script doesn't seem to know what to do with it and throws an error. Any help would be appreciated.

推荐答案

从云端硬盘插入文件不要使用 HTTP 路由,除非文件已通过 云端硬盘托管

To insert files from Drive do not use the HTTP route unless the file has been made public via drive hosting

插入使用内置的apis读取文件的博客并插入img.

Insert use the built in apis to read the file's blog and insert the img.

这是一个将 jpg 从我的驱动器(或我有权访问的文件)插入文档的示例.

Here is an example of inserting a jpg from my drive (or a file I have access to) to a document.

function insertImageFromDrive(){
 var fileId = '0B_dyIOzoasdfasdfPVTMxdTVXWDg';
 var img = DriveApp.getFileById(fileId).getBlob();
 DocumentApp.getActiveDocument().getBody().insertImage(0, img); 
}

这将在文档顶部插入一个图像.要更好地控制光标/选择替换,请参阅这篇博文.

This will insert an image at the top of the document. To get more control over the cursor/selection replacement see this blog post.

这篇关于使用 Google Apps Script 将 Google Drive 图像插入到文档中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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