pageBreaks in google docs with NodeJS and 'googleapis' failing

23 views Asked by At

I am creating a google doc from nodejs, and it works for the most part. The only part I cant get to work is creating page breaks. This is what im trying:

const docsService = google.docs({ version: 'v1', auth: jwt });
const driveService = google.drive({ version: 'v3', auth: jwt });

const fileMetadata = {
  name: 'New Google Doc',
  mimeType: 'application/vnd.google-apps.document',
};

const file = await driveService.files.create({
  requestBody: fileMetadata,
  media: { mimeType: 'text/html', body: html, },
});

This is all fine and works - a file is created with the html in it. It looks great except there are no page breaks. To work around this I label each break as 'BREAK' and then im going to search the document and replace the BREAK points with an insertPageBreak request which will be run through a batchUpdate:

docsService.documents.get({
  documentId: file.data.id,
}, (err, res) => {
  if (err) { console.error('Error retrieving document:', err); return; }

  const documentContent = res.data;

  // Search for keywords and create requests
  const requests = [];

  let index = 0;

  for (const element of documentContent.body.content) {
    if (element.paragraph && element.paragraph.elements && /BREAK\s*$/i.test(element.paragraph.elements[0].textRun.content)) {
      requests.push({
        insertPageBreak: { location: { index }, },
      });
    }
    index++;
  }

  // Apply page breaks
  if(requests.length > 0) {
    docsService.documents.batchUpdate({ documentId: file.data.id, requests, }
    ,(err, res) => {
      if (err) console.error('Error inserting page breaks:', err.errors);
      else {
        return `https://docs.google.com/document/d/${file.data.id}/edit`;
      }
    });
  }
});

the requests object looks like this:

[{"insertPageBreak":{"location":{"index":42}}},{"insertPageBreak":{"location":{"index":205}}},{"insertPageBreak":{"location":{"index":247}}},{"insertPageBreak":{"location":{"index":289}}},{"insertPageBreak":{"location":{"index":452}}},{"insertPageBreak":{"location":{"index":499}}}]

which should be perfect, but unfortunatele it doesnt work, giving the error:

Error inserting page breaks: [ { message: Invalid JSON payload received. Unknown name "requests[insertPageBreak][location][index]": Cannot bind query parameter. Field 'requests[insertPageBreak][location][index]' could not be found in request message., reason: 'invalid' }

I have been consulting heavily with BARD who told me the insertPageBreak method didnt get created until docs v1.5, and as you can see im using v1. However, if I change this to v1.5, or v2 then it doesnt recognise it. I dont know how to update only docs (or if its possible) as the googleapis package is the latest version.

I have tried it with a single request to no luck, and even something bard tells me was available in v1, insertText. This gives the same error, except it compains about insertText as opposed to insertPageBreak.

Thanks, happy to provide more info if needed.

Update

So I tried using googles api tool (https://developers.google.com/docs/api/reference/rest/v1/documents/batchUpdate) to test it out and it worked from there, using the same credentials and the exact same requests object - thus this is not an issue with the requests object but rather one of:

  • The API module (googleapis) being not the right version
  • The way it is being called from the code

A few other posts on here suggest it should be:

resources: requests

but I think that must be outdated as everything else suggests its requests and resources doesnt work either.

Note I have also tried setting up the requests with " and ' surrounding the keys and it made not difference (also a suggestion sfrom an older SO post)

0

There are 0 answers