Insert image into a specified location

前提是你 提交于 2019-11-30 16:23:49

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!