DriveApp从DocX转换为PDF失败(谷歌应用脚本) [英] DriveApp conversion from DocX to PDF fails (google apps script)
问题描述
我正尝试转换我的Google云端硬盘中现有的.DOCX文件。
它会一直工作到新的(PDF)文件应该被创建,然后我得到这个错误信息:
从应用程序/ vnd.openxmlformats-officedocument.wordprocessingml.document到应用程序的转换/ pdf失败。
相关的DOCX文件不应该被破坏,因为它可以通过Google文档打开并从那里手动转换为PDF。
我已经启用Drive API(在Drive和API Console中)
$ b 代码:
$ b $ $ $ $ $ $ $ $ $ $ $变种docId = FILEID;
var f = DriveApp.getFileById(docId);
var n = f.getName();
var docFolder = f.getParents()。next()。getId();
var docblob = f.getAs('application / pdf');
n = n.replace(。docx,。pdf);
docblob.setName(n);
var bn = docblob.getName();
var t = docblob.getContentType();
Logger.log(bn + - >+ t); //< - works
var file = DriveApp.createFile(docblob); //<< - error msg here
var fileId = file.getId();
moveFileId(fileId,docFolder);
return(fileId);
在Google上,它不可能直接从 docx
转换为 pdf
。但是,在将 docx
转换为Google文档后,它可以转换为 pdf
。为了将 docx
转换为Google文档,您可以使用使用高级Google服务的Drive API。为此,请按以下步骤启用高级Google服务的Drive API。
- 在脚本编辑器中,选择资源>高级Google服务
- 在出现的对话框中,单击Drive API v2的开关。
- 单击确定按钮。
- In the script editor, select Resources > Advanced Google services
- In the dialog that appears, click the on/off switch for Drive API v2.
- Click OK button.
ol>
高级Google服务的详细信息是 我认为在API控制台启用Drive API已经完成了,因为您已经使用了 使用Drive API如下所示。
DriveApp
在脚本上。当脚本中使用 DriveApp
时,Google会在API控制台上自动启用Drive API。
修改过的脚本:
function convertPDF(docx_id ){
var docx = DriveApp.getFileById(docx_id);
var docFolder = docx.getParents()。next()。getId();
var n = docx.getName();
var res = Drive.Files.insert({//此处使用高级Google服务的Drive API。
mimeType:application / vnd.google-apps.document,
title:n
},docx.getBlob());
var f = DriveApp.getFileById(res.id).getBlob();
var docblob = f.getAs('application / pdf');
n = n.replace(。docx,。pdf);
docblob.setName(n);
var bn = docblob.getName();
var t = docblob.getContentType();
Logger.log(bn + - >+ t);
var file = DriveApp.createFile(docblob);
var fileId = file.getId();
Logger.log(fileId)
moveFileId(fileId,docFolder);
return(fileId);
}
我不知道 moveFileId()
。所以如果在这一行发生错误,请检查函数。
注意:
在这种情况下, Google文档是作为转换为PDF的临时文件而创建的。文件名与docx文件相同。因此,如果您不需要,请删除它。
I'm trying to convert an existing .DOCX file on my Google Drive. It works up until the new (PDF) file should be created, then I get this error message: "Conversion from application/vnd.openxmlformats-officedocument.wordprocessingml.document to application/pdf failed".
The relevant DOCX file should not be corrupted as it can be opened by Google Document and manually convertred to PDF from there.
I have enabled Drive API (both in Drive and in API Console)
Anyone else seen this problem?
Code:
function convertPDF(fileid) {
var docId = fileid;
var f=DriveApp.getFileById(docId);
var n=f.getName();
var docFolder = f.getParents().next().getId();
var docblob = f.getAs('application/pdf');
n=n.replace(".docx",".pdf");
docblob.setName(n);
var bn=docblob.getName();
var t=docblob.getContentType();
Logger.log(bn+"-->"+t); // <-- works
var file = DriveApp.createFile(docblob); // <<- error msg here
var fileId = file.getId();
moveFileId(fileId, docFolder);
return (fileId);
}
At Google, it cannot be directly converted from docx
to pdf
. But, after it converted docx
to Google document, it can convert to pdf
. In order to convert docx
to Google document, you can use Drive API using Advanced Google services. For this, please enabling Drive API of Advanced Google services as follows.
The detail information of Advanced Google services is here.
I think that enabling Drive API at API console has already been done, because you have already used DriveApp
on your script. Google automatically enables Drive API on API console when DriveApp
is used in the script.
The modified script using Drive API is as follows.
Modified script :
function convertPDF(docx_id) {
var docx = DriveApp.getFileById(docx_id);
var docFolder = docx.getParents().next().getId();
var n = docx.getName();
var res = Drive.Files.insert({ // Here, Drive API of Advanced Google services is used.
"mimeType": "application/vnd.google-apps.document",
"title": n
}, docx.getBlob());
var f = DriveApp.getFileById(res.id).getBlob();
var docblob = f.getAs('application/pdf');
n=n.replace(".docx",".pdf");
docblob.setName(n);
var bn=docblob.getName();
var t=docblob.getContentType();
Logger.log(bn+"-->"+t);
var file = DriveApp.createFile(docblob);
var fileId = file.getId();
Logger.log(fileId)
moveFileId(fileId, docFolder);
return (fileId);
}
I don't know about moveFileId()
. So if an error occurs at this line, please check the function.
Note :
In this case, Google document is created as a temporally file for converting to PDF. The filename is the same to the docx file. So if it is not required for you, please delete it.
这篇关于DriveApp从DocX转换为PDF失败(谷歌应用脚本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!