在 google 工作表 api 到 PHP 中添加数组形式数据
Add array form data in google sheets api through PHP
我在 PHP 中使用 google sheets API 将我的 html 表单数据添加到 google spreadsheet.
所有其他数据都已附加到 sheet 的相应单元格中,因为每个值都有单个数据,除了一个数据在数组中,因为 html 输入数据在多个复选框中。
我的googlesheet表格:
我的代码在 php 中:
<?php
//add data to google spreadsheet
require __DIR__ . '/vendor/autoload.php';
//getting form data
$name_kanji = $_POST['name_kanji'];
$name_katakana = $_POST['name_katakana'];
$age = $_POST['age'];
$phone = $_POST['phone'];
$userEmail = $_POST['email'];
$zip_code = $_POST['zip_code'];
$city = $_POST['city'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$experience = $_POST['experience'];
$others_text = $_POST['others_text'];
$lessons = $_POST['check_list'];
$source = $_POST['source'];
//Reading data from spreadsheet.
$client = new \Google_Client();
$client->setApplicationName('Google Sheets and PHP');
$client->setScopes([\Google_Service_Sheets::SPREADSHEETS]);
$client->setAccessType('offline');
$client->setAuthConfig(__DIR__ . '/credentials.json');
$service = new Google_Service_Sheets($client);
$spreadsheetId = "14B0iB8-uLFNkb_d0qY183wNXeW5reAW5QBjOf69VXeU"; //It is present in the spreadsheet URL
//Adding the data in your google sheet
$update_range = 'data';
$values = [[ $name_kanji, $name_katakana, $age, $phone, $userEmail, $zip_code.', '.$city.', '.$address1.', '.$address2, $experience, $others_text, $lessons, $source]];
$body = new Google_Service_Sheets_ValueRange([
'values' => $values
]);
$params = [
'valueInputOption' => 'RAW'
];
$insert = [
'insertDataOption' => 'INSERT_ROWS'
];
$update_sheet = $service->spreadsheets_values->append($spreadsheetId, $update_range, $body, $params);
我的问题出在下面的代码中
$values = [[ $name_kanji, $name_katakana, $age, $phone, $userEmail, $zip_code.', '.$city.', '.$address1.', '.$address2, $experience, $others_text, $lessons, $source]];
此处 $lessons 在数组中,其数据应由逗号填充或在分布的 I 列中的列表中填充sheet。
我能得到关于如何传递数组数据的解决方案吗?
要将您的数组转换为以逗号分隔的字符串,请使用例如implode method
样本:
$list = implode(', ', $lessons);
$values = [[ $name_kanji, $name_katakana, $age, $phone, $userEmail, $zip_code.', '.$city.', '.$address1.', '.$address2, $experience, $others_text, $list, $source]];
我在 PHP 中使用 google sheets API 将我的 html 表单数据添加到 google spreadsheet. 所有其他数据都已附加到 sheet 的相应单元格中,因为每个值都有单个数据,除了一个数据在数组中,因为 html 输入数据在多个复选框中。
我的googlesheet表格:
我的代码在 php 中:
<?php
//add data to google spreadsheet
require __DIR__ . '/vendor/autoload.php';
//getting form data
$name_kanji = $_POST['name_kanji'];
$name_katakana = $_POST['name_katakana'];
$age = $_POST['age'];
$phone = $_POST['phone'];
$userEmail = $_POST['email'];
$zip_code = $_POST['zip_code'];
$city = $_POST['city'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$experience = $_POST['experience'];
$others_text = $_POST['others_text'];
$lessons = $_POST['check_list'];
$source = $_POST['source'];
//Reading data from spreadsheet.
$client = new \Google_Client();
$client->setApplicationName('Google Sheets and PHP');
$client->setScopes([\Google_Service_Sheets::SPREADSHEETS]);
$client->setAccessType('offline');
$client->setAuthConfig(__DIR__ . '/credentials.json');
$service = new Google_Service_Sheets($client);
$spreadsheetId = "14B0iB8-uLFNkb_d0qY183wNXeW5reAW5QBjOf69VXeU"; //It is present in the spreadsheet URL
//Adding the data in your google sheet
$update_range = 'data';
$values = [[ $name_kanji, $name_katakana, $age, $phone, $userEmail, $zip_code.', '.$city.', '.$address1.', '.$address2, $experience, $others_text, $lessons, $source]];
$body = new Google_Service_Sheets_ValueRange([
'values' => $values
]);
$params = [
'valueInputOption' => 'RAW'
];
$insert = [
'insertDataOption' => 'INSERT_ROWS'
];
$update_sheet = $service->spreadsheets_values->append($spreadsheetId, $update_range, $body, $params);
我的问题出在下面的代码中
$values = [[ $name_kanji, $name_katakana, $age, $phone, $userEmail, $zip_code.', '.$city.', '.$address1.', '.$address2, $experience, $others_text, $lessons, $source]];
此处 $lessons 在数组中,其数据应由逗号填充或在分布的 I 列中的列表中填充sheet。
我能得到关于如何传递数组数据的解决方案吗?
要将您的数组转换为以逗号分隔的字符串,请使用例如implode method
样本:
$list = implode(', ', $lessons);
$values = [[ $name_kanji, $name_katakana, $age, $phone, $userEmail, $zip_code.', '.$city.', '.$address1.', '.$address2, $experience, $others_text, $list, $source]];