Write to Sheet with Google Sheets API - php

I want write data to my sheet:
$spreadsheetId = 'xxx';
$range = 'Test!A1:D5';
$values = [
'range' => "A1",
'majorDimension' => 'DIMENSION_UNSPECIFIED',
'values' => [
["Item", "Cost", "Stocked", "Ship Date"],
["Wheel", "$20.50", "4", "3/1/2016"],
["Door", "$15", "2", "3/15/2016"],
["Engine", "$100", "1", "30/20/2016"],
],
];
$body = new Google_Service_Sheets_ValueRange(array(
'values' => $values
));
$params = array(
'valueInputOption' => 'RAW'
);
$result = $service->spreadsheets_values->update($spreadsheetId, $range, $body, $params);
But in log i see:
PHP Fatal error: Uncaught exception 'Google_Service_Exception' with message '{
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"range\" at 'data.values': Cannot find field.\nInvalid JSON payload received. Unknown name \"major_dimension\" at 'data.values': Cannot find field.",
"errors": [
{
"message": "Invalid JSON payload received. Unknown name \"range\" at 'data.values': Cannot find field.\nInvalid JSON payload received. Unknown name \"major_dimension\" at 'data.values': Cannot find field.",
"domain": "global",
"reason": "badRequest"
}
],
"status": "INVALID_ARGUMENT"
}
}
What am I doing wrong? I will be grateful for the example code that works for you :)
In the examples there is not enough information( https://developers.google.com/sheets/api/guides/values#writing_to_a_single_range)

You're almost there. Don't forget to include the sheet name
"range": "Sheet1!A1:D5"
Check the Basic Writing guide for more samples.

Try this:
$spreadsheetId = 'xxx';
$range = 'Test!A1:D5';
$values = [
["Item", "Cost", "Stocked", "Ship Date"],
["Wheel", "$20.50", "4", "3/1/2016"],
["Door", "$15", "2", "3/15/2016"],
["Engine", "$100", "1", "30/20/2016"],
];
$updateBody = new Google_Service_Sheets_ValueRange([
'range' => $range,
'majorDimension' => 'ROWS',
'values' => $values,
]);
$valueInputOption = 'USER_ENTERED'; // Or RAW
$params = [
'valueInputOption' => $valueInputOption
];
$result = $this->sheetsService->spreadsheets_values->update(
$spreadsheetID,
$updateRange,
$updateBody,
$params
);

Not the case here, but I have solved a similar issue by removing NULL values from the items inside of the values array.
Google's PHP library seems to have a hard time parsing these values and ends up generating an invalid JSON payload.

Related

How we can insert a horizontal rule/line in google docs using google docs API using PHP?

I want to insert a horizontal rule in google docs using PHP. I have also followed and gone through this link https://developers.google.com/apps-script/reference/document/paragraph#inserthorizontalrulechildindex but I am unable to create a horizontal line.
I am trying to send the request with this next approach:
$requests = [
new Google_Service_Docs_Request([
'insertHorizontalRule' => [
'location' => [
'index' => 1,
],
]
]),
];
$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(
'requests' => $requests
));
$response = $service->documents->batchUpdate($documentId, $batchUpdateRequest);
Can anybody tell me what am I missing or what mistake I am making.
getting this error -
PHP Fatal error: Uncaught Google\Service\Exception: {
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"insertHorizontalRule\" at 'requests[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"fields\" at 'requests[0]': Cannot find field.",
"errors": [
{
"message": "Invalid JSON payload received. Unknown name \"insertHorizontalRule\" at 'requests[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"fields\" at 'requests[0]': Cannot find field.",
"reason": "invalid"
}
],
"status": "INVALID_ARGUMENT",

Selective batch update for a row on Google sheet spreadsheet v4 api

$range = "2020!A92:L92";
$valueRange= new Google_Service_Sheets_ValueRange();
$valueRange->setValues(["values" => ["updatetest1", "updatetest2", "updatetest3", "updatetest4", "updatetest5", "updatetest6", "updatetest7", "updatetest8", "updatetest9", "updatetest10", "updatetest11", "updatetest12"]]);
$conf = ["valueInputOption" => "RAW"];
$service->spreadsheets_values->update($spreadsheetId, $range, $valueRange, $conf);
I have this snippet, but the problem is tha I need to modify B92, F92 and G92 only, so I would need to update those with the values updatetest2, updatetest6 and updatetest7. I could call it three times, but there's probably a better way. I know there's \Google_Service_Sheets_BatchUpdateSpreadsheetRequest, but the documentation is pretty parse.
https://developers.google.com/resources/api-libraries/documentation/sheets/v4/php/latest/class-Google_Service_Sheets_BatchUpdateSpreadsheetRequest.html#$responseIncludeGridData
$data = [];
array_push(
$data,
new Google_Service_Sheets_ValueRange([
'range' => 'B92',
'values' => [["BATCHUPDATE"]]
])
);
array_push(
$data,
new Google_Service_Sheets_ValueRange([
'range' => 'E92',
'values' => [["BATCHUPDATE"]]
])
);
array_push(
$data,
new Google_Service_Sheets_ValueRange([
'range' => 'F92',
'values' => [["BATCHUPDATE"]]
])
);
$body = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
"valueInputOption" => "RAW",
"data" => $data
]);
$result = $service->spreadsheets->batchUpdate($spreadsheetId, $body);
I also tried the following and I am getting:
PHP Fatal error: Uncaught Google\Service\Exception: {
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"valueInputOption\"
: Cannot find field.\nInvalid JSON payload received. Unknown name \"data\": Cann
ot find field.",
"errors": [
{
"message": "Invalid JSON payload received. Unknown name \"valueInputOpti
on\": Cannot find field.\nInvalid JSON payload received. Unknown name \"data\":
Cannot find field.",
"reason": "invalid"
}
],
"status": "INVALID_ARGUMENT"
}
}
The error you are receiving is due to the fact that the spreadsheets.batchUpdate request does not expect a field named valueInputOption.
However, since you want to update the values from a sheet, you should use the
spreadsheets.values.batchUpdate request and make the following change:
$body = new Google_Service_Sheets_BatchUpdateValuesRequest([
"valueInputOption" => "RAW",
"data" => $data
]);
For further use, if needed, you can easily simulate the Sheets API requests by making use of the "Try this API!" from the documentation pages.
Reference
Class Google_Service_Sheets_BatchUpdateValuesRequest;
Sheets API spreadsheets.values.batchUpdate.

