Ejemplo n.º 1
0
<?php

require_once 'vendor/autoload.php';
$G_CLIENT_ID = '1093409669917-r0b7k4fjs0asvjh4a1q193b6akmmb7ed.apps.googleusercontent.com';
$G_CLIENT_EMAIL = '*****@*****.**';
$G_CLIENT_KEY_PATH = 'TT85-20151107-148a6e2b5d19.p12';
$G_CLIENT_KEY_PW = 'notasecret';
$obj_client_auth = new Google_Client();
$obj_client_auth->setApplicationName('TestApplication');
$obj_client_auth->setClientId($G_CLIENT_ID);
$obj_client_auth->setAssertionCredentials(new Google_Auth_AssertionCredentials($G_CLIENT_EMAIL, array('https://spreadsheets.google.com/feeds', 'https://docs.google.com/feeds'), file_get_contents($G_CLIENT_KEY_PATH), $G_CLIENT_KEY_PW));
$obj_client_auth->getAuth()->refreshTokenWithAssertion();
$obj_token = json_decode($obj_client_auth->getAccessToken());
$accessToken = $obj_token->access_token;
$serviceRequest = new Google\Spreadsheet\DefaultServiceRequest($accessToken);
Google\Spreadsheet\ServiceRequestFactory::setInstance($serviceRequest);
$spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
$spreadsheetFeed = $spreadsheetService->getSpreadsheets();
$spreadsheet = $spreadsheetFeed->getByTitle('AAAAAA');
$worksheetFeed = $spreadsheet->getWorksheets();
$worksheet = $worksheetFeed->getByTitle('111111');
$listFeed = $worksheet->getListFeed();
// array from file
setlocale(LC_ALL, 'ja_JP.UTF-8');
$file = 'tmp/tmp.csv';
$data = file_get_contents($file);
$data = mb_convert_encoding($data, 'UTF-8', 'sjis-win');
$temp = tmpfile();
$csv = array();
fwrite($temp, $data);
rewind($temp);
function googleDocs_connect_to_spreadsheet($spreadsheetName, $worksheetName)
{
    global $googleDocs;
    if (!$googleDocs) {
        return;
    }
    global $accessToken;
    global $listFeed;
    debug("Trying accessToken '{$accessToken}'...");
    $request = new Google\Spreadsheet\Request($accessToken);
    $serviceRequest = new Google\Spreadsheet\DefaultServiceRequest($request);
    Google\Spreadsheet\ServiceRequestFactory::setInstance($serviceRequest);
    debug('Success!', 'success');
    // Find and connect to spreadsheet
    $spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
    $spreadsheetFeed = $spreadsheetService->getSpreadsheets();
    $spreadsheet = $spreadsheetFeed->getByTitle($spreadsheetName);
    // Get all worksheets
    $worksheetFeed = $spreadsheet->getWorksheets();
    // Get the worksheet
    $worksheet = $worksheetFeed->getByTitle($worksheetName);
    // Get all the rows
    $listFeed = $worksheet->getListFeed();
    /* Sample code */
    /*
    // Get all the rows
    $listFeed = $worksheet->getListFeed();
    
    // Loop over the rows
    foreach ($listFeed->getEntries() as $entry) {
    $values = $entry->getValues();
    echo '<p>';
    print_r($values);
    echo '</p>';
    }
    */
    // Insert row
    /*
    $row = array(
            'portalname' => 'Test',
        'recharge' => 'Test2',
            'ownsince' => '1/1/2014',
            'daysowned' => '',
            'zone' => 'Test3',
            'kmfromhome' => '999',
            'favorite' => '',
            'risk' => '10',
            'access' => '0',
            'record' => '0',
            'strategy' => 'none',
            'days' => 'some formula',
            'days_2'=> 'a formula',
            'dididiscoverit' => '',
            'enemies' => '',
            'allies' => '',
            'address' => '',
            'link' => '',
            'latitude' => '',
            'longitude' => '',
            'computationstuff' => 'formula',
            'computationstuff_2' => 'formula',
            'computationstuff_3' => 'formula'
    );
    
    $listFeed->insert($row);
    */
    // Display all the rows in the spreadsheet
    if (isset($_REQUEST['test']) && $_REQUEST['test'] == 'read') {
        // Loop over the rows
        foreach ($listFeed->getEntries() as $entry) {
            $values = $entry->getValues();
            echo '<p>';
            print_r($values);
            echo '</p>';
        }
    }
}