I'm creating a google API append script. There's a google sheet with lots of google document links. I retrieve these links open the file, go through its content and append it to a new document. This needs to be done for all documents in this list. After struggling for a while I got all of it working except for positioned images. Either I don't get an image at all, or the image gets copied in, but from that point on is copied and pasted on every page of the document. I'm probably just failing somewhere in the coding process but can't seem to find my own flaw. Note that I work in IT but not as a developer so Yes the code is written very poorly :'(
I've tried and reeditted my code repeatedly to ensure i was picking up the positioned image properly (e.g. by putting text in instead of the image etc. and it all seems to work properly, however the positioned image keeps repeating all over the document.)
Note that below I've only included the bad function and the main i'm calling, not all the get stuff from the excel sheet as that's working properly.
UPDATE: I've updated my code. It now successfully finds, sets layout etc. and places the positioned images.
However, from that point on, the positioned images that have been placed earlier in the merged document are repeated on the last paragraph of every appended document.
function main() {
var spreadsheetid = "Fill_this_with_spreadsheet_ID";
var DocIDS = [];
DocIDS = getDocIDFromSpreadsheet(spreadsheetid);
mergeGoogleDocs(DocIDS);
//remove_blank(DocIDS);
}
/*Function to append contents of document to another document
for (var i = 1; i < List.length; ++i ) {
var otherBody = DocumentApp.openById(List[i]).getActiveSection();
var totalElements = otherBody.getNumChildren();
baseDoc.saveAndClose()
var baseDoc = DocumentApp.openById(DocIDS[0]);
var body = baseDoc.getActiveSection();
for( var j = 0; j < totalElements; ++j ) {
var element = otherBody.getChild(j).copy();
var type = element.getType();
if( type == DocumentApp.ElementType.PARAGRAPH ){
var positionedImages = element.getPositionedImages();
if ( positionedImages == "PositionedImage") {
//This is required because the positionedImage needs a "new" pragraph to anchor to...
var paragraph = body.appendParagraph("");
var tempvalue = positionedImages[0].getLayout();
var tempheight = positionedImages[0].getHeight();
var tempwidth = positionedImages[0].getWidth();
var tempblob = positionedImages[0].getBlob();
paragraph.addPositionedImage(tempblob);
var positionedImages2 = paragraph.getPositionedImages();
positionedImages2[0].setLayout(tempvalue);
positionedImages2[0].setLayout(tempvalue);
positionedImages2[0].setHeight(tempheight);
positionedImages2[0].setWidth(tempwidth);
//To-do, fix repeated multiplication of positioned Images on all following documents at the final paragraph.
}
else {
body.appendParagraph(element);
}
}
else if( type == DocumentApp.ElementType.TABLE ){
body.appendTable(element);}
else if( type == DocumentApp.ElementType.LIST_ITEM ) {
glyphType = element.getGlyphType();
body.appendListItem(element);
element.setGlyphType(glyphType);
}
else
throw new Error("Unknown element type: "+type);
}
body.appendPageBreak()
}
I expect to get all documents merged into one document. while it does merge everything, it goes wrong on positioned images. When I run the code I get the positioned images duplicated on every new appended document following the first time it was inserted. Where it only needs to be included once, not on every document.
I've been struggling for several evenings now and desperate for pointers and/or a proper fix.