How to create course work for a Google Classroom project using API in php

I want to create a course work(Assignment) as a course teacher using Google Classroom API to itself for a project in PHP. To create an assignment which correct code I wrote? Give me some suggestions with PHP code for creating an assignment.
I've added some code to the quickstart.php file.
Code:
$client = getClient();
$courseId = '394192735087';
$service = new Google_Service_Classroom($client);
$post_body = new Google_Service_Classroom_CourseWork(array(
'workType' => 'ASSIGNMENT',
'title' => 'Quiz-5',
'description' => 'where you add up by the number of numbers',
'state' => 'PUBLISHED',
'maxPoints' => 100,
'associatedWithDeveloper' => true,
'assigneeMode' => 'ALL_STUDENTS',
'submissionModificationMode' => 'SUBMISSION_MODIFICATION_MODE_UNSPECIFIED'
));
$service->courses_courseWork->create($courseId, $post_body);
But when I run this code in quickstart.php at localhost the following problems can be seen.
Fatal error: Uncaught Google\Service\Exception: {
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"name\" at 'course_
work': Cannot find field.",
"errors": [
{
"message": "Invalid JSON payload received. Unknown name \"name\" at 'cou
rse_work': Cannot find field.",
"reason": "invalid"
}
],
"status": "INVALID_ARGUMENT"
}
}
in C:\xampp\htdocs\api\vendor\google\apiclient\src\Http\REST.php:128
How can I solved it ?

How to update multiple cells in google sheets via Sheets Api?

