表格单元格内的Inline Image无法复制到新文档问题
我开发了一个Google Apps Script程序,在表单提交时会将多个文档内容复制到新建文档中,但目前遇到表格内嵌入的Inline Image无法复制的问题。
提交表单时,我收到邮件警报,提示代码中var dstImg = dstParagraph.insertInlineImage(l, srcImg.getBlob());这一行出现Service unavailable: Documents错误。
已排查的内容
- 确认使用的是Inline Image而非Fixed Image
- 已启用Google Docs和Drive API
- 查阅过多个Stack Overflow相关问题,但未找到解决方法
参考过的Stack Overflow资源
- Google Apps Script - appendParagraph with image
- Images disappear when nested inside a table and appended to a new document. Bug or did I miss something?
- How to copy image from one table cell to another within google documents using a google script (programmatically)?
- Paragraphs with inline images breaking app script merging sheet data into doc
相关代码
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()); } } } } } } }
内容的提问来源于stack exchange,提问作者Actualvarric
相关产品推荐
相关产品推荐

