我开始在 Python 中探索 Google Docs API。除了一件事之外,它几乎可以完成我想要它做的所有事情。
我可以替换文档的文本,但无法更改超链接的值。
意思是如果链接看起来像这样:一个链接,我可以更改文本的值,a link但不能更改目标 URL。
我一直在浏览文档,但我找不到任何关于它的信息。它可能是一个缺失的功能还是我错过了这样做的方法?
您可以使用 Google Docs API 中 batchupdate 方法的 UpdateTextStyleRequest 修改超链接。此时,请设置的属性Link的TextStyle。
POST https://docs.googleapis.com/v1/documents/{file ID}:batchUpdate
Run Code Online (Sandbox Code Playgroud)
{
"requests": [
{
"updateTextStyle": {
"textStyle": {
"link": {
"url": "https://sampleUrl" # Please set the modified URL here.
}
},
"range": {
"startIndex": 1,
"endIndex": 2
},
"fields": "link"
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
如果这对您的情况没有用,我深表歉意。
从你的回复评论中,我可以理解如上。当我的理解正确时,您可以使用documents.get方法检索它。在这种情况下,当fields使用时,响应变得容易阅读。
GET https://docs.googleapis.com/v1/documents/{file ID}?fields=body(content(paragraph(elements(endIndex%2CstartIndex%2CtextRun(content%2CtextStyle%2Flink%2Furl)))))
Run Code Online (Sandbox Code Playgroud)
body(content(paragraph(elements(endIndex,startIndex,textRun(content,textStyle/link/url)))))用作fields.例如,当以下文本放入 Google 文档并def具有超链接时,
abc
def
Run Code Online (Sandbox Code Playgroud)
响应如下。从下面的结果中,您可以检索带有可检索超链接的文本的位置。使用它,您可以修改超链接。
{
"body": {
"content": [
{},
{
"paragraph": {
"elements": [
{
"startIndex": 1,
"endIndex": 5,
"textRun": {
"content": "abc\n",
"textStyle": {}
}
}
]
}
},
{
"paragraph": {
"elements": [
{
"startIndex": 5,
"endIndex": 8,
"textRun": {
"content": "def",
"textStyle": {
"link": {
"url": "https://sample/"
}
}
}
},
{
"startIndex": 8,
"endIndex": 9,
"textRun": {
"content": "\n",
"textStyle": {}
}
}
]
}
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1620 次 |
| 最近记录: |