onlyadmin980
onlyadmin980

Reputation: 1

Apps Script (Error Exception: The document is inaccessible. Please try again later. Creator @ Code.gs:10)

I wrote a script to automatically create and fill in the required fields in the file, but an error

Error Exception: The document is inaccessible. Please try again later. Creator @ Код.gs:10

Please look at the code, tell me where I was wrong.

function Creator() {

const docFile = DriveApp.getFileById("1DhHBAd3ssYMgbbxJatK6_URxBPdoPk2K");
const tempFolder = DriveApp.getFolderById("16keE-_Dm0gglO42Lw0IphiPHYK-_TFXH");
const pdfFolder = DriveApp.getFolderById("1ZsmW9UPNaqgjt8kFh3y9h8tdBLAtj7gC");
const tempFile = docFile.makeCopy(tempFolder);
const tempDocFile = DocumentApp.openById(tempFile.getId());
const body = tempDocFile.getBody();
var list = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var l=list.getLastRow();
for (var i=2; i <= l; i++) {
var a1 = list.getRange(i, 3).getValue();
var a2 = list.getRange(i, 4).getValue();
body.replaceText("{ПІБ}", a1);
body.replaceText("{Назва структури}", a2);
tempDocFile.saveAndClose();
const pdfFile = tempDocFile.getAs(MimeType.PDF);
pdfFolder.createFile(pdfFile).setName(a1);
tempFolder.removeFile(tempFile);
}
}

Upvotes: 0

Views: 590

Answers (1)

Wicket
Wicket

Reputation: 38349

Assuming that the code line having the error is

var l=list.getLastRow();

It's very likely that you faced a Google environment glitch (I have faced the same error this weekend). In situations like this there is nothing else to do than to wait a bit and try again.

If you are developing a script / add-on / web-app to be used by others or you would like to handle this automatically you could use an algorithm like exponential backoff

If the error occurred on the previous code lines calling the Document Service, the problem might be caused for trying to open non Google Document file i.e. Microsoft Word file (.docx). In this case you should first convert the file to Google Document format and use the converted document instead of the original.

Related

Resources

Scripts implementing the exponential backoff algorithm in Google Apps Script

Upvotes: 0

Related Questions