1

I've copied Serge's script (Serge insas) But when there's an image inside a table cell, it isn't visible in the targetDoc. Do you know what to do?

function copyDoc() {
  var sourceDoc = DocumentApp.getActiveDocument().getBody();
  var targetDoc = DocumentApp.create('CopyOf'+DocumentApp.getActiveDocument().getName());
//  var targetDoc = DocumentApp.openById('another doc ID');
  var totalElements = sourceDoc.getNumChildren();
  
  for( var j = 0; j < totalElements; ++j ) {
    var body = targetDoc.getBody()
    var element = sourceDoc.getChild(j).copy();
    var type = element.getType();
    if( type == DocumentApp.ElementType.PARAGRAPH ){
      body.appendParagraph(element);
    }
    else if( type == DocumentApp.ElementType.TABLE){
      body.appendTable(element);
      }
    else if( type == DocumentApp.ElementType.LIST_ITEM){
      body.appendListItem(element);
      }
//    ...add other conditions (headers, footers...
    }
  targetDoc.saveAndClose();
}
Iamblichus
  • 18,540
  • 2
  • 11
  • 27
  • 1
    Does this answer your question? [app-script google doc copy table with inline image](https://stackoverflow.com/questions/37206436/app-script-google-doc-copy-table-with-inline-image) – Diego Nov 29 '20 at 18:22
  • How was the image added? Can you provide a copy of the document, free of sensitive information? – Iamblichus Nov 30 '20 at 10:16
  • Thank you, Diego! I tried that script also, but it erases all the cells and doesn´t copy the images. – user14693613 Dec 03 '20 at 11:55
  • Hi, Lamblichus! Thank you for asking. Here is an example of a simple document with a table and an image inside of it. https://docs.google.com/document/d/1O220GsICkd7kjtZ5urm8VVCzh4MpOAIKFJlG3PaFxWM/edit?usp=sharing And here is the screenacapture of what happens when I run the script to copy the document. https://drive.google.com/file/d/1wEj7ifCsCOrF4knUGuW_fCwxh_V9C9T5/view?usp=sharing – user14693613 Dec 03 '20 at 11:56
  • Does this answer your question? [How to copy image from one table cell to another within google documents using a google script (programmatically)?](https://stackoverflow.com/questions/53873919/how-to-copy-image-from-one-table-cell-to-another-within-google-documents-using-a) – Iamblichus Dec 04 '20 at 12:54
  • Thanks, Lamblichus! I´ve tried the last script proposed as an answer, but still it doesn´t copy the image. https://drive.google.com/file/d/1NzRT4mQkNvFLTEhmxoaRAp3e9aJorc5R/view?usp=drivesdk It amused me that it did copy the image when I set the source document as the target document. https://drive.google.com/file/d/1k1GfZeaWj4vRvI8S3taDoM6LoZdy67Wj/view?usp=drivesdk – user14693613 Dec 05 '20 at 17:18

0 Answers0