I want to use google script to copy content from a source file to a destination file. The source file contains images inside tables. The problem is that the sample scripts I used do not generate the images inside the tables, ie the images that are contained in the tables do not appear in the destination file. Anyone have an idea how to solve this problem? Here is the code used to accomplish this task.
function createDocument() {
var pastaId = '1NiH5Gk3........';
var idPadrao = '1Z-SrW6ocb........';
var docPadrao = DocumentApp.openById(idPadrao);
var pasta = DriveApp.getFileById(idPadrao).getParents()
var body = docPadrao.getBody();
var docNovo = DocumentApp.create('novoDocTeste1');
bodyNovo = docNovo.getBody();
bodyNovo.appendParagraph("This is a testing paragraph.")
appendToDoc(docPadrao, docNovo);
var top = docNovo.getBody().getChild(0);
bodyNovo.insertParagraph(0, "Nome..................");
salvarArquivo(docNovo, pastaId);
docPadrao.saveAndClose();
}
function appendToDoc(src, dst) {
Logger.log('append...');
for (var i = 0; i < src.getNumChildren(); i++) {
appendElementToDoc(dst, src.getChild(i));
}
}
function appendElementToDoc(doc, object) {
var type = object.getType();
var element = object.copy();
Logger.log("Element type is "+type);
if (type == "PARAGRAPH") {
doc.appendParagraph(element);
} else if (type == "TABLE") {
doc.appendTable(element);
}
}
function salvarArquivo(docNovo, pastaId){
// Get folder by ID
var folder = DriveApp.getFolderById(pastaId);
docNovo.saveAndClose();
// Get file by doc id
var file = DriveApp.getFileById(docNovo.getId());
// Get root directory
var parents = file.getParents();
// Remove file from root folder
parents.next().removeFile(file);
// Add file to the specified folder
folder.addFile(file);
}