Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8137271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:03:44+00:00 2026-06-06T11:03:44+00:00

I have a Google Apps script which replaces placeholders in a copy of a

  • 0

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();      
    }      
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T11:03:47+00:00Added an answer on June 6, 2026 at 11:03 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read some of the basic Google Apps Script documentation/tutorials. I have not
I have created a UI web app in google apps script which access a
I have 2 html documents inside my google apps script project (showHtml1, showHtml2). I
I have been playing around with Google Apps Script today and I am trying
I have now written a number of different functions in Google Apps Script that
I have built an object in Javascript on the Google Apps Script engine and
I've been looking at the Google Apps Script API documentation, and have not been
I have developed several general purpose script functions, for Google Spreadsheet apps. Currently I
I have this script to copy multiple value of textbox1 [which are separated by
I have some html and script here: <html> <head> <script type=text/javascript src=http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js></script> <script type=text/javascript>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.