php - 致命错误:未捕获的 Google_Service_Exception Google Sheets API PHP 错误添加行
问题描述
收到以下错误:
Fatal error: Uncaught Google_Service_Exception: { "error": { "code": 400, "message": "Invalid JSON payload received. Unknown name \"0\" at 'data.values[1]': Cannot find field.
奇怪的是,只有当我添加的数组从第 7 个索引开始时才会发生。请参阅下面的完整数组。
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)
这是我用来添加行的函数:
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);
}
我再说一遍,如果我只有数组中的第六个索引,它会很好地工作。
________编辑____________
收集数据以插入 Google 表格表单 wordpress 帖子
$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);
解决方案
它反对一些传入的数据,但错误消息并不比这更清楚。这Unknown name \"0\" at 'data.values[0]': Cannot find field.
是一个误导。问题不一定是 [0] 值。我的问题是第 3 行 [7] 中的 NULL 值。如果您在 $insert_data 中传递多行,则其中任何一个都可能存在问题。这将是相同的错误消息。
也许工作表期望该列中有一个数字?尝试不同的空白工作表?
推荐阅读
- sql-server - 您的应用程序错误中发生了未处理的异常
- android - 切换到 PreferenceFragmentCompat 后没有分隔符
- html - Bootstrap 4页脚到底部高度
- tsql - 使用 AND 的 SQL Where 子句
- java - 如何从 IntelliJ 插件的操作事件创建文件?
- c++11 - 从 .txt 文件中读取并循环直到换行
- spring - Spring Boot 2 OAuth2 资源服务器不访问授权服务器进行访问令牌验证
- html - 如何在 CSS 和 HTML 上排列文本的顺序
- android - 安装两个版本的 apk(具有不同的 applicationId)
- javascript - 如何检测 html 中的非官方标签?