使用 Google Apps 脚本将 Google 幻灯片演示文稿下载为 PowerPoint 文档?
Downloading a Google Slides presentation as PowerPoint doc using Google Apps Script?
Google Slides 的 GUI 提供下载 GSlides 演示文稿作为 Powerpoint (myFile.pptx)。我在 Google Apps 脚本文档中找不到等效项 - 任何指针?
编辑
感谢评论和回答,我尝试了这个片段:
function testFileOps() {
// Converts the file named 'Synthese' (which happens to be a Google Slide doc) into a pptx
var files = DriveApp.getFilesByName('Synthese');
var rootFolder = DriveApp.getRootFolder();
while (files.hasNext()) {
var file = files.next();
var blobPptx = file.getBlob().getAs('application/vnd.openxmlformats-officedocument.presentationml.presentation');
var result = rootFolder.createFile(blobPptx);
}
}
它returns一个错误:
Converting from application/vnd.google-apps.presentation to
application/vnd.openxmlformats-officedocument.presentationml.presentation
is not supported. (line 7, file "Code")
第二次编辑
根据评论中的另一个建议,我尝试从 Google App Script 进行 http 调用,这将直接将 gslides 转换为 pptx,没有大小限制。它在 G 驱动器上生成一个文件,但该文件已损坏/不可读。 GAS 脚本:
function convertFileToPptx() {
// Converts a public Google Slide file into a pptx
var rootFolder = DriveApp.getRootFolder();
var response = UrlFetchApp.fetch('https://docs.google.com/presentation/d/1Zc4-yFoUYONXSLleV_IaFRlNk6flRKUuAw8M36VZe-4/export/pptx');
var blobPptx = response.getContent();
var result = rootFolder.createFile('test2.pptx',blobPptx,MimeType.MICROSOFT_POWERPOINT);
}
备注:
- 我得到了 pptx 的 mime 类型 here
- 使用 mime 类型
'pptx'
returns 相同的错误信息
如 tehhowch 所述,您可以从云端硬盘中获取 Google 幻灯片文件并将其作为 .pptx
。 (不确定 MIME 类型。)
这个修改怎么样?
修改点:
response.getContent()
returns 字节数组。所以请使用response.getBlob()
.
修改后的脚本:
function convertFileToPptx() {
var fileId = "1Zc4-yFoUYONXSLleV_IaFRlNk6flRKUuAw8M36VZe-4";
var outputFileName = "test2.pptx";
var url = 'https://docs.google.com/presentation/d/' + fileId + '/export/pptx';
var rootFolder = DriveApp.getRootFolder();
var response = UrlFetchApp.fetch(url);
var blobPptx = response.getBlob();
var result = rootFolder.createFile(blobPptx.setName(outputFileName));
}
注:
- 如果您想转换 Google 幻灯片,这些幻灯片未在您的 Google 驱动器中发布,请使用访问令牌。届时请修改
url
如下。
var url = 'https://docs.google.com/presentation/d/' + fileId + '/export/pptx?access_token=' + ScriptApp.getOAuthToken();
DriveApp.createFile()
默认情况下在根文件夹中创建一个文件。
参考文献:
我添加了带有令牌部分和特定文件夹的所有修改
function convertFileToPptx() {
var fileId = "Your File ID";
var outputFileName = "name.pptx";
var url = 'https://docs.google.com/presentation/d/' + fileId + '/export/pptx';
//var rootFolder = DriveApp.getRootFolder();
var rootFolder = DriveApp.getFolderById("Your Folder ID")
var params = {method:"GET", headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};
var response = UrlFetchApp.fetch(url,params);
var blobPptx = response.getBlob();
var result = rootFolder.createFile(blobPptx.setName(outputFileName));
}
Google Slides 的 GUI 提供下载 GSlides 演示文稿作为 Powerpoint (myFile.pptx)。我在 Google Apps 脚本文档中找不到等效项 - 任何指针?
编辑
感谢评论和回答,我尝试了这个片段:
function testFileOps() {
// Converts the file named 'Synthese' (which happens to be a Google Slide doc) into a pptx
var files = DriveApp.getFilesByName('Synthese');
var rootFolder = DriveApp.getRootFolder();
while (files.hasNext()) {
var file = files.next();
var blobPptx = file.getBlob().getAs('application/vnd.openxmlformats-officedocument.presentationml.presentation');
var result = rootFolder.createFile(blobPptx);
}
}
它returns一个错误:
Converting from application/vnd.google-apps.presentation to application/vnd.openxmlformats-officedocument.presentationml.presentation is not supported. (line 7, file "Code")
第二次编辑
根据评论中的另一个建议,我尝试从 Google App Script 进行 http 调用,这将直接将 gslides 转换为 pptx,没有大小限制。它在 G 驱动器上生成一个文件,但该文件已损坏/不可读。 GAS 脚本:
function convertFileToPptx() {
// Converts a public Google Slide file into a pptx
var rootFolder = DriveApp.getRootFolder();
var response = UrlFetchApp.fetch('https://docs.google.com/presentation/d/1Zc4-yFoUYONXSLleV_IaFRlNk6flRKUuAw8M36VZe-4/export/pptx');
var blobPptx = response.getContent();
var result = rootFolder.createFile('test2.pptx',blobPptx,MimeType.MICROSOFT_POWERPOINT);
}
备注:
- 我得到了 pptx 的 mime 类型 here
- 使用 mime 类型
'pptx'
returns 相同的错误信息
如 tehhowch 所述,您可以从云端硬盘中获取 Google 幻灯片文件并将其作为 .pptx
。 (不确定 MIME 类型。)
这个修改怎么样?
修改点:
response.getContent()
returns 字节数组。所以请使用response.getBlob()
.
修改后的脚本:
function convertFileToPptx() {
var fileId = "1Zc4-yFoUYONXSLleV_IaFRlNk6flRKUuAw8M36VZe-4";
var outputFileName = "test2.pptx";
var url = 'https://docs.google.com/presentation/d/' + fileId + '/export/pptx';
var rootFolder = DriveApp.getRootFolder();
var response = UrlFetchApp.fetch(url);
var blobPptx = response.getBlob();
var result = rootFolder.createFile(blobPptx.setName(outputFileName));
}
注:
- 如果您想转换 Google 幻灯片,这些幻灯片未在您的 Google 驱动器中发布,请使用访问令牌。届时请修改
url
如下。var url = 'https://docs.google.com/presentation/d/' + fileId + '/export/pptx?access_token=' + ScriptApp.getOAuthToken();
DriveApp.createFile()
默认情况下在根文件夹中创建一个文件。
参考文献:
我添加了带有令牌部分和特定文件夹的所有修改
function convertFileToPptx() {
var fileId = "Your File ID";
var outputFileName = "name.pptx";
var url = 'https://docs.google.com/presentation/d/' + fileId + '/export/pptx';
//var rootFolder = DriveApp.getRootFolder();
var rootFolder = DriveApp.getFolderById("Your Folder ID")
var params = {method:"GET", headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};
var response = UrlFetchApp.fetch(url,params);
var blobPptx = response.getBlob();
var result = rootFolder.createFile(blobPptx.setName(outputFileName));
}