如何下载已由Google Sheets打开的Excel文件?

如何下载已由Google Sheets打开的Excel文件?,excel,google-drive-api,Excel,Google Drive Api,我在使用GoogleDriveAPIv3下载Excel.xlsx文件时遇到了一些问题。我正在使用的代码如下(我正在使用): 您可以按照描述的步骤轻松运行此示例。这很好,但是,一旦有人用Google Sheets打开文件并对其进行修改,我就会看到以下错误 D2020-03-16 02:10:13.647293 Response[00000007] Response status: InternalServerError 'Internal Server Error' D2020-03-16 02:

我在使用GoogleDriveAPIv3下载Excel.xlsx文件时遇到了一些问题。我正在使用的代码如下(我正在使用):

您可以按照描述的步骤轻松运行此示例。这很好,但是,一旦有人用Google Sheets打开文件并对其进行修改,我就会看到以下错误

D2020-03-16 02:10:13.647293 Response[00000007] Response status: InternalServerError 'Internal Server Error'
D2020-03-16 02:10:13.653278 Response[00000007] An abnormal response wasn't handled. Status code is InternalServerError
D2020-03-16 02:10:13.660288 Response[00000007] Abnormal response is being returned. Status Code is InternalServerError
E2020-03-16 02:10:13.667240 Exception occurred while downloading media The service drive has thrown an exception: Google.GoogleApiException: Internal Server Error
   at Google.Apis.Download.MediaDownloader.<DownloadCoreAsync>d__31.MoveNext()
使用mimeType=“application/vnd.openxmlformats officedocument.spreadsheetml.sheet”

但是,我随后在以下错误中运行:

D2020-03-16 01:53:13.512928 Response[00000003] Response status: Forbidden 'Forbidden'
D2020-03-16 01:53:13.520906 Response[00000003] An abnormal response wasn't handled. Status code is Forbidden
D2020-03-16 01:53:13.525911 Response[00000003] Abnormal response is being returned. Status Code is Forbidden
E2020-03-16 01:53:13.538857 Exception occurred while downloading media The service drive has thrown an exception: Google.GoogleApiException: Google.Apis.Requests.RequestError
Export only supports Google Docs. [403]
Errors [
    Message[Export only supports Google Docs.] Location[ - ] Reason[fileNotExportable] Domain[global]
]

   at Google.Apis.Download.MediaDownloader.<DownloadCoreAsync>d__31.MoveNext()
D2020-03-16 01:53:13.512928响应[00000003]响应状态:禁止“禁止”
D2020-03-16 01:53:13.520906响应[00000003]未处理异常响应。禁止使用状态代码
D2020-03-16 01:53:13.525911响应[00000003]返回异常响应。禁止使用状态代码
E2020-03-16 01:53:13.538857下载媒体时发生异常服务驱动器引发异常:Google.GoogleAppeException:Google.API.Requests.RequestError
导出仅支持Google文档。[403]
错误[
消息[导出仅支持Google文档。]位置[-]原因[文件不可导出]域[全局]
]
在Google.api.Download.MediaDownloader.d_u31.MoveNext()上

因此,在这种特殊情况下,下载和导出都不起作用。还有什么我应该试试的吗?使用webContentLink()可以很好地(在浏览器中)工作,所以我想应该可以下载该文件。

我向谷歌提出了这个问题,似乎已经解决了(cf)。我今天再次尝试,按照原始问题中描述的步骤,我现在可以看到,在使用Google sheets编辑Excel文件后,其大小现在大于0,可以下载


由于此问题而无法下载的文件似乎仍存在相同的问题,但手动删除/重新加载这些文件应使其可下载。

我遇到了相同的问题,请告诉我解决方案,以防您设法修复它!嗨@AntoineChaussin!在检查和复制代码之后,我不清楚这种行为是来自代码本身还是API。为了避免遗漏任何重要的细节,您能否分享复制此问题的最小完整代码?在发布脚本之前,请隐藏或删除脚本上的任何私人可识别信息。最后,我也想知道,如果你在一个浏览器中测试这个主题,只有一个或多个谷歌用户登录,你能向我澄清一下吗?@Jacques GuzelHeron,你能看看我的代码吗?下面的代码可以工作,但仅适用于本机Google Sheets文件:
var request=service.files.Export(fileId,“application/vnd.openxmlformats officedocument.spreadsheetml.sheet”);downloadProgress=请求。DownloadWithStatus(流)
和以下代码一直工作到文件被打开并用Google Sheets编辑。尽管如此,我几乎100%的感觉它在两周前工作了
var request=service.Files.Get(fileId);downloadProgress=请求。DownloadWithStatus(流)你好@Jacques GuzelHeron!我已经编辑了这篇文章,以获得一个可执行的示例。若要复制,请在Google drive上上载Excel文件(您可以运行示例,然后确认其是否按预期工作),然后使用Google sheets打开文件,修改它,关闭GSheet选项卡,然后再次运行示例。你会得到一个错误。还请注意,这可以在OAuth Played中复制,因此我怀疑.NET代码是这里的问题。您好@AntoineChaussin和@JoãoTeixeira!我使用发布的代码来重现错误,并遵循您的每一个步骤,但我无法得到相同的错误。我可以下载并打开已编辑的.XLSX,没有问题。是否每个.XLSX都会出现这种行为?如果没有,请共享一个触发错误的电子表格,以便我可以复制它。
FilesResource.ExportRequest exportRequest = client.Files.Export(fileId, mimeType);

using (var stream = new System.IO.FileStream(fileName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite))
{
    await exportRequest.DownloadAsync(stream, cancellationToken);
}
D2020-03-16 01:53:13.512928 Response[00000003] Response status: Forbidden 'Forbidden'
D2020-03-16 01:53:13.520906 Response[00000003] An abnormal response wasn't handled. Status code is Forbidden
D2020-03-16 01:53:13.525911 Response[00000003] Abnormal response is being returned. Status Code is Forbidden
E2020-03-16 01:53:13.538857 Exception occurred while downloading media The service drive has thrown an exception: Google.GoogleApiException: Google.Apis.Requests.RequestError
Export only supports Google Docs. [403]
Errors [
    Message[Export only supports Google Docs.] Location[ - ] Reason[fileNotExportable] Domain[global]
]

   at Google.Apis.Download.MediaDownloader.<DownloadCoreAsync>d__31.MoveNext()