【问题标题】:Accessing properties in Google API Client Libraries (PHP)访问 Google API 客户端库 (PHP) 中的属性
【发布时间】:2017-03-29 13:27:08
【问题描述】:

我正在使用适用于 PHP 的 Google API 客户端库(测试版)对 Google 表格进行更改。我想访问电子表格中的第一个工作表,然后对其进行更改。

$service = new Google_Service_Sheets($this->client);
$spreadsheet = $service->spreadsheets->get($google_sheet_id);
$sheets = $spreadsheet->getSheets();

使用上面的代码,我有一个 Google_Service_Sheets_Sheet 对象数组。如果我var_dump() 的内容,我可以在名为modelData 的受保护数组中看到我需要的属性。我想访问 sheetId 属性。

...
["modelData":protected]=>
array(3) {
  ["properties"]=>
  array(5) {
    ["sheetId"]=>
    int(123456789)
    ["title"]=>
    string(15) "Page Popularity"
    ["index"]=>
    int(0)
    ["sheetType"]=>
    string(4) "GRID"
    ["gridProperties"]=>
    array(2) {
      ["rowCount"]=>
      int(4)
      ["columnCount"]=>
      int(4)
    }
  }
...

我已经尝试了所有我能想到的方法,包括标准的 PHP 魔术 get/set 语法,但没有任何效果。

$id = $sheets[0]->getSheetId(); // fails
$id = $sheets[0]->sheetId; // fails
$id = $sheets[0]->get('sheetId'); // fails

如何访问该对象的属性?

【问题讨论】:

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


    【解决方案1】:

    您可能需要检查 Reference 的 Sheets API

    在上图中,工作表(object) 具有包含 sheetID(您想要获取的内容)的属性(对象)。简而言之,它看起来像这样:

    Sheets->properties->sheetID
    

    或者:

    Sheets:{
        properties:{
             "sheetId": number,
             "title": string,
             "index": number,
             ........
     },
    ............
    }
    

    尝试检查来自post的代码:

    sheet_metadata = service.spreadsheets().get(spreadsheetId=spreadsheet_id).execute()
    sheets = sheet_metadata.get('sheets', '')
    title = sheets[0].get("properties", {}).get("title", "Sheet1")
    sheet_id = sheets[0].get("properties", {}).get("sheetId", 0)
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-03
      • 2017-07-29
      • 2020-04-14
      • 1970-01-01
      • 2017-10-18
      • 1970-01-01
      相关资源
      最近更新 更多