Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I also followed this link https://developers.google.com/docs/api/reference/rest/v1/documents/request but I am unable to achieve this.

$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.

like image 217
Taniya Halder Avatar asked Nov 18 '25 12:11

Taniya Halder


1 Answers

Sample1\n of 'text' => 'Sample1\n', is converted to 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: The correct 'End Of Line' symbol for this platform.

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 and "Sample1\n" are used as 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.

References:

  • 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:

like image 98
Tanaike Avatar answered Nov 20 '25 03:11

Tanaike



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!