Home > Enterprise >  Insert page breaks in Google Docs with App script
Insert page breaks in Google Docs with App script

Time:11-26

I want to create a Google Docs file, provided that the number of pages must be even. I used the following code:

function myFunction(){
var data = doc.getAs("application/pdf").getDataAsString();
    var pages = data.match(/\/Contents/g).length;
    Logger.log(pages);
if(pages % 2 !== 0)
{
  var searchText = '----- End -----';
  var res = Docs.Documents.get(docId);
  let offset = 0;
  const requests = res.body.content.reduce((ar, e) => {
    if (e.paragraph) {
      e.paragraph.elements.forEach(f => {
        if (f.textRun) {
          const re = new RegExp(searchText, "g");
          let p = null;
          while (p = re.exec(f.textRun.content)) {
            ar.push({insertPageBreak: {location: {index: p.index   offset}}});
          }
        }
      })
    }
    offset = e.endIndex;
    return ar;
  }, []).reverse();
  Docs.Documents.batchUpdate({requests: requests}, docId);
}

When running, I get the error: Preventing GoogleJsonResponseException: API call to sheets.spreadsheets.batchUpdate failed with error: Must specify at least one request.

After some research, I fixed the line

Docs.Documents.batchUpdate({requests: requests}, docId);

to

if (requests.length > 0) {
  Docs.Documents.batchUpdate({requests: requests}, docId);}
}

Now the code is running normally but in the generated Google Docs file, there are still no page breaks. I need help. Thank you.

CodePudding user response:

From your showing script, I thought that you might my this answer https://stackoverflow.com/a/65745933 . If my understanding is correct, I thought that the reason for your current issue of Preventing GoogleJsonResponseException: API call to sheets.spreadsheets.batchUpdate failed with error: Must specify at least one request. is due to that in your Google Document has no value of var searchText = '----- End -----'. For example, when you put the value of ----- End ----- to the last page of your Google Document, this script can be used. But, from your error message, I'm worried that the value might not be included in your Google Document.

So, in your situation, I thought that you might want to insert the page break to the last page. In this case, how about the following modifications? At the following modifications, the page break is inserted at the last of the document.

Pattern 1:

In this pattern, Docs API is used.

From:

var searchText = '----- End -----';
var res = Docs.Documents.get(docId);
let offset = 0;
const requests = res.body.content.reduce((ar, e) => {
  if (e.paragraph) {
    e.paragraph.elements.forEach(f => {
      if (f.textRun) {
        const re = new RegExp(searchText, "g");
        let p = null;
        while (p = re.exec(f.textRun.content)) {
          ar.push({insertPageBreak: {location: {index: p.index   offset}}});
        }
      }
    })
  }
  offset = e.endIndex;
  return ar;
}, []).reverse();
Docs.Documents.batchUpdate({requests: requests}, docId);

To:

var res = Docs.Documents.get(docId);
var requests = [{ insertPageBreak: { location: { index: res.body.content.pop().endIndex - 1 } } }];
Docs.Documents.batchUpdate({ requests }, docId);

Pattern 2:

In this pattern, Docs API is not used.

From:

var searchText = '----- End -----';
var res = Docs.Documents.get(docId);
let offset = 0;
const requests = res.body.content.reduce((ar, e) => {
  if (e.paragraph) {
    e.paragraph.elements.forEach(f => {
      if (f.textRun) {
        const re = new RegExp(searchText, "g");
        let p = null;
        while (p = re.exec(f.textRun.content)) {
          ar.push({insertPageBreak: {location: {index: p.index   offset}}});
        }
      }
    })
  }
  offset = e.endIndex;
  return ar;
}, []).reverse();
Docs.Documents.batchUpdate({requests: requests}, docId);

To:

var body = doc.getBody();
doc.getBody().insertPageBreak(body.getChildIndex(body.appendParagraph("")));
  • From your script, unfortunately, I cannot know your doc. So, I guessed doc as Class Document object. Please be careful about this.

Reference:

  • Related