如何上传Google Spread Sheet中存在的XLS文件,php?

时间:2016-07-31 16:45:01

标签: php excel google-spreadsheet-api google-api-client

我用

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

我在谷歌驱动器中创建新的电子表格

   "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()
    }

我有altUrl - https://docs.google.com/spreadsheets/d/xxxxx/edit?usp=drivesdk,这没关系,请转到这个网址我有一个emty google电子表格

我知道如何上传存在的xls文件,在PHPExel中生成并在google驱动器中保存和上传

    "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()
    }

我有alternativeUrl - https://drive.google.com/file/d/xxxxxx/view?usp=drivesdk,这没关系,转到此网址我看到文件和按钮在GoogleSpreadSheet中打开

而这一刻我的xls文件不是googleSpreadSheet,只有当我在googleSpreadSheet中打开我的文件时,这个文件才会获得googleSpreadSheetId

我可以通过ID或标题获得谷歌电子表格

    "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);

我尝试在application/vnd.google-apps.spreadsheet中替换momeType文件,但在googledrive emty googleSpreadSheet

我拒绝相信我无法立即在googleSpreadSheet中下载我的文件或正确下载一个请求。我尝试找到解决方案,但是为期两天的搜索没有产生任何结果。

如何在googleSpreadSheed中下载我的xls文件?请帮忙

1 个答案:

答案 0 :(得分:0)

我发现:)

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
    ];