且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Google Docs API将页眉/页脚添加到

更新时间:2023-01-09 12:19:11

此答案如何?

  • 您要将页眉和页脚添加到Google文档中.

关于这个问题,接下来的流程如何?

About this question, how about the following flow?

  1. 首先,需要使用浏览器手动打开Goog​​le Document中的页眉和页脚.这样,似乎可以创建页眉ID和页脚ID.

  1. 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"
               }
              }
             ]
            }
            

              "segmentId": "kix.#####"
            • 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.
              • 问题2:

                • 您想了解在第一页和其他页面之间设置不同值的方法.
                • 关于这个问题,接下来的流程如何?

                  About this question, how about the following flow?

            1. 首先,需要使用浏览器在Google文档中手动打开页眉和页脚.这样,似乎可以创建页眉ID和页脚ID.
              • 即使文档仅具有1页,当不同的第一页"被选中,2的id第一页的和其它的网页针对页眉和页脚自动创建.
              • 设置了第一页和第二页的页眉和页脚的不同示例文本后,您可以轻松找到ID.
            1. 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.

            此后,流程与问题1的答案相同.

            After this, the flow is the same with the answer for Question 1.

            • 当您请求上述端点时,请使用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.