I want to add header and footer to an existing Google Docs file using Google Docs API.
Looking at the documents.batchUpdate
(link) we can insert text, replace text, add images and table etc by mentioning them in the JSON payload with objects like replaceAllText
, insertText
etc but I cannot find any way to insert header and footer.
Question 1: How can I add header and footer?
Question 2: How can I add different header/footer for the first page of document?
If not possible, suggestion to use any other API or method (like using MS Word) will be much appreciated. Thank you in advance.
How about this answer?
Question 1:
- You want to add the header and footer to Google Document.
About this question, how about the following flow?
Flow:
At first, it is required to manually open the header and footer in Google Document with your browser. By this, it seems that the header ID and footer ID can be created.
- Even if the Document has only one page, when "Different first page" is checked, 2 ID s of 1st page and other pages are automatically created for the header and footer.
- I think that this might be the current specification.
- When the different sample texts for header and footer of 1st page and 2nd page are set, you can easily find the IDs.
As the next step, it retrieves the header ID and footer ID using the method of documents.get of Docs API. The endpoint is as follows.
GET https://docs.googleapis.com/v1/documents/{documentId}?fields=footers%2Cheaders
- From this result, the header ID and footer ID can be retrieved.
- You can see 2 IDs for the header and footer.
- When you manually set the text to the header and footer, if you checked "Different first page", each ID can be used for the 1st page and other pages except for 1st page. (This is the answer for your Question 2.)
It updates the header and footer using the method of documents.batchUpdate of Docs API. The endpoint and request body are as follows.
POST https://docs.googleapis.com/v1/documents/{documentId}:batchUpdate
{
"requests": [
{
"insertText": {
"location": {
"segmentId": "kix.#####",
"index": 1
},
"text": "sample text"
}
}
]
}
kix.#####
of "segmentId": "kix.#####"
is the header ID and footer ID. By this, the text can be set.
index
is the position of inserted text.
Question 2:
- You want to know the method for setting the different values between 1st page and other pages.
About this question, how about the following flow?
Flow:
- At first, it is required to manually open the header and footer in Google Document with your browser. By this, it seems that the header ID and footer ID can be created.
- Even if the Document has only one page, when "Different first page" is checked, 2 ID s of 1st page and other pages are automatically created for the header and footer.
- When the different sample texts for header and footer of 1st page and 2nd page are set, you can easily find the IDs.
After this, the flow is the same with the answer for Question 1.
Note:
- When you request above endpoints, please use the access token retrieved by OAuth2 and Service account. And the please use
https://www.googleapis.com/auth/documents
as the scope.
- About creating the IDs of header and footer, I have tried to do it to new Document using only API. But in the current stage, I couldn't achieve it yet. I apologize.
- The current result is as follows. When the header and footer are added using Document Service with Google Apps Script, the IDs are not created. Only when the manually open the header and footer, the IDs are created.
- From this result, in the current stage, I thought that this might be the specification.
References:
- documents.get
- documents.batchUpdate
- InsertTextRequest
If I misunderstood your question and this was not the direction you want, I apologize.