Book Image

Google Apps Script for Beginners

By : Serge Gabet
Book Image

Google Apps Script for Beginners

By: Serge Gabet

Overview of this book

Table of Contents (16 chapters)
Google Apps Script for Beginners
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
7
Using User Interfaces in Spreadsheets and Documents
Index

Using a script to insert an image into a document


A complete example including every method would be very long, so I'll show a simplified layout to show how it works, but that only identifies every type of element and replaces a paragraph with an image if a predefined placeholder =='###', is found in the paragraph.

You should easily be able to implement a more universal version yourself that adds or modifies elements based on certain conditions. Test the following code in a text document, such as the one we just created with our recipes.

function findMarkAndReplace() {
  var Doc = DocumentApp.getActiveDocument();
  var image = DocsList.getFileById('0B3qSFd3iikE3UmpjelRQdlZmQXc');
  // this is an image from the recipe's example stored on my drive and shared publicly  
  var totalElements = Doc.getNumChildren();
  var el=[];
  for( var j = 0; j < totalElements; ++j ) {
    var element = Doc.getChild(j);
    var type = element.getType();
    Logger.log('element '+j+" : "+type);
    // to see...