google 驱动 api,导出 google 幻灯片作为幻灯片(ppt 或 pptx)- node.js

google drive api, export google slide as power point (ppt or pptx) - node.js

我想使用 google api 将 google 幻灯片作为 PowerPoint 演示文稿(ppt 或 pptx)下载到我们的 node.js 服务器。我设置了 google 驱动器 api,并且在我的驱动器文件夹中创建了一张幻灯片。我正在尝试使用 drive.files.export 下载幻灯片,但由于幻灯片在 windows 或 mac 上并不是真正的东西,我正在尝试通过设置将其转换为幻灯片mimeType 为 application/vnd.ms-powerpoint:

  const { drive } = await Google.getInstance();
  const fileId = 'SomeLongTemplateId';
  const fileName = 'test slide processing';
  console.log('exporting ppt');
  const ppt = await drive.files.export(  // report.js:343
    { fileId, mimeType: 'application/vnd.ms-powerpoint' },
    { responseType: 'arraybuffer' }
  );
  return Buffer.from(ppt.data);

但是,这会抛出错误并且似乎不起作用:

error: {
    message: ArrayBuffer {
      [Uint8Contents]: <7b 0a 20 22 65 72 72 6f 72 22 3a 20 7b 0a 20 20 22 65 72 72 6f 72 73 22 3a 20 5b 0a 20 20 20 7b 0a 20 20 20 20 22 64 6f 6d 61 69 6e 22 3a 20 22 67 6c 6f 62 61 6c 22 2c 0a 20 20 20 20 22 72 65 61 73 6f 6e 22 3a 20 22 62 61 64 52 65 71 75 65 73 74 22 2c 0a 20 20 20 20 
22 6d 65 73 73 61 67 65 22 3a 20 ... 195 more bytes>,
      byteLength: 295
    },
    stack: 'Error: [object ArrayBuffer]\n' +
      '    at Gaxios._request (C:\Users\rasmu\Documents\triggerz\triggerz\node_modules\gaxios\build\src\gaxios.js:129:23)\n' +
      '    at processTicksAndRejections (internal/process/task_queues.js:95:5)\n' +
      '    at async JWT.requestAsync (C:\Users\rasmu\Documents\triggerz\triggerz\node_modules\google-auth-library\build\src\auth\oauth2client.js:345:18)\n' +
      '    at async processionSlide (C:\Users\rasmu\Documents\triggerz\triggerz\entity-access\src\act\report.js:343:15)'
  },

不幸的是,它并没有真正说明问题是什么,但它肯定不起作用。我也尝试使用 mimeType application/vnd.openxmlformats-officedocument.presentationml.presentation 导出为 pptx,但这给出了类似的错误。

我习惯于使用这种技术导出 pdf,它就像一个魅力,唯一的区别是我导出一个文档(google 文档)并使用 mimeType application/pdf

在 google 驱动器的网络 UI 中,我可以右键单击幻灯片并选择“下载”这个,下载一个 pptx 文件,我可以用 MS power point 和自由办公室。 - 所以让我很惊讶的是 google node.js api 库无法以相同的方式导出。

关于如何将幻灯片导出为适用于 windows/mac 的演示文稿,有什么想法吗?

您可以尝试按照此参考资料中的建议将您的 responseType 替换为 streamgoogle api nodejs client [Help] Google Drive export example not working

const ppt = await drive.files.export(  // report.js:343
    { fileId, mimeType: 'application/vnd.ms-powerpoint' },
    { responseType: 'stream' }
  )

如果上述解决方案仍然失败,我可以建议的一种解决方法是使用 Drive.Files.Get, Get the file resource 您的 Google 使用文件 ID 的幻灯片文件。然后获取您首选的 mimeType 的 exportLinks

示例(API 资源管理器):

回复:

{
 "name": "Untitled presentation",
 "exportLinks": {
  "application/vnd.oasis.opendocument.presentation": "https://docs.google.com/feeds/download/presentations/Export?id=13vwP4xQ_nVAHCPCuXI3GQKhMJrQ5goTfX7mh6qP5fKM&exportFormat=odp&resourcekey=0-nwpcI7sa0wRmreOT5Z6RJg",
  "application/pdf": "https://docs.google.com/feeds/download/presentations/Export?id=13vwP4xQ_nVAHCPCuXI3GQKhMJrQ5goTfX7mh6qP5fKM&exportFormat=pdf&resourcekey=0-nwpcI7sa0wRmreOT5Z6RJg",
  "application/vnd.openxmlformats-officedocument.presentationml.presentation": "https://docs.google.com/feeds/download/presentations/Export?id=13vwP4xQ_nVAHCPCuXI3GQKhMJrQ5goTfX7mh6qP5fKM&exportFormat=pptx&resourcekey=0-nwpcI7sa0wRmreOT5Z6RJg",
  "text/plain": "https://docs.google.com/feeds/download/presentations/Export?id=13vwP4xQ_nVAHCPCuXI3GQKhMJrQ5goTfX7mh6qP5fKM&exportFormat=txt&resourcekey=0-nwpcI7sa0wRmreOT5Z6RJg"
 }
}
  • 使用 application/vnd.openxmlformats-officedocument.presentationml.presentation 作为密钥访问导出 link。使用 link.
  • 下载文件

问题和解决方法:

不幸的是,似乎在当前阶段,当我看到 Drive API 的带有“About: get”的 exportFormats 时,application/vnd.google-apps.presentation 的 mimeType(Google 幻灯片)可以导出为以下 mimeTypes。

"application/vnd.google-apps.presentation": [
  "application/vnd.oasis.opendocument.presentation",
  "application/pdf",
  "application/vnd.openxmlformats-officedocument.presentationml.presentation",
  "text/plain"
],

由此看来,当使用导出方法时,Google 幻灯片无法导出为 application/vnd.ms-powerpoint。这样,当我对此进行测试时,就会发生错误。但是从上面的exportFormats发现,当使用application/vnd.openxmlformats-officedocument.presentationml.presentation的mimeType时,Google的幻灯片可以导出为PPTX。

但是从你的问题来看,你是这样说的。

Unfortunately, it doesn't really say what the problem is, but it definitely doesn't work. I have also tried exporting as pptx using the mimeType application/vnd.openxmlformats-officedocument.presentationml.presentation but this gave a similar error.

当我使用 application/vnd.openxmlformats-officedocument.presentationml.presentation 的 mimeType 测试您的脚本时,没有发生错误。所以请修改你的脚本如下,再测试一下

修改后的脚本:

const fileId = 'SomeLongTemplateId';
const fileName = "test slide processing";
console.log("exporting ppt");
const ppt = await drive.files.export(
  {
    fileId: fileId,
    mimeType: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
  },
  { responseType: "arraybuffer" }
);
fs.writeFile("sample.pptx", Buffer.from(ppt.data), (err) => {
  if (err) console.log(err);
});

// return Buffer.from(ppt.data);
  • 当我测试这个修改后的脚本时,我可以确认可以创建 sample.pptx 的文件,并且可以将文件作为 PPTX 文件打开。

参考: