繁体   English   中英

谷歌驱动器 api,将谷歌幻灯片导出为电源点(ppt 或 pptx) - node.js

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

我想使用 google api 将 google 幻灯片作为 power point 演示文稿(ppt 或 pptx)下载到我们的 node.js 服务器。 我已经设置了谷歌驱动器 api,并在我的驱动器文件夹中创建了一张幻灯片。 我正在尝试使用drive.files.export下载幻灯片,但由于幻灯片在 windows 或 mac 上并不是真正的东西,我试图通过将 mimeType 设置为application/vnd.ms-powerpoint将其转换为 power point 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,它就像一个魅力,唯一的区别是我导出一个文档(谷歌文档)并使用 mimeType application/pdf

在google drive的web UI中,我可以右键单击幻灯片并选择“下载”这个,下载一个可以用MS power point和Libre office打开的pptx文件。 - 所以令我惊讶的是谷歌 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 ,使用其文件 ID 获取您的 Google 幻灯片文件的文件资源 然后获取您喜欢的 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作为键访问导出链接。 使用链接下载文件。

问题和解决方法:

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

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

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

但从你的问题来看,你说如下。

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

当我使用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 文件打开。

参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM