Reputation: 651
Getting the following error:
Fatal error: Uncaught Google_Service_Exception: { "error": { "code": 400, "message": "Invalid JSON payload received. Unknown name \"0\" at 'data.values[1]': Cannot find field.
Weirdly it only happens when the array I am adding has from the 7th index on. See full array below.
array (size=11)
0 => int 2176
1 => string '16 Bedroom Residence With Unique Design' (length=39)
2 => string 'This residence has a proven solid rental income... (length=2224)
3 => string '2095000' (length=7)
4 => string '3000' (length=4)
5 => string '16' (length=2)
6 => string '16.5' (length=4)
7 => string 'Tamarindo' (length=9)
8 => string 'For Sale' (length=8)
9 => string 'House' (length=5)
10 => string 'Air Conditioning, BBQ Area, Close to Schools, Close to Shops, Fully Equipped, Fully Furnished, Internet / Wifi, Laundry Room, Parking, Private Garden, Private Pool, ' (length=165)
Here is the function I call to add the rows:
function insert_google_sheet($data)
{
require 'vendor/autoload.php';
$client = new \Google_Client();
$client->setApplicationName('Properties Google Sheet');
$client->setScopes([\Google_Service_Sheets::SPREADSHEETS]);
$client->setAccessType('offline');
$client->setAuthConfig(__DIR__ . '/credentials.json');
$service = new Google_Service_Sheets($client);
$spreadsheetID = '1_3o2B7kucFBNWLJ99CWKCJsePqfZgB2fi8U7AatFoow';
$range = 'Properties';
$body = new Google_Service_Sheets_ValueRange([
'values' => $data
]);
$params = [
'valueInputOption' => 'RAW'
];
$insert = [
'insertDataOption' => 'INSERT_ROWS'
];
$service->spreadsheets_values->append($spreadsheetID, $range, $body, $params, $insert);
}
I repeat, it works well if I only have up to the sixth index in the array.
________EDIT____________
Gathering data to insert in Google Sheets form wordpress post
$insert_data = [];
// Get Post Info
$properties = get_posts(array(
'numberposts' => -1,
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'property',
));
foreach ($properties as $property) {
// Get Post Meta
$id = $property->ID;
$title = $property->post_title;
$description = $property->post_content;
$property_meta = get_post_meta($id);
$price = $property_meta['_meta_price'][0];
$area = $property_meta['_meta_area'][0];
$beds = $property_meta['_meta_bedroom'][0];
$baths = $property_meta['_meta_bathroom'][0];
// $featured = $property_meta['_meta_featured'][0];
$property_location = get_the_terms($id, 'location');
$property_location = $property_location[0]->name;
$property_status = get_the_terms($id, 'status');
$property_status = $property_status[0]->name;
$property_type = get_the_terms($id, 'type');
$property_type = $property_type[0]->name;
$property_features = get_the_terms($id, 'features');
if ($property_features) {
$features = '';
foreach ($property_features as $feat) {
$features .= $feat->name . ', ';
}
}
$data_array = array(
$id,
$title,
$description,
$price,
$area,
$beds,
$baths,
$property_location,
$property_status,
$property_type,
$features
);
array_push($insert_data, $data_array);
}
insert_google_sheet($insert_data);
Upvotes: 0
Views: 1240
Reputation: 136
It is objecting to some of the incoming data, but the error message isn't any clearer than that. The Unknown name \"0\" at 'data.values[0]': Cannot find field.
is a misdirection. It's not necessarily the [0] value that is a problem. My problem was a NULL value in [7] in row 3. If you are passing multiple rows in $insert_data, a problem in any of them might be the issue. It would be the same error message.
Perhaps the sheet expects a number in that column? Try a different, blank worksheet?
Upvotes: 2