Google Script Document Inaccessible Error - creating and attempting to open document in code
Asked Answered
R

3

5

I had some old code that worked great 5 years ago in taking a Google Form and filling in a pre-designed template from the information. I haven't needed to use it in awhile, but now I have a similar project and was trying to revitalize the outdated code. I have one error I cannot figure out or find an answer to online. Please Help!

The relevant part of the code is:

//Create a copy of template

  var copy = DriveApp.getFileById(docTemplate)
            .makeCopy(docName+' TEST ')
            .getId();

// Open the temporary document

  var copyDoc = DocumentApp.openById(copy);

The first part works, a copy of the document is created in my Google Drive and var copy returns the value of the new document ID.

I get the error message "The document is inaccessible. Please try again later. (line 79, file "Code")"

line 79 of code is the : "var copyDoc = DocumentApp.openById(copy);".

I have tried a few different codes for opening a file and they are all returning the same error message.

Rommel answered 15/8, 2019 at 15:28 Comment(3)
These lines by themselves seem to work. Could it be that the variable copy is getting reset or redefined between these two lines of code?Amygdalin
I don't know why it would. I added code to email me the value of var copy after the first line and it sends me the correct ID for the new document.Rommel
I have similar problem. But in my case a trigger onSubmit() from a form and open a template doc to populate data. It can't open the document because triggers don't have permissions , but I don't see how to turn that aroundLantz
C
29

Just want to share my resolution to this problem. I know is an old thread but there may be other people in the future who will stumble upon this thread who are still trying to fix this issue.

I encountered this error just now. It turns out that the file that was cloned and tried to open after was a ".docx" file. A MS word file that was uploaded to google drive. The DocumentApp API can only open google docs files. To fix this, just open the the base file that was being cloned then File > Save as Google Docs. This will create a new file. Use that as the new base file.

Candlepower answered 15/3, 2021 at 3:54 Comment(2)
Right into the block hole. Thanks muchChappie
Awesome! I had the same error, and that fixed it! I wish the error was more descriptive. All it says is "document inaccessible." Thank you!Cullum
T
1

This works for me:

function runOne() {
  var docTemplate='id string';
  var file=DriveApp.getFileById(docTemplate);
  var copy = file.makeCopy(file.getName()+'TEST').getId();
  var copyDoc = DocumentApp.openById(copy);
  copyDoc.getBody().appendParagraph("This is new text.");
  copyDoc.saveAndClose();
}

Of course, when the script opens the document by Id it does not actually open it with a user interface it just opens it on the server. But when I opened it, sure enough that appendedParagrahp was there.

Tychonn answered 15/8, 2019 at 18:9 Comment(0)
L
0

Actually since the new versions, you can't just run the scripts... I spend the hole day on this.

https://developers.google.com/apps-script/guides/cloud-platform-projects#switching_to_a_different_standard_gcp_project

  1. You need to create a Standard project
  2. Grab the ID and give it to your script
  3. On the script enable the API's you need (Drive, Docs, etc)
  4. So do on the GCP Project

Once I've done this, files started opening files. The main problem is that a trigger on a form (or otherwise) can't open docs for security. Which is a pain since you now need to do a bunch of other stuff on top...

Lantz answered 1/9, 2019 at 19:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.