0

I am create the Google Document and Insert the Multiple Image but Can not Set the Positions how to fix it laravel using Google Docs API

I am Expecting the add the InsertInlineImage Set Position that is i want to expected

    public function createGoogleDocSheet(Request $request) 
    {
        $client = new Google_Client();
        $client->setAuthConfig('../resources/google/service_account.json');
        $client->setApplicationName('Google Docs API');
        $client->setScopes([\Google_Service_Docs::DOCUMENTS,\Google_Service_Drive::DRIVE]);

        $service = new \Google_Service_Docs($client);

        $document = new Google_Service_Docs_Document(array(
            'title' => "My Doc"
        ));
        $my_document = $service->documents->create($document);
        $document_id = $my_document->documentId;

        $requests[] = new Google_Service_Docs_Request(array(
            'insertInlineImage' => array(
                'uri' => 'https://fonts.gstatic.com/s/i/productlogos/docs_2020q4/v6/web-64dp/logo_docs_2020q4_color_1x_web_64dp.png',
                'location' => array(
                    'index' => 1,
                ),
                'objectSize' => array(
                    'height' => array(
                        'magnitude' => 50,
                        'unit' => 'PT',
                    ),
                    'width' => array(
                        'magnitude' => 50,
                        'unit' => 'PT',
                    ),
                    "offsetX"=> array(
                        "magnitude"=> 50,
                        "unit" => "PT",
                    ),
                    "offsetY" => array(
                        "magnitude" => 50,
                        "unit" => "PT",
                    ),
                ),
            )
        ));

        $requests[] = new Google_Service_Docs_Request(array(
            'insertInlineImage' => array(
                'uri' => 'https://fonts.gstatic.com/s/i/productlogos/docs_2020q4/v6/web-64dp/logo_docs_2020q4_color_1x_web_64dp.png',
                'location' => array(
                    'index' => 1,
                ),
                'objectSize' => array(
                    'height' => array(
                        'magnitude' => 50,
                        'unit' => 'PT',
                    ),
                    'width' => array(
                        'magnitude' => 50,
                        'unit' => 'PT',
                    ),
                ),
            ),
            
        ));

        // Execute the requests.
        $batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(
            'requests' => $requests
        ));
        
        $response =
            $service->documents->batchUpdate($document_id, $batchUpdateRequest);
       
        $userPermission = new Google_Service_Drive_Permission(array(
            'type' => 'user',
            'role' => 'writer',
            'emailAddress' => '[email protected]'
        ));

        $driveService = new \Google_Service_Drive($client);
                    $driveService->getClient()->setUseBatch(true);

        $batch = $driveService->createBatch();

        $request = $driveService->permissions->create($document_id, $userPermission, array('fields' => 'id'));

        $batch->add($request, 'user');
        
        $results = $batch->execute();

        $data['document_id'] = $document_id;

        return response()->json([
            'status' => true,
            'status_message' => Lang::get('talent_messages.create_document_successfully'),
            'data' => $data,
        ]); 
        
    }

throw the error for "{\n "error": {\n "code": 400,\n "message": "Invalid JSON payload received. Unknown name \"offsetX\" at 'requests[0].insert_inline_image.object_size': Cannot find field.\nInvalid JSON payload received. Unknown name \"offsetY\" at 'requests[0].insert_inline_image.object_size': Cannot find field.\nInvalid JSON payload received. Unknown name \"offsetX\" at 'requests[1].insert_inline_image.object_size': Cannot find field.\nInvalid JSON payload received. Unknown name \"offsetY\" at 'requests[1].insert_inline_image.object_size': Cannot find field.",\n "errors": [\n {\n "message": "Invalid JSON payload received. Unknown name \"offsetX\" at 'requests[0].insert_inline_image.object_size': Cannot find field.\nInvalid JSON payload received. Unknown name \"offsetY\" at 'requests[0].insert_inline_image.object_size': Cannot find field.\nInvalid JSON payload received. Unknown name \"offsetX\" at 'requests[1].insert_inline_image.object_size': Cannot find field.\nInvalid JSON payload received. Unknown name \"offsetY\" at 'requests[1].insert_inline_image.object_size': Cannot find field.",\n "reason": "invalid"\n }\n ],\n "status": "INVALID_ARGUMENT",\n "details": [\n {\n "@type": "type.googleapis.com/google.rpc.BadRequest",\n "fieldViolations": [\n {\n "field": "requests[0].insert_inline_image.object_size",\n "description": "Invalid JSON payload received. Unknown name \"offsetX\" at 'requests[0].insert_inline_image.object_size': Cannot find field."\n },\n {\n "field": "requests[0].insert_inline_image.object_size",\n "description": "Invalid JSON payload received. Unknown name \"offsetY\" at 'requests[0].insert_inline_image.object_size': Cannot find field."\n },\n {\n "field": "requests[1].insert_inline_image.object_size",\n "description": "Invalid JSON payload received. Unknown name \"offsetX\" at 'requests[1].insert_inline_image.object_size': Cannot find field."\n },\n {\n "field": "requests[1].insert_inline_image.object_size",\n "description": "Invalid JSON payload received. Unknown name \"offsetY\" at 'requests[1].insert_inline_image.object_size': Cannot find field."\n }\n ]\n }\n ]\n }\n}\n" how to fix it

2
  • About but Can not Set the Positions, where do you want to put the image into the document body? And, it seems that your tag related to the language is google-apps-script. You want to achieve your goal using Google Apps Script? Commented May 9, 2024 at 11:56
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a minimal reproducible example. Commented May 12, 2024 at 7:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.