将图片插入指定的位置 [英] Insert image into a specified location

查看:130
本文介绍了将图片插入指定的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Google Apps脚本,它通过调用body.replaceText('TextA','TextB');替换模板文档的副本中的占位符。



现在我想扩展它来包含图像。有没有人知道如何做到这一点?



谢谢,
Andrey



编辑:Just明确我的脚本的功能。我在电子表格中创建了一个Google表单。我创建了一个脚本,它在表单提交时运行,遍历与表单相对应的表单,查找未处理的行,从相应的单元格获取值并将它们放入Google文档的副本中。
Google表单中的某些字段是多行文本字段,这是'\r\r'的来源。



这是一种解决方法I'现在已经出现了,并不优雅,但它到目前为止:

  //替换< IMG src =URL >从URL 
函数获取的图像processIMG_(Doc){

var totalElements = Doc.getNumChildren(); (var j = 0; j
var element = Doc.getChild(j);

;
var type = element.getType();

if(type =='PARAGRAPH'){
var par_text = element.getText();

var start = par_text.search(new RegExp('< IMG'));
var end = par_text.search(new RegExp('>'));
if(start == - 1)
continue;

//从网上检索图片。
var url = getURL_(par_text.substring(start,end));
if(url == null)
continue;

//在image
之前var substr = par_text.substring(0,start);
var new_par = Doc.insertParagraph(++ j,substr);

//插入图片
var resp = UrlFetchApp.fetch(url);
new_par.appendInlineImage(resp.getBlob());

//形象
var substr = par_text.substring(end + 1);
Doc.insertParagraph(++ j,substr);

element.removeFromParent();
j - = 2; //一个 - 用于后面的增量;另一个 - for for循环增量
totalElements = Doc.getNumChildren();




解决方案


(可能有其他方法可以做到这一点,它肯定需要一些增强功能,但总体思路就在那里)



我选择在文档中使用'###'来标记图像将被插入的位置,图像必须位于您的谷歌驱动器(或更准确地在'一些'谷歌驱动器)。
下面的代码使用我共享的文档和我共享的图像,所以你可以尝试它
这里是链接到文档,不要忘记删除图像,并把## #在测试之前的某个地方(如果有人在你之前运行过代码 - )
$ b $ pre $ function analyze(){//只是一个名称,我用它来分析文档
var Doc = DocumentApp.openById('1INkRIviwdjMC-PVT 9io5LpiiLW8VwwIfgbq2E4xvKEo);
var image = DocsList.getFileById('0B3qSFd3iikE3cF8tSTI4bWxFMGM')
var totalElements = Doc.getNumChildren();
var el = []
for(var j = 0; j var element = Doc.getChild(j);
var type = element.getType();
Logger.log(j +:+ type); //查看文档内容
if(type =='PARAGRAPH'){
el [j] = element.getText()
if(el [j] =='###'){element.removeFromParent(); //移除###
Doc.insertImage(j,image); //'image'是图像文件为blob
}
}
}
}

编辑:对于此脚本来说,###字符串必须在其段落中单独使用,之前和之后都没有其他字符。请记住,每次强制新行时输入文档创建一个新的段落。


I have a Google Apps script which replaces placeholders in a copy of a template document with some text by calling body.replaceText('TextA', 'TextB');.

Now I want to extend it to contain images. Does anybody have idea how to do this?

Thank you, Andrey

EDIT: Just to make it clear what my script does. I have a Google form created in a spreadsheet. I've created a script which runs upon form submission, traverses a sheet corresponding to the form, find unprocessed rows, takes values from corresponding cells and put them into a copy of a Google document. Some fields in the Google form are multi-line text fields, that's where '\r\r' comes from.

Here's a workaround I've come up with by now, not elegant, but it works so far:

// replace <IMG src="URL"> with the image fetched from URL
function processIMG_(Doc) {

  var totalElements = Doc.getNumChildren();

  for( var j = 0; j < totalElements; ++j ) {

    var element = Doc.getChild(j);
    var type = element.getType();

    if (type =='PARAGRAPH'){
      var par_text = element.getText();

      var start = par_text.search(new RegExp('<IMG'));
      var end = par_text.search(new RegExp('>'));
      if (start==-1)
        continue;

      // Retrieve an image from the web.
      var url = getURL_(par_text.substring(start,end));
      if(url==null)
        continue;

      // Before image
      var substr = par_text.substring(0,start);
      var new_par = Doc.insertParagraph(++j, substr);

      // Insert image
      var resp = UrlFetchApp.fetch(url);
      new_par.appendInlineImage(resp.getBlob());

      // After image
      var substr = par_text.substring(end+1);
      Doc.insertParagraph(++j, substr);

      element.removeFromParent();
      j -= 2; // one - for latter increment; another one - for increment in for-loop
      totalElements = Doc.getNumChildren();      
    }      
  }
}

解决方案

Here is a piece of code that does (roughly) what you want.

(there are probably other ways to do that and it surely needs some enhancements but the general idea is there)

I have chosen to use '###" in the doc to mark the place where the image will be inserted, the image must be in your google drive (or more accurately in 'some' google drive ). The code below uses a document I shared and an image I shared too so you can try it. here is the link to the doc, don't forget to remove the image and to put a ### somewhere before testing (if ever someone has run the code before you ;-)

function analyze() { // just a name, I used it to analyse docs
  var Doc = DocumentApp.openById('1INkRIviwdjMC-PVT9io5LpiiLW8VwwIfgbq2E4xvKEo');
  var image = DocsList.getFileById('0B3qSFd3iikE3cF8tSTI4bWxFMGM')
    var totalElements = Doc.getNumChildren();
    var el=[]
    for( var j = 0; j < totalElements; ++j ) {
      var element = Doc.getChild(j);
      var type = element.getType();
Logger.log(j+" : "+type);// to see doc's content
       if (type =='PARAGRAPH'){
       el[j]=element.getText()
       if(el[j]=='###'){element.removeFromParent();// remove the ###
         Doc.insertImage(j, image);// 'image' is the image file as blob 
         }
       }
    }
}

EDIT : for this script to work the ### string MUST be alone in its paragraph, no other character before nor after... remember that each time one forces a new line with ENTER the Document creates a new paragraph.

这篇关于将图片插入指定的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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