I am using the following code in a PHP server to update values in cells A4 & A7 of a Google Sheet:
$values = array(
array('1234'),
array('abcd')
);
$range = array(array('Sheet1!A4'),array('Sheet1!A7'));
$data = [];
$data[] = new Google_Service_Sheets_ValueRange([
'range' => $range,
'values' => $values
]);
// Additional ranges to update ...
$body = new Google_Service_Sheets_BatchUpdateValuesRequest([
'valueInputOption' => $valueInputOption,
'data' => $data
]);
$result = $service->spreadsheets_values->batchUpdate($spreadsheetId, $body);
On running this request, the following error is received:
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"range\" at 'data[0]': Proto field is not repeating, cannot start list.",
"errors": [
{
"message": "Invalid JSON payload received. Unknown name \"range\" at 'data[0]': Proto field is not repeating, cannot start list.",
"reason": "invalid"
}
],
"status": "INVALID_ARGUMENT"
}
}
What is the correct way to make a Google Sheets API request to update multiple discontinuous cells at once in PHP?
Solved the issue by posting the data in the following format:
$data = [
[
'range'=> 'Sheet1!A4',
'values'=> array(
array('1234')
)
],[
'range'=> 'Sheet1!A7',
'values'=> array(
array('abcd')
)
]
];
// Additional ranges to update ...
$body = new Google_Service_Sheets_BatchUpdateValuesRequest([
'valueInputOption' => 'USER_ENTERED',
'data' => $data
]);
$result = $service->spreadsheets_values->batchUpdate($spreadsheetId, $body);

Google Sheets API (""Invalid JSON payload received. Unknown name \"\": Root element must be a message."")

Trying to add data to a Google Sheet via the Google Sheets API and the BatchUpdate method, but I get this error (don't know what it refers to)
> PHP Fatal error: Uncaught Google\Service\Exception: { "error": {
> "code": 400,
> "message": "Invalid JSON payload received. Unknown name \"\": Root element must be a message.",
> "errors": [
> {
> "message": "Invalid JSON payload received. Unknown name \"\": Root element must be a message.",
> "reason": "invalid"
> }
> ],
> "status": "INVALID_ARGUMENT" } }
Here's my new code:
$spreadsheetId = 'myspreadsheetid';
$client = new Google_Client();
$client->setAuthConfig('mycredntials.json');
$client->setApplicationName('Sheet Automation');
$client->setScopes(Google_Service_Sheets::SPREADSHEETS);
$service = new Google_Service_Sheets($client);
$range = 'A2:L';
$valueInputOption = 'USER_ENTERED';
$body = new Google_Service_Sheets_ValueRange([
'values' => $row1
]);
$params = [
'valueInputOption' => $valueInputOption
];
$result = $service->spreadsheets_values->append($spreadsheetId, $range, $body, $params);
EDIT:
The data I get from the database is put into an array ($row1) that when echoed looks like this:
(
[cdr_id] => myid
[calldate] => 2021-05-27
[src] => mysource
[accountcode] => myaccountcode
[userfield] => disposition
[ivr_std_txn_full] =>
)
I then grab that info and use implode to put it all in one line (this may be the issue)
echo $line1 = implode(',', $row1)
I tried setting the values to be appended to both $row1 and $line1, but still get the payload issue.
in my condition, its because some data have null value, so make sure data you want send not null, you can change it to empty string or other.
example :
[ivr_std_txn_full] => null
change to :
[ivr_std_txn_full] => ''
The solution was pretty simple once I finally figured it out. If you won't be putting the values in manually and instead have an array filled with data you just need to reference each key in the array instead of just the array.
The request body ended up looking like this:
$body = new Google_Service_Sheets_ValueRange([
//'values' => $row1
'values' =>
[
[
$row1['cdr_id'], $row1['calldate'], $row1['src'], $row1['accountcode'], $row1['userfield'], $row1['ivr_std_txn_full']
]
]
]);
$params = [
'valueInputOption' => $valueInputOption
];
$result = $service->spreadsheets_values->append($spreadsheetId, $range, $body, $params);

Categories