Drive.Files.export 在 Apps 脚本中不起作用

问题描述 投票:0回答:1

我们正在尝试使用 Google Apps 脚本中的 Advanced Drive 服务 v3 将 Google 文档导出为 Word。

查看文档应该很简单:

“Drive.Files.export(fileId:字符串,mimeType:字符串):void

将 Google Workspace 文档导出为请求的 MIME 类型并返回导出的字节内容。请注意,导出的内容限制为 10MB。 ”

那么我们试试这个

let file= DriveApp.getFileById("DriveFileIdHere");
let excelFile = Drive.Files.export(file.getId(), MimeType.MICROSOFT_WORD);

得到这个:

GoogleJsonResponseException:对drive.files.export 的API 调用失败并出现错误:导出需要alt=media 才能下载导出的内容。

因此尝试将其添加到通话中(未记录)

let excelFile = Drive.Files.export(file.getId(), MimeType.MICROSOFT_WORD, {alt:'media'});

我们得到这个:

HttpResponseException:响应代码:200。消息:PK����X��������������word/header1.xmlíZmsÛ

我们还缺少什么?

google-apps-script google-drive-api export-to-word
1个回答
0
投票

您看到的输出是字节排列

根据https://developers.google.com/drive/api/reference/rest/v3/files/export

列出了 mimeTypes https://developers.google.com/drive/api/reference/rest/v3/about/get?apix_params=%7B%22fields%22%3A%22exportFormats%22%7D

{
  "exportFormats": {
    "application/vnd.google-apps.document": [
      "application/rtf",
      "application/vnd.oasis.opendocument.text",
      "text/html",
      "application/pdf",
      "application/epub+zip",
      "application/zip",
      "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
      "text/plain"
    ],
    "application/vnd.google-apps.vid": [
      "video/mp4"
    ],
    "application/vnd.google-apps.spreadsheet": [
      "application/x-vnd.oasis.opendocument.spreadsheet",
      "text/tab-separated-values",
      "application/pdf",
      "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
      "text/csv",
      "application/zip",
      "application/vnd.oasis.opendocument.spreadsheet"
    ],
    "application/vnd.google-apps.jam": [
      "application/pdf"
    ],
    "application/vnd.google-apps.script": [
      "application/vnd.google-apps.script+json"
    ],
    "application/vnd.google-apps.presentation": [
      "application/vnd.oasis.opendocument.presentation",
      "application/pdf",
      "application/vnd.openxmlformats-officedocument.presentationml.presentation",
      "text/plain"
    ],
    "application/vnd.google-apps.form": [
      "application/zip"
    ],
    "application/vnd.google-apps.drawing": [
      "image/svg+xml",
      "image/png",
      "application/pdf",
      "image/jpeg"
    ],
    "application/vnd.google-apps.site": [
      "text/plain"
    ],
    "application/vnd.google-apps.mail-layout": [
      "text/plain"
    ]
  }
}

话虽这么说,导出将返回在列出的 mytypes 之一中转换的字节 https://developers.google.com/drive/api/reference/rest/v3/files/export#response-body

有关下载文件的更多详细信息 https://developers.google.com/drive/api/guides/manage-downloads

希望对你有帮助

© www.soinside.com 2019 - 2024. All rights reserved.