2011-07-20 42 views
1

我的难题如下。我有很多数据存储在Google文档的各种电子表格中。将所有数据编译成单个电子表格(出于分析目的)达到大小限制。眼见为事,我需要另一种方法。使用PHP下载一系列Google文档(.csv文件)到我的网站

我写了一个Apps脚本来获取数据并将其全部导出到一系列对应的.csv文件。我现在需要从存储在Google Docs中的这30个.csv文件中获取数据,并将其导入到MySQL服务器中。这是最有效的方法吗?

现在,我需要一种方法将我的Google文档列表中的所有.csv文件导入服务器,在那里我的PHP脚本可以将它们导入到数据库。你能为我指出正确的方向吗?

我发现我的研究,这似乎相当接近在下面的帖子,但是当涉及到它适应我的目的,这是不幸的是我头上的方式...

http://gdatatips.blogspot.com/2009/07/download-google-doc-using-php-library.html

function download($client, $url, $format=null) { 
    $sessionToken = $client->getHttpClient()->getAuthSubToken(); 
    $opts = array(
    'http' => array(
     'method' => 'GET', 
     'header' => "GData-Version: 3.0\r\n". 
        "Authorization: AuthSub token=\"$sessionToken\"\r\n" 
    ) 
); 
    if ($url != null) { 
    $url = $url . "&exportFormat=$format"; 
    } 
    return file_get_contents($url, false, stream_context_create($opts)); 
} 

// TODO 1: setup a Zend_Gdata_Docs client in $docs_client 
// TODO 2: fetch a $feed or $entry 
$contentLink = $feed->entries[0]->content->getSrc(); 
$fileContents = download($docs_client, $contentLink, 'txt'); 
echo 'Contents of document "' . $feed->entries[0]->title . '":<hr>'; 
echo "<pre>$fileContents</pre>"; 

谢谢非常!任何援助将不胜感激! :)

+0

您曾经考虑过30CSV文件下载到您的服务器,做一个简单的LOAD DATA LOCAL INFILE? –

+0

嗨@Edgar,感谢您的评论!不幸的是,这是跟踪时间表数据的系统的一部分,其中每个30代表不同的人 - 具有不断更新的数据。我们理想地喜欢自动方法来下载csv文件,比如说每天一次;为了确保提供最新的信息。这甚至有可能吗? – Andrew

回答

0

我将使用从计划任务(即每天一次)启动的批处理文件下载.csv文件 - 然后在批处理文件中启动SQL c:\ sql \ update_timekeeping.sql 存储在update_timekeeping.sql文本文件中的SQl命令。此外还提供运行时间保持更新的历史....

+0

谢谢@Gator!这看起来像我们将要做的事情。设置一个工作来下载/上传我们本地机器上的文件。感谢大家的建议! – Andrew

0

在我看来,这file_get_contents()应该是类似这个脚本的PHP版本

function file_get_contents(){ 
    //This function generates a pdf of your current spreadsheet and emails it to yourself as attachment 
    //Make sure your spreadsheet is not too big. The pdf size tends to be 200kb/page and if too large 
    //if the pdf is too large the urlFetch might timeout 

    var AUTH_TOKEN = "xxxxxxxxxxxxxx"; //Enter your AUTH_TOKEN 
    //You can receive it from https://appscripts.appspot.com/getAuthToken 

    var ssID=SpreadsheetApp.getActiveSpreadsheet().getId()+"&gid="+SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getSheetId(); 
    var url = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=" 
     + ssID + "&exportFormat=csv"; 
    //Add &gid=x at the end of above url if you only want a particular sheet 
    //gid of a sheet can be obtained by the undocumented function getSheetId() 
    //ex: SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getSheetId(); 
    //exportFormat=xls did not work when I tried. I dont know why 

    var auth = "AuthSub token=\"" + AUTH_TOKEN + "\""; 

    var res = UrlFetchApp.fetch(url, {headers: {Authorization: auth}}); 
    var content=res.getContentText(); 
    return content 
}