I have a script that goes through a spreadsheet, retrieves values from the cells and inserts the values in a doc. The code works perfectly but I've noticed that when I make a copy of the doc, run the same code and pass the doc ID within the code I get the following error:
No item with the given ID could be found, or you do not have permission to access it.
I've tried to make the doc public but I get the same error. Interestingly if I create a new doc and pass that doc ID to the function it works just fine.
the code:
var templateid = "1IrEbukq3cVKg9MAPT-Aanfe4XCzoc-RCJKq6sOpQKGU"; // get template file id
var copyDoc = DocsList.getFileById(templateid).makeCopy(docName);
I'm not sure I understood exactly when you problem occurs but I'd guess it is when you try to access copyDoc.
This is because you try to access it by its ID (as it appears from the error message) and you don't have it : from the code you write here copyDoc is a document, if you want its Id just add .getId()
at the end like this :
var copyDoc = DocsList.getFileById(templateid).makeCopy(docName).getId();
Hoping I had the right guess ;-)
You could also do that like this so you'll have everything ready to use :
var doctemplate = DocumentApp.openById("1IrEbukq3cVKg9MAPT-Aanfe4XCzoc-RCJKq6sOpQKGU");// this can be placed outside the function as a global variable
var docname="new name for the copy";
var copydoc=DocsList.copy(doctemplate,docname);
var copydocId = copydoc.getId();
Edit : corrected error in code following jwesonga comment (thx)