如何使用Google Docs API将页眉/页脚添加到 [英] How to add Header/Footer to with Google Docs API
问题描述
我想使用 Google Docs API Google Docs API .
I want to add header and footer to an existing Google Docs file using Google Docs API.
查看documents.batchUpdate
(链接),我们可以通过在replaceAllText
,insertText
等对象提及文本来插入文本,替换文本,添加图像和表格等,但是我找不到任何插入页眉和页脚的方法.
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.
问题1:如何添加页眉和页脚?
Question 1: How can I add header and footer?
问题2:如何为文档的首页添加不同的页眉/页脚?
Question 2: How can I add different header/footer for the first page of document?
如果不可能的话,建议您使用其他任何API或方法(例如使用MS Word).先感谢您.
If not possible, suggestion to use any other API or method (like using MS Word) will be much appreciated. Thank you in advance.
推荐答案
此答案如何?
- 您要将页眉和页脚添加到Google文档中.
关于这个问题,接下来的流程如何?
About this question, how about the following flow?
-
首先,需要使用浏览器手动打开Google Document中的页眉和页脚.这样,似乎可以创建页眉ID和页脚ID.
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.
- 即使文档仅具有1页,当不同的第一页"被选中,2的id第一页的和其它的网页针对页眉和页脚自动创建.
- 我认为这可能是当前的规范.
- 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.
下一步,它使用Docs API的documents.get方法检索页眉ID和页脚ID.端点如下.
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
- 从此结果中,可以检索页眉ID和页脚ID.
- 您可以看到2个ID用于页眉和页脚.
- 当您手动将文本设置为页眉和页脚时,如果选中第一页不同",则每个ID均可用于第一页以及除第一页以外的其他页面. (这是您的问题2的答案.)
- 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.)
它使用Docs API的documents.batchUpdate方法更新页眉和页脚.端点和请求正文如下.
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.#####
是页眉ID和页脚ID.这样,可以设置文本. -
index
是插入文本的位置. 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.- 您想了解在第一页和其他页面之间设置不同值的方法.
- 首先,需要使用浏览器在Google文档中手动打开页眉和页脚.这样,似乎可以创建页眉ID和页脚ID.
- 即使文档仅具有1页,当不同的第一页"被选中,2的id第一页的和其它的网页针对页眉和页脚自动创建.
- 设置了第一页和第二页的页眉和页脚的不同示例文本后,您可以轻松找到ID.
- 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.
- 当您请求上述端点时,请使用OAuth2和服务帐户检索到的访问令牌.并且请使用
https://www.googleapis.com/auth/documents
作为范围. - 关于创建页眉和页脚的ID,我尝试仅使用API对新文档进行处理.但是在目前阶段,我还无法实现.我道歉.
- 当前结果如下.使用带有Google Apps脚本的文档服务添加页眉和页脚时,不会创建ID.仅当手动打开页眉和页脚时,才会创建ID.
- 从这个结果来看,在当前阶段,我认为这可能是规范.
- 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.
- documents.get
- documents.batchUpdate
- InsertTextRequest
如果我误解了你的问题,而这不是你想要的方向,我深表歉意.
If I misunderstood your question and this was not the direction you want, I apologize.
这篇关于如何使用Google Docs API将页眉/页脚添加到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
"segmentId": "kix.#####"
的问题2:
关于这个问题,接下来的流程如何?
About this question, how about the following flow?
此后,流程与问题1的答案相同.
After this, the flow is the same with the answer for Question 1.