1

Basically for cutting and pasting elements: These are all valid commands,

{
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  var N = [some number]//the element to copy
  var elementVar = body.getChild(N)
  if (element.getType = 'LIST_ITEM')
  {
      body.appendListItem(elementVar.copy());//copy is required
  }
  //replace XXX_XXX below with one of about a dozen available types.
  if (element.getType = 'XXX_XXX')
  {
      body.appendXxxXxx(elementVar.copy());
  }
  //etc.
}

But what if you want to insert an element without knowing its type and without having use a bunch of if statements?

{
   ...
   body.appendElement(elementVar.copy());//error
}
Michael
  • 77
  • 8

1 Answers1

0

consider mapping out the possible functions in an object before hand...

var map = {
  'LIST_ITEM' : function(copy) { body.appendListItem(copy); },
  'PARAGRAPH' : function(copy) { body.appendParagraph(copy); }
};

var child = body.getChild(N);
var copy = child.copy();

// pass copy to corresponding append func based on type
map[child.getType()](copy); 
Bryan P
  • 5,031
  • 3
  • 30
  • 44