简体   繁体   English

我们如何使用 google docs api 和 php 在 google docs 中插入多个文本和段落?

[英]How can we insert multiple texts and paragraphs in google docs using google docs api with php?

I want to insert more the one texts in google docs with multiple paragraph contents and also want to style them.我想在具有多个段落内容的 google 文档中插入更多的一个文本,并且还想设置它们的样式。 I also followed this link https://developers.google.com/docs/api/reference/rest/v1/documents/request but I am unable to achieve this.我也点击了这个链接https://developers.google.com/docs/api/reference/rest/v1/documents/request但我无法实现这一点。

$requests [] = new Google_Service_Docs_Request(
[
    'insertText' => [
        'text' => 'Sample1\n',
        'location' => [
            'index' => 1
        ]
    ]
],
  [
    'insertText' => [
      'text' => 'sample2\n',
      'location' => [
        'index' => 9
      ]
    ]
  ],
  [
    'updateParagraphStyle' => [
      'range' => [
        'startIndex' => 1,
        'endIndex' => 8
      ],
      'paragraphStyle' => [
        'namedStyleType' => 'HEADING_1'
      ],
      'fields' => 'namedStyleType'
    ]
  ],
  [
    'updateParagraphStyle' => [
      'range' => [
        'startIndex' => 9,
        'endIndex' => 17
      ],
      'paragraphStyle' => [
        'namedStyleType' => 'NORMAL_TEXT'
      ],
      'fields' => 'namedStyleType'
  ]
  ],
  [
    'updateTextStyle' => [
      'range' => [
        'startIndex' => 9,
        'endIndex' => 16
      ],
      'textStyle' => [
        'link' => [
          'url' => 'https://www.google.com'
        ]
      ],
      'fields' => 'link'
    ]
  ]
);
$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(
    'requests' => $requests
));

$response = $service->documents->batchUpdate($documentId, $batchUpdateRequest);

I am doing in this way and getting this error我这样做并收到此错误

PHP Fatal error:  Uncaught Google\Service\Exception: {
  "error": {
    "code": 400,
    "message": "Invalid requests[0]: No request set.",
    "errors": [
      {
        "message": "Invalid requests[0]: No request set.",
        "domain": "global",
        "reason": "badRequest"
      }
    ],
    "status": "INVALID_ARGUMENT"
  }
}

Can anyone help me out with this.谁能帮我解决这个问题。 It would be a great help and i need it in PHP code.这将是一个很大的帮助,我需要它在 PHP 代码中。

Sample1\n of 'text' => 'Sample1\n', is converted to Sample1\\n . 'text' => 'Sample1\ Sample1\n 'text' => 'Sample1\n',转换为Sample1\\n I thought that this might be the reason of your issue.我认为这可能是您遇到问题的原因。 In this case, how about the following modification using PHP_EOL ?在这种情况下,使用PHP_EOL进行以下修改怎么样?

PHP_EOL : The correct 'End Of Line' symbol for this platform. PHP_EOL :此平台的正确“行尾”符号。

Modified script:修改脚本:

$requests = [
    new Google_Service_Docs_Request([
        'insertText' => [
            'text' => 'Sample1' . PHP_EOL, // Modified
            'location' => [
                'index' => 1
            ]
        ]
    ]),
    new Google_Service_Docs_Request([
        'insertText' => [
            'text' => 'Sample2' . PHP_EOL, // Modified
            'location' => [
                'index' => 9 // Modified
            ]
        ]
    ])
];

or, please enclose the text by the double quotes as follows.或者,请用双引号将文本括起来,如下所示。

$requests = [
    new Google_Service_Docs_Request([
        'insertText' => [
            'text' => "Sample1\n", // Modified
            'location' => [
                'index' => 1
            ]
        ]
    ]),
    new Google_Service_Docs_Request([
        'insertText' => [
            'text' => "Sample2\n", // Modified
            'location' => [
                'index' => 9 // Modified
            ]
        ]
    ])
];
  • By this modification, 'Sample1'. PHP_EOL通过此修改, 'Sample1'. PHP_EOL 'Sample1'. PHP_EOL and "Sample1\n" are used as Sample\n . 'Sample1'. PHP_EOL"Sample1\n"用作Sample\n By this, I thought that your goal can be achieved.通过这个,我认为你的目标可以实现。
  • In this case, index of the 2nd request is 9 that you have used in the first script.在这种情况下,第二个请求的index是您在第一个脚本中使用的9

References:参考:

  • Predefined Constants for PHP PHP 的预定义常量

  • Single quoted 单引号

    To specify a literal single quote, escape it with a backslash ().要指定文字单引号,请使用反斜杠 () 将其转义。 To specify a literal backslash, double it (\).要指定文字反斜杠,请将其加倍 (\)。 All other instances of backslash will be treated as a literal backslash: this means that the other escape sequences you might be used to, such as \r or \n, will be output literally as specified rather than having any special meaning.反斜杠的所有其他实例将被视为文字反斜杠:这意味着您可能习惯的其他转义序列(例如 \r 或 \n)将按照指定的字面意思为 output,而不具有任何特殊含义。

  • Double quoted 双引号

    If the string is enclosed in double-quotes ("), PHP will interpret the following escape sequences for special characters:如果字符串包含在双引号 (") 中,PHP 将解释以下特殊字符的转义序列:

I think the error is caused by the construction of the batch request.我认为错误是由批量请求的构造引起的。 The request array must contain a number of instances of Google_Service_Docs_Request .请求数组必须包含多个Google_Service_Docs_Request实例。

Try change this:尝试改变这个:

$requests [] = new Google_Service_Docs_Request(
[
    'insertText' => [
        'text' => 'Sample1\n',
        'location' => [
            'index' => 1
        ]
    ]
],
  [
    'insertText' => [
      'text' => 'sample2\n',
      'location' => [
        'index' => 9
      ]
    ]
  ],

To this:对此:

$requests = [
    new Google_Service_Docs_Request([
    'insertText' => [
        'text' => 'Sample1\n',
        'location' => [
            'index' => 1
        ]
    ]
    ]),
    new Google_Service_Docs_Request([
    'insertText' => [
      'text' => 'sample2\n',
      'location' => [
        'index' => 9
      ]
    ])
];

And after proceed as follows:然后进行如下操作:

$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(['requests' => $requests]);
$result = $service->documents->batchUpdate($documentId, $batchUpdateRequest);

Documentation:文档:

暂无
暂无

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

相关问题 我们如何使用 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? 我们如何使用 PHP 代码在 google docs api 中插入 header 和页脚 - How we can insert header and footer in google docs with google docs api using PHP code 我们如何使用谷歌文档 api 和 PHP 在同一行中对齐图像和文本? - How can we align an image and texts in same single line using google docs api with 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 can we insert text into Header/Footer after creating Header/Footer in 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? 我们如何使用 php 更改 google docs api 表中整行的背景颜色? - How we can change the background color for the entire row in table with google docs api using php? 使用 Google 文档 API - Using the Google Docs API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM