php 通过php解析Google Drive电子表格数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 通过php解析Google Drive电子表格数据相关的知识,希望对你有一定的参考价值。

<?php
/**
 * @desc Parse Google Drive spreadsheet data via php
 * @author Misha M.-Kupriyanov https://plus.google.com/104512463398531242371/
 * @link https://gist.github.com/3898429
 */
 
//Spreadsheet https://docs.google.com/spreadsheet/pub?key=0Akgh73WhU1qHdFg4UmRhaThfUFNBaFR3N3BMVW9uZmc&output=html

//JSON Representation
$url = 'https://spreadsheets.google.com/feeds/list/0Akgh73WhU1qHdFg4UmRhaThfUFNBaFR3N3BMVW9uZmc/od6/public/basic?prettyprint=true&alt=json';
$formatArr = array('column1', 'column2', 'column3');

$rows = GData::getSpreadsheetData($url, $formatArr);

print_r($rows);


class GData {


   public static function getSpreadsheetData($url, $rowFormatArr) {


      $content = file_get_contents($url);

      $contentArr = json_decode($content, true);

      $rows = array();
      foreach($contentArr['feed']['entry'] as $row) {
         if ($row['title']['$t'] == '-') {
            continue;
         }

         $rowItems = array();

         foreach($rowFormatArr as $item) {
            $rowItems[$item] = self::getRowValue($row['content']['$t'], $rowFormatArr, $item);
         }

         $rows[] = $rowItems;
      }

      return $rows;

   }

   static function getRowValue($row, $rowFormatArr, $column_name) {
      echo "getRowValue[$column_name]:$row";

      if (empty($column_name)) {
         throw new Exception('column_name must not empty');
      }

      $begin = strpos($row, $column_name);

      echo "begin:$begin";
       
      if ($begin == -1) {
         return '';
      }

      $begin = $begin + strlen($column_name) + 1;

       
      $end = -1;
      $found_begin = false;

      foreach($rowFormatArr as $entity) {
         echo "checking:$entity";

         if ($found_begin && strpos($row, $entity) != -1) {
            $end = strpos($row, $entity) - 2;
            echo "end1:$end";
            break;
         }

         if ($entity == $column_name) {
            $found_begin = true;
         }


         #check if last element
         if (substr($row, strlen($row) - 1) == $column_name) {
            $end = strlen($row);
         } else {
            if ($end == -1) {
               $end = strlen($row);
            } else {
               $end = $end + 2;
            }
         }
      }

      echo "end:$end";

      echo "$column_name:$row";
      $value = substr($row, $begin, $end - $begin);
      $value = trim($value);

      echo "${column_name}[${begin}-${end}]:[$value]";


      return $value;
}


}

以上是关于php 通过php解析Google Drive电子表格数据的主要内容,如果未能解决你的问题,请参考以下文章

用PHP列出Google电子表格答案(解析JSON)

未经PHP身份验证,Google Drive API不允许上传文件

Google Drive Api PHP 分段上传空内容

文件移动 Google Drive API v3 PHP

使用PHP无需用户交互即可连接到Google Drive API

403:在google drive api php中创建文件夹时权限不足