【问题标题】:Create a new Word document containing the selected content of the Google document创建一个包含所选 Google 文档内容的新 Word 文档
【发布时间】:2022-01-17 18:09:52
【问题描述】:

我能够创建包含 Google 文档的全部内容的 Word 文档(参考 link)。 我需要使用 Google 文档的选定内容(仅限选择)在驱动器中创建一个新的 Word 文档。有没有办法做到这一点? 我认为如果有一种方法可以获取用于选择 Google 文档内容的 blob,那么我可以使用该 blob 来实现。我在网上浏览过,但没有找到任何支持信息。

谷歌文档转word文档代码:

function convertGoogleDocToMicrosoftWordDoc(docId) {
  if (docId == null) throw new Error("No file ID.");
  var file = DriveApp.getFileById(docId);
  var mime = file.getMimeType();
  var format = "";
  var ext = "";
  switch (mime) {
    case "application/vnd.google-apps.document":
      format = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
      ext = ".docx";
      break;
    default:
      return null;
  }

  var url = "https://www.googleapis.com/drive/v3/files/" + docId + "/export?mimeType="+ format;
  var blob = UrlFetchApp.fetch(url, {
    method: "get",
    headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
    muteHttpExceptions: true
  }).getBlob();
  var filename = file.getName();

  var id = DriveApp.createFile(blob).setName(~filename.indexOf(ext) ? filename : filename + ext).getId();
  return id;
};

【问题讨论】:

    标签: google-apps-script google-docs google-docs-api


    【解决方案1】:

    一个可能的解决方案是:

    1. 将您想要的选择从原始文档复制到临时文档。
    2. 将新文档的ID传递给函数convertGoogleDocToMicrosoftWordDoc()
    3. 删除临时文档
    Code.gs
    function convertGoogleDocToMicrosoftWordDoc(docId) {
      if (docId == null) throw new Error("No file ID.");
      var file = DriveApp.getFileById(docId);
      var mime = file.getMimeType();
      var format = "";
      var ext = "";
      switch (mime) {
        case "application/vnd.google-apps.document":
          format = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
          ext = ".docx";
          break;
        default:
          return null;
      }
    
      var url = "https://www.googleapis.com/drive/v3/files/" + docId + "/export?mimeType=" + format;
      var blob = UrlFetchApp.fetch(url, {
        method: "get",
        headers: { "Authorization": "Bearer " + ScriptApp.getOAuthToken() },
        muteHttpExceptions: true
      }).getBlob();
      var filename = file.getName();
    
      var id = DriveApp.createFile(blob).setName(~filename.indexOf(ext) ? filename : filename + ext).getId();
      return id;
    };
    
    const convertSelection = () => {
      let doc = DocumentApp.openById(ID_TO_COPY)
      let paragraph = doc.getBody().getParagraphs()
      // Copied the second paragraph
      let second_paragraph_text = paragraph[2].getText()
      // Create a new document and copy the content
      let new_doc = DocumentApp.create('temp_to_docx')
      new_doc.getBody().setText(second_paragraph_text)
      let tmp_id = new_doc.getId()
      // Pass the new Document to the function
      let new_id = convertGoogleDocToMicrosoftWordDoc(tmp_id)
      // Remove the temporal document
      DriveApp.removeFile(tmp_id)
      Logger.log(new_id)
    }
    
    function getSelection() {
      let doc = DocumentApp.openById(ID_TO_COPY)
      let paragraph = doc.getBody().getParagraphs()
      // Copied the second paragraph
      let second_paragraph_text = paragraph[2].getText()
      // Create a new document and copy the content
      let new_doc = DocumentApp.create('temp_to_docx')
      new_doc.getBody().setText(second_paragraph_text)
      let tmp_id = new_doc.getId()
      // Pass the new Document to the function
      let new_id =  convertGoogleDocToMicrosoftWordDoc(tmp_id)
      // Remove the temporal document
      DriveApp.removeFile(tmp_id)
      Logger.log(new_id)
    }
    
    文档

    【讨论】:

      猜你喜欢
      • 2016-03-16
      • 2016-06-28
      • 2018-09-03
      • 1970-01-01
      • 1970-01-01
      • 2022-10-04
      • 2016-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多