簡體   English   中英

如何/在何處在 Google docs API 中添加插入文本功能

[英]How to/where to add the insert text function in Google docs API

我從 google api 復制粘貼,在我的代碼中,我使用令牌驗證用戶,然后立即執行此代碼

text1="text"
text2="bruh"
text3="reee"
requests = [
     {
        'insertText': {
            'location': {
                'index': 25,
            },
            'text': text1
        }
    },
             {
        'insertText': {
            'location': {
                'index': 50,
            },
            'text': text2
        }
    },
             {
        'insertText': {
            'location': {
                'index': 75,
            },
            'text': text3
        }
    },
]

result = service.documents().batchUpdate(
    documentId=DOCUMENT_ID, body={'requests': requests}).execute()

但我不斷收到一個錯誤代碼,上面寫着

    "Invalid requests[0].insertText: Index 25 must be less than the end index of the referenced segment, 2.

錯誤消息告訴您,您嘗試將文本插入的段只有兩個字符長,並且您不能在索引 25 處插入任何內容,因為兩個字符長的字符串只有索引 0 和 1。如果您更改索引為 0 或 1,它應該可以工作(至少對於第一個請求)。

文檔中解釋的開始和結束索引

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM