【问题标题】:Convert Google Doc to PDF using Google Script Editior使用 Google Script Editor 将 Google Doc 转换为 PDF
【发布时间】:2015-02-04 03:25:38
【问题描述】:

我一直在尝试将 Google Docs 文件转换为 PDF 文件,而无需使用下载选项。下面是我在脚本编辑器中使用的脚本,但它似乎不起作用。我认为错误是在 IF 语句之后。

function convertPDF() {
  doc = DocumentApp.getActiveDocument();
  docblob = DocumentApp.getActiveDocument().getAs('application/pdf');
  var result = DocumentApp.getUi().alert(
      'Save As PDF?',
      'Save current document (Name:'+doc.getName()+') as PDF',
      DocumentApp.getUi().ButtonSet.YES_NO);
  if (result == DocumentApp.getUi().Button.YES) {
    docblob.setName(doc.getName())
    folder.createFile(docblob);
    DocumentApp.getUi().alert('Your PDF has been converted to a PDF file.');
  } else {
    DocumentApp.getUi().alert('Request has been cancelled.');
  }
}

【问题讨论】:

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


    【解决方案1】:

    因为未定义文件夹而失败。如果您将其替换为 DriveApp,PDF 将在根文件夹中创建,您的功能将起作用。您还可以在消息框中显示完整的 URL。

    function convertPDF() {
      doc = DocumentApp.getActiveDocument();
      var ui = DocumentApp.getUi();
      var result = ui.alert(
          'Save As PDF?',
          'Save current document (Name:'+doc.getName()+'.pdf) as PDF',
          ui.ButtonSet.YES_NO);
      if (result == ui.Button.YES) {
        docblob = DocumentApp.getActiveDocument().getAs('application/pdf');
        /* Add the PDF extension */
        docblob.setName(doc.getName() + ".pdf");
        var file = DriveApp.createFile(docblob);
        ui.alert('Your PDF file is available at ' + file.getUrl());
      } else {
        ui.alert('Request has been cancelled.');
      }
    }
    

    【讨论】:

    • 感谢您的回复并解释我的错误。另一个问题是保存的文件是否可以保存在与您要转换的文件相同的文件夹中?
    【解决方案2】:

    将 PDF 保存在原始目录中:

    function convertPDF() {
      doc = DocumentApp.getActiveDocument();
      // ADDED
      var docId = doc.getId();
      var docFolder = DriveApp.getFileById(docId).getParents().next().getId();
      // ADDED
      var ui = DocumentApp.getUi();
      var result = ui.alert(
          'Save As PDF?',
          'Save current document (Name:'+doc.getName()+'.pdf) as PDF',
          ui.ButtonSet.YES_NO);
      if (result == ui.Button.YES) {
        docblob = DocumentApp.getActiveDocument().getAs('application/pdf');
        /* Add the PDF extension */
        docblob.setName(doc.getName() + ".pdf");
        var file = DriveApp.createFile(docblob);
        // ADDED
        var fileId = file.getId();
        moveFileId(fileId, docFolder);
        // ADDED
        ui.alert('Your PDF file is available at ' + file.getUrl());
      } else {
        ui.alert('Request has been cancelled.');
      }
    }
    

    并添加这个通用函数

    function moveFileId(fileId, toFolderId) {
       var file = DriveApp.getFileById(fileId);
       var source_folder = DriveApp.getFileById(fileId).getParents().next();
       var folder = DriveApp.getFolderById(toFolderId)
       folder.addFile(file);
       source_folder.removeFile(file);
    }
    

    【讨论】:

    • 我正在使用共享驱动器,但在这一行中出现错误:var file = DriveApp.createFile(docblob);
    • 非常有帮助的答案,我确实用了很多
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多