在Google脚本中使用appendParagraph时,图像会自我复制 [英] Image duplicates itself when using appendParagraph in Google Script
问题描述
我编写了一个脚本,用于将Google云端硬盘中的图片和一些自定义文字添加到Google文档中.(我从
但是,如果我摆脱了appendParagraph代码( body.appendParagraph(t1);
),我只会得到一张图像(但显然没有想要的文本段落)
这是怎么回事?如何添加一张图片和我的文本段落?
关于原因,我什至没有丝毫线索,但是我找到了一种使这项工作成功的方法.
切换代码的顺序似乎可以解决问题.我只是简单地将图像插入代码移到了 end (即之后 appendParagraph
代码),就可以正常工作了.没有重复的图片!
function myFunction(e){var doc = DocumentApp.create('fileTest');var body = doc.getBody();body.appendParagraph('测试文本的测试行');var matchedFiles = DriveApp.getFilesByName('logo.png');如果(matchedFiles.hasNext()){var image = matchedFiles.next().getBlob();varlocatedImage = body.getParagraphs()[0] .addPositionedImage(image);}doc.saveAndClose();}
I wrote a script to add an image from my Google Drive and some custom text to a Google Doc. (I got the image insertion code from here). The resulting document is created ok, but my image is added twice for some reason...
function myFunction(e) {
var doc = DocumentApp.create('fileTest');
var body = doc.getBody();
var matchedFiles = DriveApp.getFilesByName('logo.png');
if (matchedFiles.hasNext()) {
var image = matchedFiles.next().getBlob();
var positionedImage = body.getParagraphs()[0].addPositionedImage(image);
}
body.appendParagraph('Test line of text for testing');
doc.saveAndClose();
}
However, if I get rid of my appendParagraph code (body.appendParagraph(t1);
) I only get one image (but obviously without the paragraph of text I want)
What's going on here? And how do I add both one picture and my paragraph of text?
I have not even the slightest clue as to why, but I found a way to make this work.
Switching the order of my code seemed to do the trick. I simply moved the image-insertion code to the end (i.e., after the appendParagraph
code), and it worked fine. No duplicate image!
function myFunction(e) {
var doc = DocumentApp.create('fileTest');
var body = doc.getBody();
body.appendParagraph('Test line of text for testing');
var matchedFiles = DriveApp.getFilesByName('logo.png');
if (matchedFiles.hasNext()) {
var image = matchedFiles.next().getBlob();
var positionedImage = body.getParagraphs()[0].addPositionedImage(image);
}
doc.saveAndClose();
}
这篇关于在Google脚本中使用appendParagraph时,图像会自我复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!