doulang6013 2016-06-30 12:36
浏览 53

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

I am using a PHP Google sheets client:

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

This client implements v3 of the API:

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

I usually get rows like this:

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


foreach ($listFeed->getEntries() as $entry)
{
    $rows []= $entry->getValues();
}

Google turns "Admin Title" into the array key "admintitle". I would like to get a map of the Google array keys to the original Titles. For example, I want an array with entries like:

[admintitle] => "Admin Title"

etc.

How do I do that?

  • 写回答

1条回答 默认 最新

  • draj840143 2016-06-30 22:20
    关注

    I don't think there is anything in the API to do this, so I made my own function. Note that we are using the API client 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;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能