Home > Enterprise >  Document is closed, its contents cannot be updated
Document is closed, its contents cannot be updated

Time:12-02

I'm writing update code for my add-on. That is to create a Google Docs file that inserts tables, paragraphs, images.... When finished, get the page number of the document, if the page number is odd, insert a page break at the end. Initially I used the following code:

var data = doc.getAs("application/pdf").getDataAsString();
var pages = data.match(/\/Contents/g).length;
    Logger.log(pages);
 if(pages % 2 !== 0)
{
  doc.getBody().insertPageBreak(body.getChildIndex(body.appendParagraph("")));
}

But the result was not as expected. No matter how many pages the document has, I only get a page count of 1. Then I added doc.saveAndClose();, to get the following code:

doc.saveAndClose();
var data = doc.getAs("application/pdf").getDataAsString();
var pages = data.match(/\/Contents/g).length;
    Logger.log(pages);
 if(pages % 2 !== 0)
{
  doc.getBody().insertPageBreak(body.getChildIndex(body.appendParagraph("")));
}

Great, the program runs perfectly. The page number is taken correctly and a page break is inserted when the page number is odd. However, when I copy this code and paste it into the App script editor of another Google account, it doesn't run. The error is Exception: Document is closed, its contents cannot be updated.. I guarantee that the code is copied and not edited. I can't understand why with the same code, when running in another Google account, it gives an error. I really need your help. Thank you.

My complete code is as follows:

function stepFiles(continuationToken) {
  var folderID = PropertiesService.getDocumentProperties().getProperty('folder_ID');
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var SpssId = ss.getSheetByName('title').getRange('AH6').getValue();
  var destinationSpss1 = SpreadsheetApp.openById(SpssId);
  if(continuationToken) {
    var files = DriveApp.continueFileIterator(continuationToken);
  }
  else {
    var files = DriveApp.getFolderById(folderID).getFilesByType(MimeType.GOOGLE_DOCS);
  }
var k = 0;
while (files.hasNext() && k<1) {
  var file = files.next();
  var fileName = file.getName();
  var l = fileName.replace(/\D/g,'');
  var g= l;
 // Logger.log(l);
  var ss=SpreadsheetApp.getActiveSpreadsheet();
  var de=ss.getSheetByName('tests');
  var sol=SpreadsheetApp.openById(ss.getSheetByName('title').getRange('A77').getValue()).getSheetByName('Solution');
 //var doc=DocumentApp.openById(file.getId());
var doc = DocumentApp.create(l);
var docId = doc.getId();
var docIdInss=SpreadsheetApp.openById(ss.getSheetByName('title').getRange('A77').getValue()).getSheetByName('link').getRange('A' l).setValue(docId);
body=doc.getBody();
body.appendParagraph('text....');


//Insert elaments .......


body.appendParagraph('------------ Eand ------------').setAttributes(style1).setBold(true);

body.removeChild(body.getChild(2*(2*de.getLastRow()-1)));
body.insertParagraph(2*(2*de.getLastRow()-1),"").setAttributes(style);
body.removeChild(body.getChild(4*(de.getLastRow()-1)));
body.insertParagraph(4*(de.getLastRow()-1),"").setAttributes(style);
var name=doc.addFooter().insertParagraph(0,'Mã đề ' ll).setBold(false).setAttributes(style2);

doc.saveAndClose();
var data = doc.getAs("application/pdf").getDataAsString();
var pages = data.match(/\/Contents/g).length;
    Logger.log(pages);
 if(pages % 2 !== 0)//Nếu số trang là số lẻ thì
{
  //Chèn một ngắt trang
  doc.getBody().insertPageBreak(body.getChildIndex(body.appendParagraph("")));
}
   k  ;
      if(k==1){
            return files.getContinuationToken();
    }
  }
}

CodePudding user response:

The error message is explained in the saveAndClose() documentation:

Saves the current Document. Causes pending updates to be flushed and applied.

A closed Document can't be edited. Use DocumentApp.openById() to reopen a given document for editing.

Since you closed the document you cannot use the doc variable to edit it unless you open it again.

The reason why you were getting a page count of 1 without using saveAndClose() is because doc is created as a blank document and you need to save your changes first.

As mentioned in the documentation I quoted above, you can reopen the doc with DocumentApp.openById(). You'll need to reassign the doc variable and also the body variable, since the body variable that you are referring to is the one that was created before the changes were saved. It might look something like this:

  doc.saveAndClose();
  doc = DocumentApp.openById(doc.getId()) //reopen the document
  body = doc.getBody() // get the new body
  var data = doc.getAs("application/pdf").getDataAsString();
  var pages = data.match(/\/Contents/g).length;
  Logger.log(pages);
  if (pages % 2 !== 0)
  {
    doc.insertPageBreak(body.getChildIndex(body.appendParagraph("")));
  }
  • Related