使用Google Script Editior将Google Doc转换为PDF [英] Convert Google Doc to PDF using Google Script Editior

查看:151
本文介绍了使用Google Script Editior将Google Doc转换为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been trying to convert a Google Docs file to a PDF file without having to use the download option. Below is the script I have used in the Script Editor but it doesn't seem to work. I think the error is after the IF statement.

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.');
  }
}


推荐答案

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

It is failing because the folder is not defined. If you replace it with DriveApp, the PDF will be created in the root folder and your function would work. You can also show the full URL in the message box.

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.');
  }
}

这篇关于使用Google Script Editior将Google Doc转换为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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