2016-10-05 71 views
0

在谷歌文檔中,您可以看到並瀏覽文檔大綱。我試圖通過Google Drive API訪問此大綱,但找不到相關文檔。這是我現在的代碼:谷歌驅動器API - 獲取文檔大綱

//authenticate 
    $this->authenticate(); 

    $Service = new Google_Service_Drive($this->Client); 
    $File = $Service->files->get($FileID); 

    return $File; 

我得到文檔對象回來,但我找不到任何函數返回大綱。我需要大綱鏈接從我的應用程序訪問文檔的特定部分。任何想法如何能夠實現?

回答

0

我終於用DaImTo解決了這個問題,指着我朝着正確的方向前進。獲得file resource後,我用它獲取文檔HTML代碼的導出鏈接,然後使用該鏈接使用Google_Http_Request檢索該文檔的HTML內容。 (Google documentation這部分)

public function retrive_file_outline($FileID) { 
    //authenticate 
    $this->authenticate(); 

    $Service = new Google_Service_Drive($this->Client); 
    $File = $Service->files->get($FileID); 

    $DownloadUrl = $File->getExportLinks()["text/html"]; 

    if ($DownloadUrl) { 
     $Request = new Google_Http_Request($DownloadUrl, 'GET', null, null); 
     $HttpRequest = $Service->getClient()->getAuth()->authenticatedRequest($Request); 
     if ($HttpRequest->getResponseHttpCode() == 200) { 
      return array($File, $HttpRequest->getResponseBody()); 
     } else { 
      // An error occurred. 
      return null; 
     } 
    } else { 
     // The file doesn't have any content stored on Drive. 
     return null; 
    } 
} 

之後,我解析使用DOMDocument HTML內容。所有標題都有id屬性,用作anchor鏈接。我檢索了所有標題(h1到h6)的id,並將其與我的文檔編輯網址連接起來。這給了我所有的大綱鏈接。這裏是解析和連接部分:

public function test($FileID) { 
    $File = $this->model_google->retrive_file_outline($FileID); 

    $DOM = new DOMDocument; 
    $DOM->loadHTML($File[1]); 

    $TagNames = ["h1", "h2", "h3", "h4", "h5", "h6"]; 
    foreach($TagNames as $TagName) { 
     $Items = $DOM->getElementsByTagName($TagName); 
     foreach($Items as $Item) { 
      $ID = $Item->attributes->getNamedItem("id"); 
      echo "<a target='_blank' href='" . $File[0]->alternateLink ."#heading=". $ID->nodeValue . "'>" . $Item->nodeValue . "</a><br />"; 
     } 
    } 
    //echo $File; 
} 

編輯: 我合併功能retrieve_file_outline和考不上retrieve_file_outline,我得到一個返回的鏈接和IDS文檔標題的陣列功能:

public function retrive_file_outline($FileID) { 
    //authenticate 
    $this->authenticate(); 

    $Service = new Google_Service_Drive($this->Client); 
    $File = $Service->files->get($FileID); 

    $DownloadUrl = $File->getExportLinks()["text/html"]; 

    if ($DownloadUrl) { 
     $Request = new Google_Http_Request($DownloadUrl, 'GET', null, null); 
     $HttpRequest = $Service->getClient()->getAuth()->authenticatedRequest($Request); 
     if ($HttpRequest->getResponseHttpCode() == 200) { 
      $DOM = new DOMDocument; 
      $DOM->loadHTML($HttpRequest->getResponseBody()); 

      $TagNames = ["h1", "h2", "h3", "h4", "h5", "h6"]; 
      $Headings = array(); 
      foreach($TagNames as $TagName) { 
       $Items = $DOM->getElementsByTagName($TagName); 
       foreach($Items as $Item) { 
        $ID = $Item->attributes->getNamedItem("id"); 
        $Heading = array(
         "link" => $File->alternateLink . "#heading=" . $ID->nodeValue, 
         "heading_id" => $ID->nodeValue, 
         "title" => $Item->nodeValue 
        ); 

        array_push($Headings, $Heading); 
       } 
      } 

      return $Headings; 
     } else { 
      // An error occurred. 
      return null; 
     } 
    } else { 
     // The file doesn't have any content stored on Drive. 
     return null; 
    } 
} 
1

File.get返回一個file resource所有文件資源只是文件的元數據。其關於存儲在谷歌驅動器上的文件的信息。

您將需要在某個文檔應用程序中加載它以查找任何大綱鏈接。元數據不包含有關存儲在文件中的數據的任何內容。