【问题标题】:Convert and create a Google Drive Spreadsheet from a local CSV file via the Google Drive API通过 Google Drive API 从本地 CSV 文件转换和创建 Google Drive 电子表格
【发布时间】:2015-07-22 09:34:02
【问题描述】:

我正在尝试将本地 CSV 文件上传到 Google Drive,并在那里像 Google 电子表格一样显示它。但是,当我转到我的 Google 云端硬盘并单击我的文件的链接时,我只能下载它,而不能以电子表格的形式查看它。我试过使用 ?convert=true 但文件没有被转换。我也尝试使用 application/vnd.google-apps.spreadsheet 作为 mime 类型,但注意到更改,否则我收到 400 Bad request response。

异常“Google_ServiceException”和消息“调用 POST 时出错” https://www.googleapis.com/upload/drive/v2/files?convert=true&uploadType=multipart: (400) 提供的 MIME 类型无效 /var/www/html/twittery/lib/google-api-php-client/src/io/Google_REST.php:66 堆栈跟踪:#0 /var/www/html/twittery/lib/google-api-php-client/src/io/Google_REST.php(36): Google_REST::decodeHttpResponse(Object(Google_HttpRequest)) #1 /var/www/html/twittery/lib/google-api-php-client/src/service/Google_ServiceResource.php(186): Google_REST::execute(Object(Google_HttpRequest)) #2 /var/www/html/twittery/lib/google-api-php-client/src/contrib/Google_DriveService.php(484): Google_ServiceResource->__call('insert', Array) #3 /var/www/html/twittery/generate_tweets_GSpreadsheet.php(84): Google_FilesServiceResource->插入(对象(Google_DriveFile),数组) #4 {主}

【问题讨论】:

    标签: google-drive-api


    【解决方案1】:

    上传 CSV 文件的 mimeType 应该是“text/csv”。

    根据 Files:insert 的文档,请确保您在

    中提供相同的 mimeType
    $file->setMimeType($mimeType);
    

    还有

    $createdFile = $service->files->insert($file, array(
          'data' => $data,
          'mimeType' => $mimeType,
          'convert' => true,
    ));
    

    在 Google Drive 中创建或更新文件时,您可以使用 convert 查询参数将上传的文件转换为 Google Docs、Sheets 或 Slides 文档。

    您可以参考此page 以查看您上传的每种文件类型的支持转换。

    【讨论】:

      猜你喜欢
      • 2012-12-16
      • 1970-01-01
      • 2012-09-05
      • 1970-01-01
      • 2012-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-22
      相关资源
      最近更新 更多