首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >通过PHP在google sheets api中添加数组表单数据

通过PHP在google sheets api中添加数组表单数据
EN

Stack Overflow用户
提问于 2021-09-02 08:18:53
回答 1查看 588关注 0票数 1

我使用PHP中的google sheets API将我的html表单数据添加到google电子表格中。所有其它数据都被附加在工作表的相应单元格中,因为每个值具有单个数据,除了一个数据之外,该数据是在数组中,因为html输入数据在多个复选框中。

我的google表单:

和我用php编写的代码:

代码语言:javascript
运行
复制
<?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);

我的问题在于下面的代码

代码语言:javascript
运行
复制
$values = [[ $name_kanji, $name_katakana, $age, $phone, $userEmail, $zip_code.', '.$city.', '.$address1.', '.$address2, $experience, $others_text, $lessons, $source]];

这里,$lessons在数组中,其数据应该通过逗号或电子表格的i列的list填充。

我能得到如何传递数组数据的解决方案吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-02 10:18:08

要将数组转换为逗号分隔的字符串,请使用implode method

示例:

代码语言:javascript
运行
复制
$list = implode(', ', $lessons);
$values = [[ $name_kanji, $name_katakana, $age, $phone, $userEmail, $zip_code.', '.$city.', '.$address1.', '.$address2, $experience, $others_text, $list, $source]];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69026593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档