Sli*_*ime 5 google-docs google-docs-api
我想使用Google Docs API将页眉和页脚添加到现有的 Google Docs 文件。
查看documents.batchUpdate(链接),我们可以插入文本、替换文本、添加图像和表格等,方法是在 JSON 有效负载中使用 等对象提及它们replaceAllText,insertText但我找不到任何插入页眉和页脚的方法。
问题1:如何添加页眉和页脚?
问题2:如何为文档首页添加不同的页眉/页脚?
如果不可能,我们将非常感谢建议使用任何其他 API 或方法(例如使用 MS Word)。先感谢您。
这个答案怎么样?
关于这个问题,下面的流程怎么样?
首先,需要使用浏览器手动打开Google文档中的页眉和页脚。这样,似乎可以创建页眉ID和页脚ID。
下一步,它使用Docs API 的documents.get 方法检索页眉ID 和页脚ID。终点如下。
GET https://docs.googleapis.com/v1/documents/{documentId}?fields=footers%2Cheaders
Run Code Online (Sandbox Code Playgroud)
它使用Docs API的documents.batchUpdate方法更新页眉和页脚。端点和请求正文如下。
POST https://docs.googleapis.com/v1/documents/{documentId}:batchUpdate
{
"requests": [
{
"insertText": {
"location": {
"segmentId": "kix.#####",
"index": 1
},
"text": "sample text"
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
kix.#####of"segmentId": "kix.#####"是页眉 ID 和页脚 ID。这样就可以设置文本了。index是插入文本的位置。关于这个问题,下面的流程怎么样?
之后的流程与问题1的答案相同。
https://www.googleapis.com/auth/documents作为范围。如果我误解了你的问题并且这不是你想要的方向,我很抱歉。
当我回答这个问题时,无法使用 Google Docs API 添加页眉和页脚。在现阶段,可以使用 Google Docs API 添加这些内容。我有一位联系人要求更新此信息。因此,我添加了使用 Google Docs API 将页眉和页脚添加到 Google 文档的方法。
在现阶段,当您想使用Docs API向Google文档添加页眉和提示符时,似乎需要使用2次API调用,因为似乎无法手动给出页眉ID和页脚ID。
URL 和请求正文如下。
POST https://docs.googleapis.com/v1/documents/{documentId}:batchUpdate
{
"requests": [
{
"createHeader": {
"sectionBreakLocation": {
"index": 0
},
"type": "DEFAULT"
}
},
]
}
Run Code Online (Sandbox Code Playgroud)
由此,返回以下响应。
{
"replies": [
{
"createHeader": {
"headerId": "kix.###"
}
}
],
"writeControl": {
"requiredRevisionId": "###"
},
"documentId": "###"
}
Run Code Online (Sandbox Code Playgroud)
当你想将文本插入标题时,它使用headerId如下。
POST https://docs.googleapis.com/v1/documents/{documentId}:batchUpdate
{
"requests": [
{
"insertText": {
"location": {
"segmentId": "kix.###", <--- headerId
"index": 0
},
"text": "sample text"
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
URL 和请求正文如下。
POST https://docs.googleapis.com/v1/documents/{documentId}:batchUpdate
{
"requests": [
{
"createFooter": {
"type": "DEFAULT"
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
由此,返回以下响应。
{
"replies": [
{
"createFooter": {
"footerId": "kix.###"
}
}
],
"writeControl": {
"requiredRevisionId": "###"
},
"documentId": "###"
}
Run Code Online (Sandbox Code Playgroud)
当你想将文本插入标题时,它使用footerId如下。
POST https://docs.googleapis.com/v1/documents/{documentId}:batchUpdate
{
"requests": [
{
"insertText": {
"location": {
"segmentId": "kix.###", <--- footerId
"index": 0
},
"text": "sample text"
}
}
]
}
Run Code Online (Sandbox Code Playgroud)