我的目标是 1.从谷歌文档中获取base64图像字符串 2.将其作为映像保存到驱动器 3.从图片中获取图片,并将新图片插入到与真实图片相同的文档中。
function getImage(doc) {
var tempBase64 = doc.getBody().getParagraphs()[doc.getBody().getParagraphs().length - 1].getText();
var base64 = tempBase64.replace("data:image/svg+xml;base64,", "");
var decoded = Utilities.base64Decode(base64);
return decoded;
}
function run() {
var doc = DocumentApp.getActiveDocument();
var img = getImage(doc); //returns decoded blob
var body = DocumentApp.getActiveDocument().getBody();
//gets the name of the current doc
var filename = DriveApp.getFileById(DocumentApp.getActiveDocument().getId());
var blobImg = Utilities.newBlob(img, MimeType.SVG, filename);
var currentFolder = DriveApp.getFolderById("1UuP2vZNXLVtgrZxAHLSNZUqVnx0xeSgW");
//saves image as same name
var newimg = currentFolder.createFile(blobImg).getId();
//wait for image to save
Utilities.sleep(5 * 1000);
// get blob
var blob = DriveApp.getFileById(newimg).getBlob();
var cursor = doc.getCursor();
if (cursor) {
//insert in to doc
cursor.insertInlineImage(blob);
}
}
function onOpen() {
run();
}
当前正在读取文档并获取base64字符串✓ 例如:
image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+PCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkIj48c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmVyc2lvbj0iMS4xIiB3aWR0aD0iMTIiIGhlaWdodD0iMTUiPjxwYXRoIHN0cm9rZS1saW5lam9pbj0icm91bmQiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgc3Ryb2tlLXdpZHRoPSIyIiBzdHJva2U9InJnYigwLCAwLCAxMzkpIiBmaWxsPSJub25lIiBkPSJNIDEgMyBsIDEgNCIvPjxwYXRoIHN0cm9rZS1saW5lam9pbj0icm91bmQiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgc3Ryb2tlLXdpZHRoPSIyIiBzdHJva2U9InJnYigwLCAwLCAxMzkpIiBmaWxsPSJub25lIiBkPSJNIDggMSBsIDEgMSIvPjxwYXRoIHN0cm9rZS1saW5lam9pbj0icm91bmQiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgc3Ryb2tlLXdpZHRoPSIyIiBzdHJva2U9InJnYigwLCAwLCAxMzkpIiBmaWxsPSJub25lIiBkPSJNIDMgMTAgYyAwLjAyIDAuMDcgMC4zMSAzLjU0IDEgNCBjIDAuODQgMC41NiAzLjg5IDAuNDcgNSAwIGMgMC44IC0wLjM0IDIgLTMgMiAtMyIvPjwvc3ZnPg==
将其另存为特定文件夹中的图像✓
它正在返回新图像的ID。✓
它使图像成为斑点✓
在插入图像时,我得到的图像数据无效。 (第20行,文件“代码”)。从字面上看,不知道如何解决。有人帮忙吗?
答案 0 :(得分:1)
我认为您的脚本是正确的。只有一个问题。目前,mimeType为image/svg+xml
的图像尚不能直接插入Google文档。因此,需要从svg图像转换为其他mimeType。在此修改中,我使用Drive API转换为image/png
。此外,还有几种API(不是Google API)可用于将svg转换为其他API。我认为您遇到的情况有几种解决方法。因此,请仅考虑其中之一。
=s###
的查询参数,可以修改图像大小。我用这个Ref 在此修改后的脚本中,run()
被修改。请确认以下修改后的脚本。
function run() {
var doc = DocumentApp.getActiveDocument();
var img = getImage(doc); //returns decoded blob
var body = DocumentApp.getActiveDocument().getBody();
//gets the name of the current doc
var filename = DriveApp.getFileById(DocumentApp.getActiveDocument().getId());
var blobImg = Utilities.newBlob(img, MimeType.SVG, filename);
var currentFolder = DriveApp.getFolderById("1UuP2vZNXLVtgrZxAHLSNZUqVnx0xeSgW");
//saves image as same name
var newimg = currentFolder.createFile(blobImg).getId();
//wait for image to save
Utilities.sleep(5 * 1000);
// get blob
// --- Added --- Begin
var width = 300; // width of the image. A value of 300 pixels was used as a sample.
var url = "https://www.googleapis.com/drive/v3/files/" + newimg + "?fields=thumbnailLink&access_token=" + ScriptApp.getOAuthToken();
var link = JSON.parse(UrlFetchApp.fetch(url).getContentText()).thumbnailLink.replace(/=s\d+/, "=s" + width);
var blob = UrlFetchApp.fetch(link).getBlob();
// --- Added --- End
var cursor = doc.getCursor();
if (cursor) {
//insert in to doc
cursor.insertInlineImage(blob);
}
}
如果这不是您想要的结果,我表示歉意。