Add array form data in google sheets api through PHP

  api, google-sheets, google-sheets-api, php

I am using google sheets API in PHP to add my html form data in google spreadsheet.
All other data have been appended in the respective cell of the sheet since each value had single data except one data which is in array since the html input data is in multiple checkbox.

My google sheet form:

enter image description here

And my code in 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);

My problem is in following code

$values = [[ $name_kanji, $name_katakana, $age, $phone, $userEmail, $zip_code.', '.$city.', '.$address1.', '.$address2, $experience, $others_text, $lessons, $source]];

Here $lessons is in array whose data should be populated either by comma or in list in I column of the spreadsheet.

Can I get solution on how to pass the array data?

Source: Ask PHP

LEAVE A COMMENT