0

I have followed this script How to copy content and formatting between Google Docs? to copy the content from one google doc and paste it to another, which works great, however everytime the content is pasted, there is a space on top of the pasted content, see below. How can the content be pasted properly?

enter image description here

Source file: https://docs.google.com/document/d/1xVpJM4hSN3fosFXR16JbZ1_7r0_PxV92T-G24X5LQRo/edit
Target file: https://docs.google.com/document/d/1g9oon4e0FDBF2fbexVCR-uxKko3B6-Hpj850kiH3qXo/edit

Basically the table from the source file will get copied and pasted to the target file for multiple times, and the tables should sit side by side on the target file without space on top which breaks the format.

appscript is inbedded in the source file

function copyDoc() {
  var sourceDoc = DocumentApp.getActiveDocument().getBody();
//  var targetDoc = DocumentApp.create('CopyOf'+DocumentApp.getActiveDocument().getName());
  var targetDoc = DocumentApp.openById('1g9oon4e0FDBF2fbexVCR-uxKko3B6-Hpj850kiH3qXo');
  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();
}

Edit1: Based on TheWizEd's answer, here's how it looks like, format looks very off.

enter image description here

Subaru Spirit
  • 394
  • 3
  • 19
  • To be clear, you create a copy of source, which has the table on the left. Then you add another copy of source table to the copy on the right? Columns 2? – TheWizEd Feb 02 '23 at 14:22
  • I don't think you will ever get them to line up. There are several elements before the table including an Unsupported element. And a paragraph element after the table. You can remove the last paragraph and copy the table but it still doen't line up. The copied table on the right now appears above the table on the left by a small amount. Trying to figure out how to adjust the position of the copy is too much for me. – TheWizEd Feb 02 '23 at 15:05
  • I might need to switch to portrait view and do it that way instead of trying to align them in landscape view – Subaru Spirit Feb 03 '23 at 09:47
  • I think you still have column set to 2. – TheWizEd Feb 08 '23 at 20:32
  • Hi, apology for the delay in getting back. Can you elaborate on the column set to 2 bit please? I basically used your script on the source file, which returned a weird format. Do I need to change the table properties on the source file tables or change something on the target file? Thanks – Subaru Spirit Feb 21 '23 at 08:42
  • Under Format->Columns you have it set to 2. – TheWizEd Feb 21 '23 at 13:23

1 Answers1

2

Why not copy the table cell into a new cell to the right. Try this.

First I copy the Source or template to a new file. Then I find the table and loop through each row duplicating the cell from column 1 to new column 2. If you wanted to add another row you could duplicate the first row as many times as you want to the new table.

However I notice you are using replacable text. You will have to have a different script to replace the text in column 1 vs. column 2.

I've updated my script to do multiple tables. You don't say how you do multiples. I've just hard coded a variable numCopies. But you should be able to figure out on your own how to do it.

function copyDoc() {
  try {
    let source = DocumentApp.getActiveDocument();
    let file = DriveApp.getFileById(source.getId()).makeCopy("Target");
    let target = DocumentApp.openById(file.getId());
    let body = target.getBody();
    let numCopies = 4;  // note 1 already exists in source
    for( let i=0; i<body.getNumChildren(); i++ ) {
      let child = body.getChild(i);
      if( child.getType() === DocumentApp.ElementType.TABLE ) {
        let table = child.asTable();
        let copy = table.copy(); // master copy
        let j = 0;
        while( j<numCopies ) {
          j++;
          if( j%2 === 0 ) {
            table = body.appendTable(copy.copy());
          }
          else {
            for( let k=0; k<copy.getNumRows(); k++ ) {
              let tRow = table.getRow(k); // target
              let sRow = copy.getRow(k);  // source
              for( let l=0; l<sRow.getNumCells(); l++ ) {
                let cell = copy.getCell(k,l).copy();
                tRow.appendTableCell(cell);
              }
            }
          }
        }
        return;
      }
    }
  }
  catch(err){
    console.log("Error in copyDoc: "+err);
  }
}

Here is the result of my script.

enter image description here

TheWizEd
  • 7,517
  • 2
  • 11
  • 19
  • I have included the output image of your script in the question, can you take a look please? – Subaru Spirit Feb 08 '23 at 10:11
  • I've include an image of what I get when I run the script. – TheWizEd Feb 08 '23 at 19:33
  • I get it working now by setting the columns to 1, but the problem is it only creates two of those side by side. But I want the form to be populating infinitely in the same doc. So it's not 2 "Ready to return" forms in a doc, but multiple forms in the same doc. Thanks – Subaru Spirit Feb 22 '23 at 08:36
  • Your original question only showed one copy. And you can only fit 2 on a page. Do you want a page break between pairs? – TheWizEd Feb 22 '23 at 14:37
  • Yes that's correct, I want those to keep generating while mainting 2 forms on a single page format. So there could be 10 forms for example, which would be 5 pages, with 2 forms on a single page (each script run adds one form) – Subaru Spirit Feb 24 '23 at 12:15