简体   繁体   English

Google 脚本 - 将文档导出到 PDF

[英]Google Script - Export a document to PDF

I'm pretty new to programming and don't really know Javascript.我对编程很陌生,并不真正了解 Javascript。 Recently, to help out my mother with her business, I've came up with the script below.最近,为了帮妈妈做生意,我想出了下面的脚本。 Basically it takes information from a sheet (the sheet is filled with answers from Google Forms), copy a template document and then replace some fields in the copied document with those informations.基本上,它从工作表中获取信息(工作表中填充了来自 Google 表单的答案),复制模板文档,然后将复制文档中的某些字段替换为这些信息。

Now what I'm trying to do is to export the filled document to PDF, can someone help?现在我要做的是将填充的文档导出到 PDF,有人可以帮忙吗?

Here's the actual script:这是实际的脚本:

function autoFillGoogleDocFromForm(e) {
//e.values is an array of form values

var timestamp = e.values[0];
  
//client info
var nome = e.values[1];
var cpf = e.values[2];
var rg = e.values[3];
var rua = e.values[4];
var numero = e.values[5];
var bairro = e.values[6];
var cidade = e.values[7];
var estado = e.values[8];
var cep = e.values[9];
var celular = e.values[10];
  
  

//seller info
  var nome_vendedor = e.values[11];
  var cpf_vendedor = e.values[12];
  
  //template is the template file, and you get it by ID
  var template = DriveApp.getFileById('1VB5u4OrqIQO8P6scj-rzkjtRlZ8NI-ZKd9xGKfnEqBA'); 
  
  //acess main folder
  var mainfolder = DriveApp.getFolderById('1G6g2VyrvpKpqSvLei3_jefQMNsGb-be0');
  
  //create a new folder
  var newFolderId = mainfolder.createFolder(nome + ' ' + cpf).getId();
  
  //get new folder Id
  var id = DriveApp.getFolderById(newFolderId);
  
  //copy the template file to the new folder
  var copy = template.makeCopy(nome + ' ' + cpf, id);
  
  //open the document by it's Id
  var document = DocumentApp.openById(copy.getId()); 
  
  //get to the doc body for the replace
  var body = document.getBody();  
  
  
  ////////////
  ////////////
  ////////////
  ////////////
  ////////////
  //ReplaceText methods
  ////////////
  ////////////
  ////////////
  ////////////  
  
  
  //client info
  body.replaceText('{{nome}}', nome); 
  body.replaceText('{{cpf}}', cpf);  
  body.replaceText('{{rg}}', rg);
  body.replaceText('{{rua}}', rua); 
  body.replaceText('{{numero}}', numero);
  body.replaceText('{{bairro}}', bairro); 
  body.replaceText('{{cidade}}', cidade); 
  body.replaceText('{{estado}}', estado); 
  body.replaceText('{{cep}}', cep); 
  body.replaceText('{{celular}}', celular); 
  
  
  //seller info
  body.replaceText('{{nome_vendedor}}', nome_vendedor);
  body.replaceText('{{cpf_vendedor}}', cpf_vendedor);
  
    //save and close the document
    document.saveAndClose(); 
  
    }

    

The easiest way to export a Goolge Document to pdf is using the UrlFetchApp将 Goolge 文档导出到 pdf 的最简单方法是使用UrlFetchApp

You need the你需要

  • basic export url基本出口 url
  • the file id文件id
  • specofy export parameters if and as required根据需要指定导出参数
  • An access token that you can obtain with the ScriptApp您可以通过ScriptApp获得的访问令牌

Sample snippet based on you code before where you already have the document id:在您已经拥有文档 ID 之前基于您的代码的示例片段:


  var url = "https://docs.google.com/document/d/"+id+"/export?";
  
  var url_ext = 'exportFormat=pdf&format=pdf'        // export as pdf / csv / xls / xlsx
  + '&size=letter'                                   // paper size legal / letter / A4
  + '&portrait=true'                                // orientation, false for landscape
  +'&top_margin=0.50'
  +'&bottom_margin=0.50' 
  +'&left_margin=0.50' 
  +'&right_margin=0.50'
  // other parameters if you need
  /*
  + '&fitw=true&source=labnol'                       // fit to page width, false for actual size
  + '&sheetnames=false&printtitle=false'             // hide optional headers and footers
  + '&pagenumbers=false'                               // hide page numbers 
  + '&fzr=false'                                     // do not repeat row headers (frozen rows) on each page
  + '&gid=';                                         // the sheet's Id
  */
  
  var response = UrlFetchApp.fetch(url + url_ext, 
                                   {
                                   headers: {
                                   'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()
                                   },
                                   muteHttpExceptions:true
                                   });
  DriveApp.createFile(response.getBlob().setName("myPdf"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM