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:
- Ensured that the Drive API is enabled and the script has the correct permissions.
- Verified that the
{{image1}}placeholder is present in the Google Doc template. - Checked that the
extractIdFromDriveUrlfunction returns a valid Google Drive file ID. - 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.
bodyand a valid file ID of an image,{{image1}}is replaced with the inserted image on Google Document. Unfortunately, I cannot replicate your situation ofbut the image placeholder is not replaced with the actual image.. For example, even when an error occurs atplaceholderElement.asParagraph().insertInlineImage(0, imgBlob), I think thatplaceholderElement.clear()is run. But, you saythe image placeholder is not replaced. So, I'm worried thatplaceholderElement.clear()might not be run. So, can you confirm whetherimageFileIdis true?adding body, in your showing script,bodyis not shown. So, I addedconst 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 ofbut the image placeholder is not replaced with the actual image.?