简体   繁体   English

我们如何使用 google docs api 和 PHP 在 google docs 中插入表格后插入多个段落?

[英]How we can insert multiple paragraphs after the insertion of table in google docs using google docs api with PHP?

I am working on google docs api.I want to create a google docs with google docs api using PHP.AS per my requirement I want some paragraphs then insert a table and then again a paragraph as shown in below picture-我正在处理 google 文档 api。我想使用 PHP 创建一个带有 google 文档 api 的 google 文档。根据我的要求,我想要一些段落,然后插入一个表格,然后再插入一个段落,如下图所示- 在此处输入图像描述

I am able to insert texts like this -我能够插入这样的文本 -

$requests[] = new Google_Service_Docs_Request(array(
            'insertText' => [
                'text' => 'Paragraph 1',
                'location' => [
                    'index' => 1,
                ]
            ]
        ));

I am also able to add table like this-我也可以像这样添加表格 -

$requests [] = new Google_Service_Docs_Request([
    'insertTable' => [
        'rows' =>  4,
        'columns' =>  4,
        'endOfSegmentLocation' => [
          'segmentId' => ''
        ],
    ],
]);

but after adding this table I want to add another paragraph as shown in pic.I have added another insertion text request after this table request like this -但在添加此表后我想添加另一个段落,如图所示。我在这个表请求之后添加了另一个插入文本请求,如下所示 -

$requests[] = new Google_Service_Docs_Request(array(
            'insertText' => array(
                'text' => "Paragraph 2", 
                'endOfSegmentLocation' => array(
                    'segmentId' => '',
                    ),
            ),
        ));

But like this i want to add more paragraphs after the insertion of table and for that I need index location but I am not able to do that.但是像这样我想在插入表格后添加更多段落,为此我需要索引位置但我无法做到这一点。 Can anybody help me on this how to add contents like this as shown in above pic.It would be a great huge help if anyone can help me through this.I want this in PHP.任何人都可以帮助我如何添加如上图所示的内容。如果有人可以帮助我解决这个问题,那将是一个巨大的帮助。我想要这个在 PHP。

In your situation, I thought that when the contents are inserted with the reverse order, index can be ignored.在你的情况下,我认为当内容以相反的顺序插入时, index可以被忽略。 In this case, how about the following sample script?在这种情况下,下面的示例脚本怎么样?

Sample script 1:示例脚本 1:

$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);

Sample script 2:示例脚本 2:

Or, if you want to append the text of "Paragraph 2" after the text "Paragraph 1" and a table were inserted, you can append the text using the following script.或者,如果要在插入文本“第 1 段”和表格后插入“第 2 段”的文本 append,则可以使用以下脚本插入 append 文本。

$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);
  • By this request, Paragraph 2 can be appended to the document body.应此要求,可将Paragraph 2附加到文件正文中。 Also, In this case, index for putting the text can be ignored.此外,在这种情况下,可以忽略用于放置文本的index
  • About 'text' => "Paragraph 2" , when the paragraph is the same with the last paragraph, you can create new paragraph by 'text' => "\nParagraph 2" .关于'text' => "Paragraph 2" ,当段落与上一段相同时,可以通过'text' => "\nParagraph 2"创建新段落。

Sample script 3:示例脚本 3:

If you want to insert the text by retrieving the index, you can also the following sample script.如果你想通过检索索引来插入文本,你也可以使用下面的示例脚本。

$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);

References:参考:

I don't have PHP in me so I can only provide you the request and process on how to insert paragraphs after the table.我手头没有PHP,所以只能给大家提供表格后插入段落的要求和流程。

Sample Doc:样本文件:

样本

After inserting your table, you need to get the document's last endIndex by using get request and pass body/content/endIndex as your field parameter values.插入表格后,您需要使用get请求get文档的最后一个endIndex ,并将body/content/endIndex作为您的字段参数值传递。 This will return this response from using the above sample doc:这将从使用上述示例文档返回此响应:

{
  "body": {
    "content": [
      {
        "endIndex": 1
      },
      {
        "endIndex": 576
      },
      {
        "endIndex": 614
      },
      {
        "endIndex": 615
      }
    ]
  }
}

You have to iterate all the endIndex from the response and get the last one (eg 615 ).您必须从响应中迭代所有endIndex并获取最后一个(例如615 )。 Subtract 1 from the last endIndex value and then pass it as index location in the next insert paragraph response.从最后一个endIndex值中减去1 ,然后将其作为下一个插入段落响应中的索引位置传递。

Sample request:样品要求:

{
  "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"
      }
    }
  ]
}

Output: Output:

输出

Note:笔记:

  • I have enclosed the text with \n at both ends to have some space.我在两端用\n将文本括起来以留出一些空间。
  • You can also append all your paragraphs into one request so you wouldn't have to get the last endIndex everytime.您还可以将所有段落 append 放入一个请求中,这样您就不必每次都get最后一个endIndex
  • You can also use offsets by counting every character you add so that you can follow what index you should use next.您还可以通过计算添加的每个字符来使用偏移量,以便您可以遵循接下来应该使用的索引。
  • As for the paragraph formatting like having indention and aligning the paragraph to right, see changing paragraph formatting and see the sample request.至于段落格式,如缩进和右对齐段落,请参阅更改段落格式并查看示例请求。 If you have a question regarding this, I recommend you post a separate post for it.如果您对此有疑问,我建议您为此单独发帖。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们如何使用 google docs api 和 php 在 google docs 中插入多个文本和段落? - How can we insert multiple texts and paragraphs in google docs using google docs api with php? 我们如何使用 PHP 代码在 google docs api 中插入 header 和页脚 - How we can insert header and footer in google docs with google docs api using PHP code 在使用 PHP 在 google docs api 中创建页眉/页脚后,我们如何将文本插入页眉/页脚? - How can we insert text into Header/Footer after creating Header/Footer in google docs api using PHP? 我们可以使用谷歌文档 api 和 PHP 将 html 内容插入谷歌文档吗? - Can we insert html contents into google docs using google docs api with PHP? 我们如何使用谷歌文档 api 和 PHP 将谷歌文档下载到我们的本地(计算机)/硬盘? - How we can download a google docs into our local (computer)/hard drive using google docs api with PHP? 我们如何使用 php 更改 google docs api 表中整行的背景颜色? - How we can change the background color for the entire row in table with google docs api using php? 如何使用谷歌文档插入文本 api php - How to insert text with google docs api php 我们如何更改编辑/作者的权限,使其不使用谷歌文档 API 共享谷歌文档? - How we can change the permissions for editors/writers for not sharing the google docs using google docs API? 我们如何使用谷歌文档 api 和 PHP 在同一行中对齐图像和文本? - How can we align an image and texts in same single line using google docs api with PHP? 使用 Google 文档 API - Using the Google Docs API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM