I have a program that copies the contents of various documents to a newly created document upon submission of a form. I am running into issues copying inline images embedded into tables.
When submitting the form, I get an email alert stating "Service unavailable: Documents" at the line "var dstImg = dstParagraph.insertInlineImage(l, srcImg.getBlob());" in the code below.
All of the Stack Overflow questions I have seen about this seem to stem from the fact that the user was not using Inline Images, rather Fixed Images instead. I have double-checked that my images are, in fact, inline. I do also have Google Docs and Drive API enabled.
Here is a link to the document that I am using:
https://docs.google.com/document/d/1WFEvhnW2FDtfBrDPiPXH1U09DD8mg6PA_Q6afciSLgU/edit?usp=sharing
Here is a link to a screenshot of what the generated document looks like when I receive the error message:
https://drive.google.com/file/d/1x36HFEAxiPWFOjLxLqkgDQYDnr0Fftem/view?usp=sharing
Here is a link to the form that is attached to this program: https://docs.google.com/forms/d/e/1FAIpQLSdNu3nTQF8s5ScYCLXLd1y0Ji3VKUMKtYIfa4-dIlZptRJ5wQ/viewform?usp=sharing
I realize that this is not a new issue, I have already looked at the following sources for help:
Google Apps Script - appendParagraph with image
Paragraphs with inline images breaking app script merging sheet data into doc
Here is the relevant code:
function CreateDocumentInFolder(e) {
var formResponse = e.response;
var itemResponses = formResponse.getItemResponses();
var question_twelve_answer = itemResponses[12].getResponse();
var page_number_template = DriveApp.getFileById('10HNrpwxkZsO7ulr8b61pvB6fnclLKMDbNUWI0Nw9Y9g')
var student_lp = page_number_template.makeCopy( DriveApp.getFolderById('1a5k6yBqcQUL-g21oiCj3_wK9Njn-xML1'))
var main_file_ID = student_lp.getId()
var doc = DocumentApp.openById(main_file_ID)
var body = DocumentApp.openById(main_file_ID).getBody();
var otherBody12 = DocumentApp.openById('1WFEvhnW2FDtfBrDPiPXH1U09DD8mg6PA_Q6afciSLgU').getBody();
if (question_twelve_answer != 'm<12') {
var totalElements = otherBody12.getNumChildren();
for( var j = 0; j < totalElements; ++j ) {
console.log(j);
var element = otherBody12.getChild(j).copy();
var type = element.getType();
if( type == DocumentApp.ElementType.PARAGRAPH ) {
body.appendParagraph(element);
}
else if( type == DocumentApp.ElementType.TABLE ) {
var dstTable = body.appendTable(element);
var srcTable = element.asTable();
copyTable(srcTable, dstTable);
}
else if( type == DocumentApp.ElementType.LIST_ITEM )
body.appendListItem(element);
else
throw new Error("Unknown element type: "+type);
}
body.appendPageBreak()
}
doc.saveAndClose()
}
function copyTable(srcTable, dstTable) {
var row = srcTable.getNumRows();
for (var i = 0; i < row; i++) {
var col = srcTable.getRow(i).getNumCells();
for (var j = 0; j < col; j++) {
var cell = srcTable.getCell(i, j);
var c1 = cell.getNumChildren();
for (var k = 0; k < c1; k++) {
var ty = cell.getChild(k).getType();
if (ty === DocumentApp.ElementType.TABLE) {
srcTable = cell.getChild(k).asTable();
dstTable = dstTable.getCell(i, j).getChild(k).asTable();
return copyTable(srcTable, dstTable);
} else {
var paragraph = cell.getChild(k).asParagraph();
var c2 = paragraph.getNumChildren();
for (var l = 0; l < c2; l++) {
var child = paragraph.getChild(l);
var t = child.getType();
if (t === DocumentApp.ElementType.INLINE_IMAGE) {
var srcImg = child.asInlineImage();
var dstParagraph = dstTable.getCell(i, j).getChild(k).asParagraph();
dstParagraph.getChild(l).asInlineImage().removeFromParent();
// Issue is the line below!
var dstImg = dstParagraph.insertInlineImage(l, srcImg.getBlob());
dstImg.setWidth(srcImg.getWidth());
dstImg.setHeight(srcImg.getHeight());
}
}
}
}
}
}
}