asimlqt / php-google-spreadsheet-client

A PHP library for accessing and manipulating Google Spreadsheets
Other
543 stars 154 forks source link

How to upload exist XLS file in Google Spread Sheet, php? #153

Closed shubaivan closed 8 years ago

shubaivan commented 8 years ago

I use

    "google/apiclient": "1.1.7",
    "asimlqt/php-google-spreadsheet-client": "2.3.7",
    "phpoffice/phpexcel": "1.8.1"

And I create new spread sheet in google drive

   "google/apiclient":

    $service = new \Google_Service_Drive($client->getGoogleClient());
    $file = new \Google_Service_Drive_DriveFile();

    $file->setMimeType('application/vnd.google-apps.spreadsheet');
    $insertArray = [
        'mimeType' => 'application/vnd.google-apps.spreadsheet',
    ];
    try {
        $createdFile = $service->files->insert($file, $insertArray);
        $spreadsheetId = $createdFile->getId();
    } catch (\Exception $e) {
        $e->getMessage()
    }

and I have alternativeUrl - https://docs.google.com/spreadsheets/d/xxxxx/edit?usp=drivesdk and this is ok, go to this url I have emty google spread sheet

And I know how to upload exist xls file, generate in PHPExel and save and upload in google drive

    "phpoffice/phpexcel":

    $phpExcel = new \PHPExcel();
    $ews = $phpExcel->getSheet(0);
    $ews->setTitle(Reports::LIST_AOG);

    $objWriter = new \PHPExcel_Writer_Excel2007($phpExcel);

    $path = 'uploads/';
    $rootDir = $this->get('kernel')->getRootDir() . '/../web/';
    $filePath = $rootDir . $path.$nameFile;

    $objWriter->save($filePath);
    $fileUpload = new File($filePath);

    $insertArray = [
        'mimeType' => $fileUpload->getMimeType(),
        'uploadType' => 'media',
        'data' => file_get_contents($fileUpload)
    ];

    "google/apiclient":

    $service = new \Google_Service_Drive($client->getGoogleClient());
    $file = new \Google_Service_Drive_DriveFile();

    $file->setTitle($nameFile);
    $file->setMimeType($fileUpload->getMimeType());

    try {
        $createdFile = $service->files->insert($file, $insertArray);
        $spreadsheetId = $createdFile->getId();
    } catch (\Exception $e) {
        $e->getMessage()
    }

and I have alternativeUrl - https://drive.google.com/file/d/xxxxxx/view?usp=drivesdk and this is ok, go to this url I see file and button open in GoogleSpreadSheet

and this moment my xls file not googleSpreadSheet, only when I open my file in googleSpreadSheet this file will get googleSpreadSheetId

and I can get google spread sheet by id or title

    "asimlqt/php-google-spreadsheet-client":

    $serviceRequest = new DefaultServiceRequest($arrayAccessTokenClient['access_token']);
    ServiceRequestFactory::setInstance($serviceRequest);

    $spreadsheetService = new SpreadsheetService();
    $scopeSpreadSheet = $spreadsheetService->getSpreadsheets();

    $spreadsheetByTitle = $scope->getByTitle($createdFile->getTitle());        
    $spreadsheetById = $spreadsheetService->getSpreadsheetById($spreadsheetId);

I try replace momeType file in application/vnd.google-apps.spreadsheet but have in googledrive emty googleSpreadSheet

And I refuse to believe that I can not download my file in googleSpreadSheet immediately or right one request. I try find solution but the two day search yielded no results.

How to download my xls file in googleSpreadSheed in one moment ??? Help please

shubaivan commented 8 years ago

I find :)

convert boolean Whether to convert this file to the corresponding Google Docs format. (Default: false)

    $insertArray = [
        'mimeType' => $fileUpload->getMimeType(),
        'uploadType' => 'media',
        'data' => file_get_contents($fileUpload),
        'convert' => true
    ];