insert image at specific point of paragraph

后端 未结 3 2104
一整个雨季
一整个雨季 2020-12-09 23:04

*I have a Google Document with a string like \"text {logo} text\" How do place an image where {logo} is? So far I tried:

var logoElement = s.findText(\"{logo         


        
相关标签:
3条回答
  • 2020-12-09 23:30

    Thanks to the comment from Serge and the original post of the link for pointing me in the right direction. Here's what I have now:

    var s = d.getHeader();
    var logoResult = s.findText("{logo}"); // search result
    var logoElement = logoResult.getElement(); // the paragraph that contains the placeholder
    var text = logoElement.getText();
    var placeholderStart = logoResult.getStartOffset(); // character position start placeholder
    var placeholderEnd = logoResult.getEndOffsetInclusive(); // char. position end placeholder
    var parent = logoElement.getParent(); 
    var parPosition = parent.getChildIndex(logoElement);
    
    // add new paragraph after the found paragraph, with text preceding the placeholder
    var beforeAndLogo = s.insertParagraph(parPosition+2, text.substring(0, placeholderStart));
    var logo = beforeAndLogo.appendInlineImage(logoBlob); // append the logo to that new paragraph
    
    // add new paragraph after the new logo paragraph, containing the text after the placeholder
    var afterLogo = s.insertParagraph(parPosition+3, text.substring(placeholderEnd+1));
    afterLogo.merge(); // merge these two paragraphs
    
    // finally remove the original paragraph
    parent.removeFromParent(); // remove the original paragraph
    

    It is not complete, I should also copy all the attributes. More importantly, it does not copy the tab settings (e.g. center tab). Have not found a way to set tab positions.

    0 讨论(0)
  • 2020-12-09 23:47

    I hope will be helpful, The following code is fine to me.

    function insertImage(doc) {
      // Retrieve an image from the web.
      var resp = UrlFetchApp.fetch("http://www.google.com/intl/en_com/images/srpr/logo2w.png");
      var image = resp.getBlob();
      var body = doc.getBody();
      var oImg = body.findText("<<Logo1>>").getElement().getParent().asParagraph();
      oImg.clear();
      oImg = oImg.appendInlineImage(image);
      oImg.setWidth(100);
      oImg.setHeight(100);
    }
    
    0 讨论(0)
  • 2020-12-09 23:47

    I tried a simpler version based on your answer that keeps the format of the original paragraph...

    Give it a try, it's not "foolproof" but it works in my test and is (I think) an interresting trial ;-)

    code here :

    function test(){
      placeImage('{logo}','0B3qSFd3iikE3SkFXc3BYQmlZY1U');
    //This is my page and I’d like to have a {logo} on it right here
    }
    
    
    function placeImage(placeHolder,imageId) {
      var logoBlob = DocsList.getFileById(imageId).getBlob();
      var d = DocumentApp.getActiveDocument()
      var s = d.getHeader();
      var logoResult = s.findText(placeHolder); 
      var placeholderStart = logoResult.getStartOffset(); 
      var par = s.getChild(0).asParagraph();
      var parcopy = par.copy();
      var parLen = par.editAsText().getText().length-1;
      Logger.log('placeholderStart = '+placeholderStart+'  parLen = '+parLen)
      par.editAsText().deleteText(placeholderStart, parLen);
      parcopy.editAsText().deleteText(0, placeholderStart+placeHolder.length);
      var img = s.getChild(0).appendInlineImage(logoBlob);
      s.appendParagraph(parcopy);
      parcopy.merge();
    }
    

    enter image description here

    0 讨论(0)
提交回复
热议问题