在 Google Script 中使用 appendParagraph 时,图像会自我复制 [英] Image duplicates itself when using appendParagraph in Google Script

查看:14
本文介绍了在 Google Script 中使用 appendParagraph 时,图像会自我复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个脚本,用于将 Google 云端硬盘中的图像和一些自定义文本添加到 Google 文档.(我从

但是,如果我去掉我的 appendParagraph 代码 (body.appendParagraph(t1);),我只会得到一张图片(但显然没有我想要的文本段落)

这是怎么回事?我如何同时添加一张图片和我的一段文字?

解决方案

我对原因一无所知,但我找到了一种方法来完成这项工作.

切换我的代码顺序似乎可以解决问题.我只是将图像插入代码移到 end(即,after appendParagraph 代码),它运行良好.没有重复的图像!

function myFunction(e) {var doc = DocumentApp.create('fileTest');var body = doc.getBody();body.appendParagraph('测试文本行');var matchingFiles = DriveApp.getFilesByName('logo.png');if (matchedFiles.hasNext()) {var image = matchingFiles.next().getBlob();var PositionedImage = 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 Script 中使用 appendParagraph 时,图像会自我复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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