9

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

4

2 回答 2

8

这应该工作

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);

}
于 2016-12-21T19:31:39.183 回答
2

Quoting from this item on the issue tracker...

You can convert a Google Document to docx today using the Drive Advanced Service:

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

Here is a small example:

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();
}
于 2015-03-19T18:16:29.193 回答