有没有办法替换Google Docs API中的文本的URL链接? [英] Is there a way to replace URL Link of Text in Google Docs API?
问题描述
我开始使用Python探索Google Docs API.除了一件事情,它几乎完成了我想要做的所有事情.
I started exploring Google Docs API in Python. It does pretty much everything I want it to do except for one thing.
我可以替换文档的文本,但不能更改超链接的值.
I can replace the text of a document but I can't change the value of the hyperlinks.
表示链接看起来像这样:链接,我可以更改文本的值a link
,而不是目标URL.
Meaning if a link looks like this : a link, I can change the value of the text a link
but not the target URL.
我一直在浏览文档,但找不到任何相关信息.可能是缺少的功能还是我错过了实现此功能的方法?
I've been going through the documentation but I can't find anything about it. Could it be a missing feature or am I missing the way to do that?
推荐答案
您可以使用Google Docs API中batchupdate方法的UpdateTextStyleRequest来修改超链接.此时,请设置TextStyle
的Link
的属性.
You can modify the hyperlink using UpdateTextStyleRequest of the batchupdate method in Google Docs API. At this time, please set the property of Link
of TextStyle
.
POST https://docs.googleapis.com/v1/documents/{file ID}:batchUpdate
请求正文:
{
"requests": [
{
"updateTextStyle": {
"textStyle": {
"link": {
"url": "https://sampleUrl" # Please set the modified URL here.
}
},
"range": {
"startIndex": 1,
"endIndex": 2
},
"fields": "link"
}
}
]
}
注意:
- 根据您的问题,我可以理解您已经使用过Google Docs API,并且可以修改链接文本.我认为您可以使用上面的请求正文和所拥有的脚本来修改链接.
- UpdateTextStyleRequest
- TextStyle
- Link
- 您要检索带有超链接的文本.
如果这对您的情况没有帮助,我深表歉意.
If this was not useful for your situation, I apologize.
从您的回复评论中,我可以像上面那样理解.如果我的理解正确,则可以使用documents.get
方法检索它.在这种情况下,使用fields
时,响应变得很容易阅读.
From your reply comment, I could understand like above. When my understanding is correct, you can retrieve it using documents.get
method. In this case, when fields
is used, the response become to easily read.
GET https://docs.googleapis.com/v1/documents/{file ID}?fields=body(content(paragraph(elements(endIndex%2CstartIndex%2CtextRun(content%2CtextStyle%2Flink%2Furl)))))
- 在此端点中,
body(content(paragraph(elements(endIndex,startIndex,textRun(content,textStyle/link/url)))))
用作fields
. - In this endpoint,
body(content(paragraph(elements(endIndex,startIndex,textRun(content,textStyle/link/url)))))
is used asfields
.
例如,当以下文本放入Google文档中并且def
具有超链接时,
For example, when the following texts are put in a Google Document and def
has a hyperlink,
abc
def
响应如下.从以下结果中,您可以检索文本的位置以及可以检索的超链接.使用此功能,您可以修改超链接.
The response is as follows. From the following result, you can retrieve the position of text with the hyperlink can be retrieved. Using this, you can modify the hyperlink.
{
"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": {}
}
}
]
}
}
]
}
}
参考:
- documents.get
- documents.get
Reference:
这篇关于有没有办法替换Google Docs API中的文本的URL链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!