使用Google Apps脚本:如何转换/导出云端硬盘文件? [英] using Google Apps Script: how to convert/export a Drive file?

查看:106
本文介绍了使用Google Apps脚本:如何转换/导出云端硬盘文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Google Apps脚本将本地Google电子表格/文档/图形/演示文稿文件导出为同一文件夹中的另一种格式. 我已启用高级驱动器服务,并查看了在您的应用程序中打开和转换Google文档.

I would like to export a native Google spreadsheet/document/drawing/presentation file to another format, in that same folder, using Google Apps Script. I have enabled Advanced Drive Service, and looked at the instructions to Open and Convert Google Docs in your app.

希望我可以使用导出来获取文件,然后我可以使用

Was hoping I could use Export to get a File which I could then save/rename using 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

更新:将alt = media添加到呼叫(var newFile = Drive.Files.export(file.fileId, file.targetMimetype, {alt: "media"}))时,脚本将退出,错误代码为200,并在错误消息中显示PDF内容.类似于问题6573 .

Update: when adding alt=media to the call (var newFile = Drive.Files.export(file.fileId, file.targetMimetype, {alt: "media"})), then the script exits with error code 200 and shows the PDF content in the error message. Similar to issue 6573.

更新:这可能还不够清楚,我想将页,而不仅仅是PDF.并且 DriveApp getAs 文档指出,DriveApp可以大部分转换为PDF和图像.因此,重点是使用Advanced Drive而不是DriveApp.

Update: this may not have been clear enough, I want to convert from/to all of the formats listed in the Advanced Drive page, not just to PDF. And the DriveApp getAs documentation states that DriveApp can convert mostly to PDF and images. Hence the focus on using Advanced Drive instead of DriveApp.

推荐答案

作为一种解决方法,我现在使用OAuth2库和直接Drive API.

As a workaround I now use the OAuth2 library and the direct 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)
  }
}

这篇关于使用Google Apps脚本:如何转换/导出云端硬盘文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