通过Google Sheets API(PHP)插入大数组
我有一个构建大型阵列的程序。对于本例,假设它有一行。然后是78列。这是它的通过Google Sheets API(PHP)插入大数组,php,arrays,google-sheets-api,Php,Arrays,Google Sheets Api,我有一个构建大型阵列的程序。对于本例,假设它有一行。然后是78列。这是它的打印\r: Array ( [0] => Array ( [0] => BIZ ID [1] => B1 - A3 - Butt Seat Cushion [2] => BSC - Test 1 - Interests - Testing - Top 5, EU - WC [3] => Test 1 - Old age (24M) - Top 5, EU - [£19.54 / 1.593 B
打印\r
:
Array ( [0] => Array ( [0] => BIZ ID [1] => B1 - A3 - Butt Seat Cushion [2] => BSC - Test 1 - Interests - Testing - Top 5, EU - WC [3] => Test 1 - Old age (24M) - Top 5, EU - [£19.54 / 1.593 BEP] - WC – Copy [4] => Video 2 - Thumbnail 1 [5] => [6] => CONVERSIONS [7] => 2689 [8] => 1960 [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => 23.64 [20] => 157.59 [21] => 472.53 [22] => 92.87 [23] => 36.7255 [24] => 3.928511 [25] => 8.791372 [26] => 3.049461 [27] => 0.288293 [28] => 2.119747 [29] => 0.414737 [30] => 0.4925 [31] => 0.347647 [32] => 3.94 [33] => 2.149091 [34] => 7.88 [35] => 923 [36] => 446 [37] => 225 [38] => 117 [39] => 87 [40] => 78 [41] => 5 [42] => [43] => 48 [44] => 68 [45] => 6 [46] => 11 [47] => [48] => ABOVE_AVERAGE [49] => BELOW_AVERAGE_20 [50] => AVERAGE [51] => [52] => 1.371939 [53] => 3.214286 [54] => 0.375238 [55] => 2.44898 [56] => 48 [57] => 0.591 [58] => 0.606154 [59] => 4.728 [60] => 4.728 [61] => 7.88 [62] => 40 [63] => 39 [64] => 5 [65] => 5 [66] => 3 [67] => 993 [68] => 993 [69] => 6 [70] => [71] => 4 [72] => NAME [73] => 307978786963089 [74] => 23845484873330137 [75] => 23845818516690137 [76] => 23845818516750137 [77] => 2020-10-16 [78] => 2020-11-14 ) )
我想把它插入谷歌的工作表。我已经能够用一个简单的数组来实现这一点。但是,我在使用这个大型阵列时遇到了问题。我已使此代码正常工作:
// Initilize client
$this->client = new \Google_Client();
$this->client->setApplicationName('My PHP App');
$this->client->setScopes([\Google_Service_Sheets::SPREADSHEETS]);
$this->client->setAccessType('offline');
$this->client->setAuthConfig("$authJSONFile");
// Initilize sheet
$this->sheets = new \Google_Service_Sheets($this->client);
// Sheet ID
$this->sheetID = $sheetID;
$values = [["1", "2"],["3", "4"]];
$body = new Google_Service_Sheets_ValueRange([
'values' => $values
]);
$params = [
'valueInputOption' => 'RAW'
];
$update_sheet = $this->sheets->spreadsheets_values->update($this->sheetID, ""Sheet1!A1:B2"", $body, $params);
它做了预期的事情。但是,当我将$values
更改为我的大数组时,会出现以下错误:
Fatal error: Uncaught Google\Service\Exception: { "error": { "code": 400, "message": "Invalid JSON payload received. Unknown name \"0\" at 'data.values[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"1\" at 'data.values[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"2\" at 'data.values[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"3\" at 'data.values[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"4\" at 'data.values[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"5\" at 'data.values[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"6\" at 'data.values[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"7\" at 'data.values[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"8\" at 'data.values[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"9\" at 'data.values[0]': Cannot find field.\nInvalid JSON payload received. in /home/zenkohealth/public_html/tools/FBAPIExtractor/_/composerPackages/vendor/google/apiclient/src/Http/REST.php on line 128
对于大型阵列,我确实确保更改所选范围。我增加了78列。然后我将范围更改为:Sheet1!A1:BZ1
然后我想将新数据添加到我的大数组中。然后把它导入我的谷歌工作表。一切正常。我想添加如下数据:
$largeArray[2] = ["Data","Data","Data","Data","Data","Data","Data","Data","Data"]
$largeArray[3] = ["Data","Data","Data","Data","Data","Data","Data","Data","Data"]
$largeArray[4] = ["Data","Data","Data","Data","Data","Data","Data","Data","Data"]
但是,首先,我连一排都不能工作。我想我错过了什么或是有一个盲点。我将感谢任何帮助
更新 仔细查看错误消息后。我知道这是一个JSON解析问题。因此,我认为展示如何制作阵列很重要
$FBDataRow = [];
// Loop through data
for($x = 0; $x < count($response["data"]); $x++) {
$FBDataRow[$x] = [
"$businessManagerName", // Start from 0
$response["data"][$x]["account_name"],
$response["data"][$x]["campaign_name"],
$response["data"][$x]["adset_name"],
$response["data"][$x]["ad_name"],
"", // Delivery status
$response["data"][$x]["objective"],
$response["data"][$x]["impressions"],
$response["data"][$x]["reach"],
"", // Budget
"", // Bid type
$response["data"][$x]["age"],
$response["data"][$x]["gender"],
$response["data"][$x]["country"],
$response["data"][$x]["region"],
$response["data"][$x]["publisher_platform"],
$response["data"][$x]["platform_position"],
$response["data"][$x]["impression_device"],
$response["data"][$x]["hourly_stats_aggregated_by_advertiser_time_zone"],
$response["data"][$x]["spend"],
"", // 20 - action_values.add_to_cart
"", // 21 action_values.initiate_checkout
"", // 22 - action_values.purchase
"", // 23 - estimated_profit
$response["data"][$x]["website_purchase_roas"][0]["value"],
$response["data"][$x]["cpm"],
$response["data"][$x]["ctr"],
$response["data"][$x]["cpc"],
$response["data"][$x]["website_ctr"][0]["value"],
$response["data"][$x]["cost_per_inline_link_click"],
"", // 30 - cost_per_action_type.landing_page_view
"", // 31 - cost_per_action_type.view_content:
"", // 32 - cost_per_action_type.add_to_cart
"", // 33 - cost_per_action_type.initiate_checkout
"", // 34 - cost_per_action_type.purchase
"", // 35 - actions.video_view
$response["data"][$x]["video_p25_watched_actions"][0]["value"],
$response["data"][$x]["video_p50_watched_actions"][0]["value"],
$response["data"][$x]["video_p75_watched_actions"][0]["value"],
$response["data"][$x]["video_p95_watched_actions"][0]["value"],
$response["data"][$x]["video_p100_watched_actions"][0]["value"],
$response["data"][$x]["video_avg_time_watched_actions"][0]["value"],
"", // 42 - actions.link_clicks
"", // 43 - actions.landing_page_view
"", // 44 - actions.view_content
"", // 45 - actions.add_to_cart
"", // 46 - actions.initiate_checkout
"", // 47 - actions.purchase
$response["data"][$x]["conversion_rate_ranking"],
$response["data"][$x]["engagement_rate_ranking"],
$response["data"][$x]["quality_ranking"],
$response["data"][$x]["qualifying_question_qualify_answer_rate"],
$response["data"][$x]["frequency"],
$response["data"][$x]["unique_ctr"],
$response["data"][$x]["cost_per_unique_click"],
$response["data"][$x]["unique_link_clicks_ctr"],
"", // 56 - unique_actions.link_click
"", // 57 - cost_per_unique_action_type.landing_page_view
"", // 58 - cost_per_unique_action_type.view_content:
"", // 59 - cost_per_unique_action_type.add_to_cart
"", // 60 - cost_per_unique_action_type.checkout
"", // 61 - cost_per_unique_action_type.purchase
"", // 62 - unique_actions.landing_page_view
"", // 63 - unique_actions.view_content
"", // 64 - unique_actions.add_to_cart
"", // 65 - unique_actions.checkout
"", // 66 - unique_actions.purchase
"", // 67 - actions.post_engagement
"", // 68 - actions.page_engagement
"", // 69 - actions.post_reaction
"", // 70 - actions.post_comment
"", // 71 - actions.onsite_conversion.post_save
"$businessManagerID",
$response["data"][$x]["account_id"],
$response["data"][$x]["campaign_id"],
$response["data"][$x]["adset_id"],
$response["data"][$x]["ad_id"],
$response["data"][$x]["date_start"],
$response["data"][$x]["date_stop"]
];
$FBDataRow=[];
//循环浏览数据
对于($x=0;$x
我已经解决了!:)
我决定用JSON编码我在GoogleSheets中使用的数组
我发现许多值被标记为null。这在典型的打印中看不到
0 [79]
0 : BIZ ID
1 : B1 - A3 - Butt Seat Cushion
2 : BSC - Test 1 - Interests - Testing - Top 5, EU - WC
3 : Test 1 - Old age (24M) - Top 5, EU - [£19.54 / 1.593 BEP] - WC – Copy
4 : Video 2 - Thumbnail 1
5 :
6 : CONVERSIONS
7 : 2689
8 : 1960
9 :
10 :
11 : null
12 : null
13 : null
14 : null
15 : null
16 : null
17 : null
18 : null
19 : 23.64
20 : 157.59
21 : 472.53
22 : 92.87
23 : 36.7255
24 : 3.928511
25 : 8.791372
26 : 3.049461
27 : 0.288293
28 : 2.119747
29 : 0.414737
30 : 0.4925
31 : 0.347647
32 : 3.94
33 : 2.149091
34 : 7.88
35 : 923
36 : 446
37 : 225
38 : 117
39 : 87
40 : 78
41 : 5
42 :
43 : 48
44 : 68
45 : 6
46 : 11
47 :
48 : ABOVE_AVERAGE
49 : BELOW_AVERAGE_20
50 : AVERAGE
51 : null
52 : 1.371939
53 : 3.214286
54 : 0.375238
55 : 2.44898
56 : 48
57 : 0.591
58 : 0.606154
59 : 4.728
60 : 4.728
61 : 7.88
62 : 40
63 : 39
64 : 5
65 : 5
66 : 3
67 : 993
68 : 993
69 : 6
70 :
71 : 4
72 : NAME
73 : 307978786963089
74 : 23845484873330137
75 : 23845818516690137
76 : 23845818516750137
77 : 2020-10-16
78 : 2020-11-14
因此,我决定在创建数组后运行一个循环,将所有null
替换为“”
这似乎起到了作用。数据导入非常完美:)我已经解决了它!:)
我决定用JSON编码我在GoogleSheets中使用的数组
我发现许多值被标记为null。这在典型的打印中看不到
0 [79]
0 : BIZ ID
1 : B1 - A3 - Butt Seat Cushion
2 : BSC - Test 1 - Interests - Testing - Top 5, EU - WC
3 : Test 1 - Old age (24M) - Top 5, EU - [£19.54 / 1.593 BEP] - WC – Copy
4 : Video 2 - Thumbnail 1
5 :
6 : CONVERSIONS
7 : 2689
8 : 1960
9 :
10 :
11 : null
12 : null
13 : null
14 : null
15 : null
16 : null
17 : null
18 : null
19 : 23.64
20 : 157.59
21 : 472.53
22 : 92.87
23 : 36.7255
24 : 3.928511
25 : 8.791372
26 : 3.049461
27 : 0.288293
28 : 2.119747
29 : 0.414737
30 : 0.4925
31 : 0.347647
32 : 3.94
33 : 2.149091
34 : 7.88
35 : 923
36 : 446
37 : 225
38 : 117
39 : 87
40 : 78
41 : 5
42 :
43 : 48
44 : 68
45 : 6
46 : 11
47 :
48 : ABOVE_AVERAGE
49 : BELOW_AVERAGE_20
50 : AVERAGE
51 : null
52 : 1.371939
53 : 3.214286
54 : 0.375238
55 : 2.44898
56 : 48
57 : 0.591
58 : 0.606154
59 : 4.728
60 : 4.728
61 : 7.88
62 : 40
63 : 39
64 : 5
65 : 5
66 : 3
67 : 993
68 : 993
69 : 6
70 :
71 : 4
72 : NAME
73 : 307978786963089
74 : 23845484873330137
75 : 23845818516690137
76 : 23845818516750137
77 : 2020-10-16
78 : 2020-11-14
因此,我决定在创建数组后运行一个循环,将所有null
替换为“”
这似乎起到了作用。数据导入非常完美:)我没有使用过,但您是否尝试过在“Sheet1!A1”
范围内使用单个单元格(根据文档)应该自动获取给定的数据。您好,谢谢您的评论:)我刚刚在那里尝试过。仍然会得到相同的错误。错误是“接收到无效的JSON负载”。因此我认为这与我提供的范围无关。在