首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用带有PHP的google在google文档中插入表后插入多个段落?

如何使用带有PHP的google在google文档中插入表后插入多个段落?
EN

Stack Overflow用户
提问于 2022-02-10 15:10:14
回答 2查看 253关注 0票数 0

我正在做google docs api,我想用google docs根据我的要求使用PHP.AS创建一个google文档,我想要一些段落,然后插入一个表,然后再插入一个段落,如下图所示-

我能插入这样的文字-

代码语言:javascript
运行
复制
$requests[] = new Google_Service_Docs_Request(array(
            'insertText' => [
                'text' => 'Paragraph 1',
                'location' => [
                    'index' => 1,
                ]
            ]
        ));

我还能添加这样的桌子-

代码语言:javascript
运行
复制
$requests [] = new Google_Service_Docs_Request([
    'insertTable' => [
        'rows' =>  4,
        'columns' =>  4,
        'endOfSegmentLocation' => [
          'segmentId' => ''
        ],
    ],
]);

但是在添加这个表之后,我想添加另一个段落,如图所示,我在这个表请求之后添加了另一个插入文本请求,如下所示-

代码语言:javascript
运行
复制
$requests[] = new Google_Service_Docs_Request(array(
            'insertText' => array(
                'text' => "Paragraph 2", 
                'endOfSegmentLocation' => array(
                    'segmentId' => '',
                    ),
            ),
        ));

但是像这样,我想在插入表之后添加更多的段落,为此,我需要索引位置,但我无法做到这一点。有人能帮我吗?如果有人能帮我解决这个问题的话,怎么添加像上面所示的pic.It这样的内容将是一个很大的帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-02-11 01:09:49

在您的情况下,我认为当内容以相反的顺序插入时,可以忽略index。在本例中,下面的示例脚本如何?

示例脚本1:

代码语言:javascript
运行
复制
$documentId = "###"; // Please set the Google Document ID.

$requests = [
    new Google_Service_Docs_Request(array(
        'insertText' => [
            'text' => 'Paragraph 1',
            'location' => [
                'index' => 1,
            ]
        ]
    )),
    new Google_Service_Docs_Request([
        'insertTable' => [
            'rows' =>  4,
            'columns' =>  4,
            'location' => [
              'index' => 1
            ],
        ],
    ]),
    new Google_Service_Docs_Request(array(
        'insertText' => [
            'text' => 'Paragraph 2',
            'location' => [
                'index' => 1,
            ]
        ]
    ))
];
$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(
    'requests' => array_reverse($requests)
));
$responses = $service->documents->batchUpdate($documentId, $batchUpdateRequest);

示例脚本2:

或者,如果您想在文本“第1段”之后添加“第2段”的文本并插入一个表,则可以使用以下脚本追加文本。

代码语言:javascript
运行
复制
$requests = [
    new Google_Service_Docs_Request(array(
        'insertText' => [
            'text' => "Paragraph 2", // or "\nParagraph 2"
            'endOfSegmentLocation' => [
                'segmentId' => ''
            ],
        ]
    )),
];
$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(
    'requests' => array_reverse($requests)
));
$responses = $service->documents->batchUpdate($documentId, $batchUpdateRequest);
  • 通过此请求,可以将Paragraph 2附加到文档主体。此外,在这种情况下,用于放置文本的index可以被忽略。
  • 关于'text' => "Paragraph 2",当段落与最后一段相同时,您可以通过'text' => "\nParagraph 2"创建新的段落。

示例脚本3:

如果要通过检索索引插入文本,还可以使用以下示例脚本。

代码语言:javascript
运行
复制
$documentId = "###"; // Please set the Google Document ID.

$requests = [
    new Google_Service_Docs_Request(array(
        'insertText' => [
            'text' => 'Paragraph 1',
            'location' => [
                'index' => 1,
            ]
        ]
    )),
    new Google_Service_Docs_Request([
        'insertTable' => [
            'rows' =>  4,
            'columns' =>  4,
            'endOfSegmentLocation' => [
                'segmentId' => ''
            ],
        ],
    ]),
];
$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(
    'requests' => array_reverse($requests)
));
$responses = $service->documents->batchUpdate($documentId, $batchUpdateRequest);

$obj = $service->documents->get($documentId);
$content = $obj->getBody()->getContent();
$requests = [
    new Google_Service_Docs_Request(array(
        'insertText' => [
            'text' => 'Paragraph 2',
            'location' => [
                'index' => end($content)->getEndIndex() - 1,
            ]
        ]
    ))
];
$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(
    'requests' => $requests
));
$responses = $service->documents->batchUpdate($documentId, $batchUpdateRequest);

参考文献:

票数 2
EN

Stack Overflow用户

发布于 2022-02-10 16:31:11

我没有PHP,所以我只能向您提供如何在表后插入段落的请求和处理。

医生样本:

插入表后,需要使用get请求并将body/content/endIndex作为字段参数值传递给文档的最后一个endIndex。这将从使用上面的示例文档返回此响应:

代码语言:javascript
运行
复制
{
  "body": {
    "content": [
      {
        "endIndex": 1
      },
      {
        "endIndex": 576
      },
      {
        "endIndex": 614
      },
      {
        "endIndex": 615
      }
    ]
  }
}

您必须从响应中迭代所有endIndex并获得最后一个(例如,615)。从最后一个1值中减去endIndex,然后在下一个插入段落响应中将其作为索引位置传递。

抽样请求:

代码语言:javascript
运行
复制
{
  "requests": [
    {
      "insertText": {
        "location": {
          "index": 614
        },
        "text": "\nLorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.\n"
      }
    }
  ]
}

输出:

注意:

  • 我已经在两端附上了\n的文本,以便有一些空间。
  • 您还可以将所有段落附加到一个请求中,这样您就不必每次都要get最后一个endIndex了。
  • 您还可以通过计数所添加的每个字符来使用偏移量,这样您就可以遵循下一步应该使用的索引。
  • 至于段落格式(比如缩进和对齐段落),请参见更改段落格式并查看示例请求。如果您对此有疑问,我建议您单独发布一篇文章。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71067581

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档