I’m currently working on a Google Apps Script that generates multiple documents for students based on a template. I have different templates (Google Docs) such as “Attestation of Completion,” “Training Contract,” “Pre-training Evaluation,” and so on, stored in my Google Drive. I use data from a Google Sheets file where each row represents a student, and I want the script to automatically fill the templates with the corresponding data.
The script works fine with one of the templates (“Convention”), where the placeholders (tags) such as {{FirstName}}, {{LastName}}, and {{CreationDate}} are replaced correctly with the data from the spreadsheet. However, for other templates, even though the documents are successfully created, the tags are not being replaced. Instead, I get the original template with placeholders still intact, and I receive an error like: Error processing line 2: The document is inaccessible. Please try again later.
Here’s a breakdown of my process:
I retrieve the data from Google Sheets.
For each row, I create a copy of the template, placing it in a student-specific folder.
I attempt to replace the placeholders (e.g., {{FirstName}}, {{LastName}}, {{CreationDate}}) with the student’s information.
What I’ve tried so far:
Ensured the permissions for the templates are set correctly (I’m the owner of all documents).
Added delays (Utilities.sleep) after creating the document copy to make sure the document is ready before attempting to replace the tags.
Double-checked the placeholders in the templates to make sure they exactly match the tags in the script.
The issue seems to occur only with certain templates (such as “Attestation of Completion”), even though they are structured the same as the “Convention” template, which works perfectly.
Key questions:
Why does the script generate documents but fail to replace the tags in some templates?
What could be causing the “document is inaccessible” error, even though the file is created and accessible manually?
Is there anything I’m missing in terms of how Google Apps Script handles document copying and replacing text?
Any help or insight would be greatly appreciated! Thanks in advance!
MY STUDY is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3