【问题标题】:download xls/xlsx file from google drive api从 google drive api 下载 xls/xlsx 文件
【发布时间】:2021-09-22 10:44:47
【问题描述】:

我正在尝试下载 xls 或 xlsx 文件,但出现错误:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "fileNotExportable",
    "message": "Export only supports Docs Editors files."
   }
  ],
  "code": 403,
  "message": "Export only supports Docs Editors files."
 }
}

我正在使用 nodejs。是否可以通过api下载xls xlsx文件?

代码示例:

export function getFileFromStream(auth, fileId, mimeType) {
  const destPath = `/tmp/${fileId}.xls`;
  const dest = fs.createWriteStream(destPath);
  return new Promise(async (resolve, reject) => {
    const drive = google.drive({version: 'v3', auth});
    drive.files.export(
      { fileId, mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'},
      { responseType: 'stream'},
      (errrr, response) => {
          response.data
            .on('end', function() {
            console.log('downloaded')
            })
            .on('error', function(err) {
              console.log('Error during download', err);
            })
            .pipe(dest);
      });
  });
}

【问题讨论】:

    标签: google-drive-api google-api-nodejs-client


    【解决方案1】:

    files.export 方法为documentation sample 中使用的只能用于下载 Google Workspace 文档

    要下载非 Google Workspace 文档,您需要使用 files.get 方法,如 sample for Download a file stored on Google Drive

    export function getFileFromStream(auth, fileId, mimeType) {
      const destPath = `/tmp/${fileId}.xls`;
      const dest = fs.createWriteStream(destPath);
      return new Promise(async (resolve, reject) => {
        const drive = google.drive({version: 'v3', auth});
        drive.files.get({
          fileId: fileId,
          alt: 'media'
        })
          .on('end', function () {
            console.log('Done');
          })
          .on('error', function (err) {
            console.log('Error during download', err);
          })
        .pipe(dest);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-17
      • 1970-01-01
      • 1970-01-01
      • 2015-02-06
      相关资源
      最近更新 更多