使用Google Script将Google Doc转换为Docx

时间:2013-03-26 11:58:26

标签: ms-word google-apps-script google-api google-drive-api

是否可以使用Google Script以编程方式将Google文档转换为Word docx文件?

2 个答案:

答案 0 :(得分:2)

这应该有效

function myFunction() {

  var token = ScriptApp.getOAuthToken();

  //Make sure to replace the correct file Id
  // Fetch the docx blob
  var blb = UrlFetchApp.fetch('https://docs.google.com/feeds/download/documents/export/Export?id=<ID_of_Google_Document>&exportFormat=docx', 
                              {
                                headers : {
                                  Authorization : 'Bearer '+token
                                }
                              }).getBlob();

  //Create file in Google Drive
  var file = DriveApp.createFile(blb).setName('<name_of_exported_file>.docx');

  //Put the file in a drive folder
  DriveApp.getFolderById('<FOLDER_ID>').addFile(file);

}

答案 1 :(得分:0)

在问题跟踪器上从this item引用...

  

您现在可以使用云端硬盘将Google文档转换为docx   高级服务:

     

https://developers.google.com/apps-script/advanced/drive

     

这是一个小例子:

function convertToDocx(documentId) {
  var file = Drive.Files.get(documentId);
  var url = file.exportLinks['application/vnd.openxmlformats-officedocument.wordprocessingml.document'];
  var oauthToken = ScriptApp.getOAuthToken();
  var response = UrlFetchApp.fetch(url, {
    headers: {
      'Authorization': 'Bearer ' + oauthToken
    }
  });
  return response.getBlob();
}