PHP Google Sheet API - 获取列标题(电子表格中的第1行)

时间:2016-06-30 12:36:14

标签: php google-sheets google-api-php-client google-sheets-api

我正在使用PHP Google表格客户端:

https://github.com/asimlqt/php-google-spreadsheet-client

此客户端实现API的v3:

https://developers.google.com/google-apps/spreadsheets/data

我通常会得到这样的行:

// simplified code:
$worksheet = $worksheetFeed->getByTitle($worksheet_id);
$listFeed = $worksheet->getListFeed();


foreach ($listFeed->getEntries() as $entry)
{
    $rows []= $entry->getValues();
}
谷歌转向"管理员标题"进入数组键" admintitle"。我想获得原始标题的Google数组键的地图。例如,我想要一个包含以下条目的数组:

  

[admintitle] => "管理员标题"

我该怎么做?

1 个答案:

答案 0 :(得分:0)

我认为API中没有任何东西可以做到这一点,所以我自己创造了自己的功能。请注意,我们使用的是API客户端v2.3

function get_worksheet_column_titles($worksheet)
{
    $map = array();
    $already_in_map = array();

    $row_one_query = array(
        'min-row' => 1,
        'max-row' => 1
        );

    $cellfeed = $worksheet->getCellFeed($row_one_query);
    $entries = $cellfeed->getEntries();

    foreach ($entries as $cell)
    {
        $title = trim($cell->getContent());


        if ($title && $title[0] != '_')
        {
            $array_key = strtolower(preg_replace('/[^A-Z0-9_-]/i', '', $title));

            if ($already_in_map[$array_key])
            {
                // if identical column titles, google adds count to end

                $seq = 2;

                while ($already_in_map[$array_key.'_'.$seq])
                {
                    $seq++;
                }


                $array_key .= "_{$seq}";
            }

            // mark this key as used as used.
            $already_in_map[$array_key] = true;

            $map[$array_key] = $title;
        }
        else
        {
            continue;
        }
    }
    return $map;
}