Keld Hammerum
Keld Hammerum

Reputation: 63

Copy table from one Google Docs file to another

Can anyone provide an example script showing how to copy a table from one Google Doc to another? I have tried multiple variations of "Table.insertTable" and "Table.appendTable", but I cannot get it right.

Upvotes: 0

Views: 1501

Answers (1)

Keld Hammerum
Keld Hammerum

Reputation: 63

Solved it! I realized that I had to use the "Copy()" function to get a proper handle to the table. So, the final code looks something like this:

    var sourcedoc = DocumentApp.openById(sourceid);
    var sourcebody = sourcedoc.getBody();
    var tables = sourcebody.getTables();
    var table = tables[0].copy();
    var destdoc = DocumentApp.openById(destid);
    var destbody = destdoc.getBody();
    var x = destbody.appendTable(table)

Upvotes: 2

Related Questions