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

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

我想在 google 具有多个段落内容的文档中插入更多的一个文本,并且还想设置它们的样式。我也跟着这个 link 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);

我这样做并得到这个错误

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

谁能帮我解决这个问题。这将是一个很大的帮助,我在 PHP 代码中需要它。

我认为错误是由批量请求的构造引起的。请求数组必须包含 Google_Service_Docs_Request.

的多个实例

尝试改变这个:

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

为此:

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

然后进行如下操作:

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

文档:

'text' => 'Sample1\n',

Sample1\n 转换为 Sample1\n。我认为这可能是您遇到问题的原因。在这种情况下,使用PHP_EOL进行以下修改怎么样?

PHP_EOL: The correct 'End Of Line' symbol for this platform.

修改后的脚本:

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

或者,请用双引号将文本括起来,如下所示。

$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
            ]
        ]
    ])
];
  • 通过此修改,'Sample1' . PHP_EOL"Sample1\n"被用作Sample\n。至此,我认为您的目标可以实现。
  • 在这种情况下,第二个请求的 index 是您在第一个脚本中使用的 9

参考文献:

  • Predefined Constants for 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.

  • Double quoted

    If the string is enclosed in double-quotes ("), PHP will interpret the following escape sequences for special characters: