0

I am trying to automate document creation using Google Apps Script by making copies of a template and replacing placeholders with actual data, including an image from Google Drive. However, the image is not being inserted into the document.

So far I have:

  1. Ensured that the Drive API is enabled and the script has the correct permissions.
  2. Verified that the {{image1}} placeholder is present in the Google Doc template.
  3. Checked that the extractIdFromDriveUrl function returns a valid Google Drive file ID.
  4. Attempted to manually navigate to the image using the URL constructed from the extracted file ID and confirmed it is accessible.

The script runs without throwing any errors, but the image placeholder is not replaced with the actual image.

The relevant part of my script is as follows:

// Handling image replacement
  var imageFileId = extractIdFromDriveUrl(row[6]);
  if (imageFileId) {
    var imageFile = DriveApp.getFileById(imageFileId);
    try {
      var imgBlob = imageFile.getBlob();

      // Find the placeholder text element
      var foundElement = body.findText('{{image1}}');
      if (foundElement) {
        var placeholderElement = foundElement.getElement().getParent();
        
        // Clear the placeholder text
        placeholderElement.clear();

        // Insert the image into the now-empty paragraph
        placeholderElement.asParagraph().insertInlineImage(0, imgBlob);
      } else {
        Logger.log('Placeholder "{{image1}}" not found.');
      }
    } catch (e) {
      Logger.log('Error handling image: ' + e.toString());
    }
  } else {
    Logger.log('Invalid image URL or file ID.');
  }

What could be preventing the image from being inserted into the document? Are there any additional steps I can take to debug this issue?

  • The script is run by a user who has editor access to the Google Drive folder containing the images.
  • The placeholder is in the body of the document and not within any special elements like tables or text boxes.
  • I am using the following Google Apps Script functions: DriveApp.getFileById, DocumentApp.openById, getBody, findText, insertInlineImage.
3
  • When I tested your script by adding body and a valid file ID of an image, {{image1}} is replaced with the inserted image on Google Document. Unfortunately, I cannot replicate your situation of but the image placeholder is not replaced with the actual image.. For example, even when an error occurs at placeholderElement.asParagraph().insertInlineImage(0, imgBlob), I think that placeholderElement.clear() is run. But, you say the image placeholder is not replaced. So, I'm worried that placeholderElement.clear() might not be run. So, can you confirm whether imageFileId is true? Commented Apr 16, 2024 at 23:12
  • thank you for your reply. Can you pleas explain what you mean by "adding body". Probably that's the step I am missing. Commented Apr 17, 2024 at 21:42
  • Thank you for replying. About adding body, in your showing script, body is not shown. So, I added const body = DocumentApp.getActiveDocument().getBody(). But, I'm not sure about your actual situation. So, I'm not sure whether this is a solution to your current issue. I apologize for this. And, I think that your showing script might not be your whole script. If my understanding is correct, can you provide your whole script for correctly replicating your current issue of but the image placeholder is not replaced with the actual image.? Commented Apr 18, 2024 at 1:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.