0

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

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

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());
            }
          }
        }
      }
    }
  }
}


  • I think that in your script, `}` for `else if( type == DocumentApp.ElementType.TABLE ) {` is not existing. But, you say `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.`. So, I'm worried that you might have miscopied your script. How about this? – Tanaike Feb 26 '23 at 01:09
  • Ah, I made a minor error when simplifying the code. It should be fixed now. The issue is happening in the function copyTable(srcTable, dstTable) { code block, I have denoted it with a comment. Thanks for the questions! Let me know if you need any additional information. – Actualvarric Feb 26 '23 at 01:32
  • Thank you for replying. In your updated script, when you use it, your current situation of `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.` is correctly replicated. Is my understanding correct? – Tanaike Feb 26 '23 at 02:45
  • Yes, that is correct. – Actualvarric Feb 26 '23 at 05:58
  • Thank you for replying. From `Yes, that is correct.`, when I saw your script, `CreateDocumentInFolder(e)` is not enclosed by `}`. By this, an error occurs. Even when this is modified, `itemResponses` is not declaread. By this, a next error occurs. But, you say `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.` and `Yes, that is correct.` to my question. So, I'm confused about your question. I apologize for this. – Tanaike Feb 26 '23 at 08:04
  • Thanks for pointing this out. I have fixed the } error and added definitions for all relevant variables. "response" in the variable "formResponse" is an attribute from the Google Form that is attached to the program that gathers the most recent form response. I will link to the form that I'm using if that would be helpful to see, too. The problem as stated in the post is still occuring. – Actualvarric Feb 26 '23 at 18:23
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Feb 27 '23 at 06:32

0 Answers0