更新时间:2023-12-05 21:28:22
关于原因,我什至没有丝毫线索,但是我找到了一种使这项工作成功的方法.
切换代码的顺序似乎可以解决问题.我只是简单地将图像插入代码移到了 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();
}