wiv*_*vku 3 google-apps-script google-drive-api
我想使用 Google Apps 脚本将本机 Google 电子表格/文档/绘图/演示文稿文件导出到同一文件夹中的另一种格式。我已启用高级驱动器服务,并查看了在您的应用程序中打开和转换 Google 文档的说明。
希望我可以使用Export获取一个文件,然后使用Insert保存/重命名该文件。
试图实现类似的目标:
var file = {fileId: "1yDe...lD2U",
folderId: "0B_...hZ1k",
mimetype:"application/vnd.google-apps.document",
targetMimetype:"application/pdf",
targetName:"my converted document"}
var newFile = Drive.Files.export(file.fileId, file.targetMimetype)
// results in error message:
// "Export requires alt=media to download the exported content."
// I am sure the above is incomplete, but unsure how to actually
// save the exported file, in the correct location, with correct filename
Run Code Online (Sandbox Code Playgroud)
更新:将 alt=media 添加到调用 ( var newFile = Drive.Files.export(file.fileId, file.targetMimetype, {alt: "media"})) 时,脚本会退出并显示错误代码 200,并在错误消息中显示 PDF 内容。与问题 6573类似。
更新:这可能还不够清楚,我想转换高级驱动页面中列出的所有格式,而不仅仅是转换为 PDF。DriveApp getAs文档指出,DriveApp 可以将大部分内容转换为 PDF 和图像。因此,重点是使用 Advanced Drive 而不是 DriveApp。
作为解决方法,我现在使用 OAuth2 库和直接 Drive API。
// ---------------------------------------------------------------------------------------------------
function testConvert() {
var file = {
id: "1cpAZp...I7ejZW1idk", // Google Sheet
folderId: "0B_...Z1k", // test folder
}
convert(file, "pdf", "application/pdf");
convert(file, "csv", "text/csv");
convert(file, "xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
}
// ---------------------------------------------------------------------------------------------------
function convert(file, extension, targetMimeType) {
var exportDetails = {
targetMimeType: targetMimeType || "application/pdf",
extension: extension || "pdf",
postfix: "CONVERTED_FROM_GOOGLE_DOC",
}
// http://stackoverflow.com/questions/42887569/using-google-apps-script-how-to-convert-export-a-drive-file
// https://code.google.com/p/google-apps-script-issues/issues/detail?id=6573
// var blob = Drive.Files.export(file.id, exportDetails.targetMimeType, {alt: "media"})
/*
results in error 200 + content of converted file
documentation https://developers.google.com/drive/v2/reference/files/export
1) does not mention the {alt:"media"} (and without it the error is: "export requires alt=media to download the exported content")
2) states the output is a Files resource (not a blob)
*/
// using alternative: OAuth2 + public API
// https://github.com/googlesamples/apps-script-oauth2
var driveService = getDriveService();
if (!driveService.hasAccess()) {
showSidebar();
return;
}
var accessToken = driveService.getAccessToken();
var url = "https://www.googleapis.com/drive/v2/files/"+file.id+"/export?mimeType="+ exportDetails.targetMimeType
var blob = UrlFetchApp.fetch(url, {
headers: {
Authorization: 'Bearer ' + driveService.getAccessToken()
}
});
var resource = {
title: DriveApp.getFileById(file.id).getName() +" "+ exportDetails.postfix +"."+ exportDetails.extension,
description: "This is a converted document",
mimeType: exportDetails.targetMimeType,
parents: [{
"kind": "drive#parentReference",
"id": file.folderId,
//"selfLink": string,
//"parentLink": string,
"isRoot": false
}],
}
try {
var convertedFile = Drive.Files.insert(resource, blob);
Logger.log('ID: %s, File size (bytes): %s', convertedFile.id, convertedFile.fileSize);
} catch(e) {
Logger.log(e)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8613 次 |
| 最近记录: |