1

I am trying to create a copy of a template document and edit some text in it using the following code :

 const destinationFolder = DriveApp.getFolderById(folderId) //folder where to save the document 
 const copy = templateDoc.makeCopy(customDocName.toString(), destinationFolder); //make a copy of the template document
 const doc = DocumentApp.openById(copy.getId()) //open the copy 
 const body = doc.getBody();
 
 //update the template with data
 body.replaceText('<text1>', 'replacementText');
 body.replaceText('<text2>', 'replacementText'); 

I am able to create a copy of the document, rename it and save it in the specified folder . I am getting the following error at line 3 : Exception: The document is inaccessible. Please try again later.

I have looked for the solutions and have tried all but nothing worked.

Here is all that I have tried :

  • Logout and login again
  • Enabled all relevant api under resources
  • Moved the app script under a GCP project as someone suggested that we no longer can access a doc directly from app script.
  • Enabled all relevant api's in GCP
7
  • Sometime this kind of error is temporarily. If not: Make sure that the file is a Google Docs file. Also: where is the folder located? On your Drive? Shared Drive? Shared with you? Which kind of permissions do you have over the folder? Commented Dec 18, 2020 at 11:53
  • How are you defining templateDoc?
    – sebbagoe
    Commented Dec 18, 2020 at 11:59
  • @ziganotschka The folder is in my drive and I am the owner.
    – Ashwini
    Commented Dec 18, 2020 at 12:02
  • @sebbagoe templateDoc is a google doc that is present in the same folder as my script container. I am able to make a copy of template doc and save it in the specified folder. But when i try to open it using documentApp.openById() I get the mentioned error
    – Ashwini
    Commented Dec 18, 2020 at 12:04
  • The problem might also be related to being signed in with multiple accounts - please try out to sing out of all accounts but one before running the code or use incognito mode. Commented Dec 18, 2020 at 12:12

1 Answer 1

0

The issue was with the templateDoc as it was a Microsoft Word Document(.docx). When i created a Google Doc and referred it as the template doc it worked just fine. Thank you for all your help :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.