【问题标题】:Spreadsheets error " String could not be parsed as XML"电子表格错误“无法将字符串解析为 XML”
【发布时间】:2021-11-24 12:33:02
【问题描述】:

当我尝试从 Google 云端硬盘获取电子表格的所有标题时,我收到了这条消息 The string could not be parsed as XML

ERROR ->  SimpleXMLElement::__construct(): Entity: line 1: parser error : Start tag expected, '<' not found in C:\....

ERROR-> SimpleXMLElement::__construct(): { in C:\....

ERROR-> SimpleXMLElement::__construct(): ^ in C:\...

我尝试在谷歌驱动器上连接,我成功了,但我需要在谷歌驱动器上获取电子表格的名称,我以前使用 API v3 现在我需要使用 V4。

这是调用方法getSpreadsheets()的代码;

$serviceRequest = new DefaultServiceRequest($token->access_token, $token->token_type);
                        ServiceRequestFactory::setInstance($serviceRequest);

$spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
$spreadsheetFeed = $spreadsheetService->getSpreadsheets();

这是getSpreadsheets方法的代码:

public function getSpreadsheets()
    {
        return new SpreadsheetFeed(
             

           ServiceRequestFactory::getInstance()->get('v4/spreadsheets/1u7WYzJOYMX7uH3AIM70yVLOaHBy8p_uifuJe_Saa2T4?fields=sheets.properties.title')
        
        
        );
    }


这是 SpreadsheetFeed 类,错误原因在哪里:

namespace Google\Spreadsheet;

use ArrayIterator;
use SimpleXMLElement;

/**
 * Spreadsheet feed. 
 *
 * @package    Google
 * @subpackage Spreadsheet
 * @author     Asim Liaquat <asimlqt22@gmail.com>
 */
class SpreadsheetFeed extends ArrayIterator
{
    /**
     * The spreadsheet feed xml object
     * 
     * @var \SimpleXMLElement
     */
    protected $xml;

    /**
     * Initializes the the spreadsheet feed object
     * 
     * @param string $xml the raw xml string of a spreadsheet feed
     */
    public function __construct($xml)
    {
        $this->xml = new SimpleXMLElement($xml);

        $spreadsheets = array();
        foreach ($this->xml->entry as $entry) {
            $spreadsheets[] = new Spreadsheet($entry);
        }
        parent::__construct($spreadsheets);
    }

    /**
     * Gets a spreadhseet from the feed by its title. i.e. the name of 
     * the spreadsheet in google drive. This method will return only the
     * first spreadsheet found with the specified title.
     * 
     * @param string $title
     * 
     * @return \Google\Spreadsheet\Spreadsheet|null
     */
    public function getByTitle($title)
    {
        foreach($this->xml->entry as $entry) {
            if($entry->title->__toString() == $title) {
                return new Spreadsheet($entry);
            }
        }
        return null;
    }

}

我试图找到一个解决方案如何传递这个错误! 任何帮助,如何解决这个错误? 谢谢!

【问题讨论】:

  • 你能指定DefaultServiceRequest类的来源吗?如果您正在使用这个library,似乎是not adapted 与Sheets API v4 一起使用。作为建议,您应该使用 Google 提供的 libraries 重写您的代码。
  • 您好埃梅尔,感谢您的反馈!是的,我正在使用那个库,你有一个如何做到这一点的例子吗?你有适应 API v4 的库吗?许多tnx
  • 嗨,Emel,我正在研究适应库,我做了一些更改,但现在我遇到了上面评论中描述的错误! DefaultServiceRequest 类没问题,我没有从他们那里得到错误,我调整了他们!
  • 如果您尝试将库移植到 Sheets API v4,最好的去处是库 repository。如果您尝试执行其他操作,例如获取所有电子表格标题,您可以使用此answer 作为指导。
  • 正如我在另一条评论中指出的那样,最好的办法是使用Google 提供的库重写您的代码。

标签: google-sheets-api


【解决方案1】:

这是我的应用程序的解决方案,从 v3 迁移到 v4 电子表格 API:

$this->service_drive = new Google_Service_Drive($this->client);
                $optParams = array('q'=> 'mimeType="application/vnd.google-apps.spreadsheet"');
                $this->files = $this->service_drive->files->listFiles($optParams);
            }
                 // $this->service_drive = new Google_Service_Drive($this->client);

                  //$optParams = array('q'=> 'mimeType="application/vnd.google-apps.spreadsheet"');
                  //$this->files = $this->service_drive->files->listFiles($optParams);
                  //$gdata_spreadsheets=$this->files;//my test
                  if($this->files !==null){
                   if (count($this->files->getFiles()) == 0) {
                  $gdata_spreadsheets=array("No Spreadsheets");
                  } else {
                  foreach ($this->files->getFiles() as $file) {
                   $gdata_spreadsheets[$file->getId()]=$file->getName();
                  } 
                } 
        }
            //trenuto ispod code rjesavam
            if($gdata->spreadsheet_id != '' && isset( $gdata_spreadsheets[$gdata->spreadsheet_id] ) && $this->files !== null){

            $this->service = new Google_Service_Sheets($this->client);
            $this->response = $this->service->spreadsheets->get($gdata->spreadsheet_id);
            $this->sheets=$this->response->getSheets();
             
            // $spreadsheet = $this->service_drive($gdata_spreadsheets[$gdata->spreadsheet_id]);
              // $worksheetFeed = $spreadsheet->getWorksheets();
                
                foreach ( $this->sheets as $sheet ){
                   $gdata_worksheets[$sheet->properties->sheetId]= $sheet->properties->title;
                    //$gdata_worksheets[]=$this->sheets;
                }
               //$gdata_worksheets[]=$gdata->worksheet_id;
                if($gdata->worksheet_id != '' && isset( $gdata_worksheets[$gdata->worksheet_id] )){
                    $range = 'List 2!A1:Z';
                    $worksheet =  $this->service->spreadsheets_values->get($gdata->spreadsheet_id, $range);
                   $cellFeed = $worksheet->getValues();

                    foreach( $cellFeed  As $row) {
                       
                       //$row = $cellEntry->getRow();
                        //$col = $cellEntry;
                       // $gdata_worksheets=$this->service;
                        
                    
                       if( $row > 1 ){
                    $gdata_columns=$row;
                           break;
                            
                       }
                   }
                }
            }
            
       
        } catch(Exception $e){
            
            $error = $e->getMessage();
        }

这是我的代码的一部分,其中描述了如何从 v3 迁移到 v4 电子表格 API。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-27
    • 2017-02-05
    • 2020-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多