仅生成一张电子表格的PDF

时间:2018-03-09 15:46:15

标签: javascript pdf google-apps-script google-sheets pdf-generation

我需要一个只用一张电子表格创建PDF的脚本。 我目前有一个生成PDF的脚本,但它与整个文件一起使用。 我无法将值复制到另一个文件,因为我需要导出的工作表是包含从另一个工作表中提取的数据的图形。 你可以帮帮我吗?谢谢。

function myFunction() {
  var archivo = SpreadsheetApp.getActive();
  var hoja = archivo.getSheetByName("Graficos 2");
  var id = archivo.getId();
  var archivoId = DriveApp.getFileById(id);
  var archivoBlob = archivoId.getBlob();
  var carpetaId = archivoId.getParents();
  var contenidoPDF = archivo.getAs(MimeType.PDF);
  carpetaId.next().createFile(contenidoPDF);
}

1 个答案:

答案 0 :(得分:3)

此问题已或多或少得到解决here

简而言之,您可以修改Url queryString中的导出参数以获取特定工作表。然后将blob转换为pdf。

以下是相同的代码,基本代码是从here

获得的
function emailSpreadsheetAsPDF() {

  // Send the PDF of the spreadsheet to this email address
  var email = "Your Email Id"; 

  // Get the currently active spreadsheet URL (link)
  // Or use SpreadsheetApp.openByUrl("<<SPREADSHEET URL>>");
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("<Your Sheet name>")

  // Subject of email message
  var subject = "PDF generated from sheet " + sheet.getName(); 

  // Email Body can  be HTML too with your logo image - see ctrlq.org/html-mail
  var body = "Install the <a href='http://www.labnol.org/email-sheet'>Email Spreadsheet add-on</a> for one-click conversion.";

  // Base URL
  var url = "https://docs.google.com/spreadsheets/d/SS_ID/export?".replace("SS_ID", ss.getId());

  /* Specify PDF export parameters
  From: https://code.google.com/p/google-apps-script-issues/issues/detail?id=3579
  */

  var url_ext = 'exportFormat=pdf&format=pdf'        // export as pdf / csv / xls / xlsx
  + '&size=letter'                       // paper size legal / letter / A4
  + '&portrait=false'                    // orientation, false for landscape
  + '&fitw=true&source=labnol'           // fit to page width, false for actual size
  + '&sheetnames=false&printtitle=false' // hide optional headers and footers
  + '&pagenumbers=false&gridlines=false' // hide page numbers and gridlines
  + '&fzr=false'                         // do not repeat row headers (frozen rows) on each page
  + '&gid=';                             // the sheet's Id

  var token = ScriptApp.getOAuthToken();


  //make an empty array to hold your fetched blobs  
  var blobs;


    // Convert your specific sheet to blob
    var response = UrlFetchApp.fetch(url + url_ext + sheet.getSheetId(), {
      headers: {
        'Authorization': 'Bearer ' +  token
      }
    });

    //convert the response to a blob and store in our array
    blobs = response.getBlob().setName(sheet.getName() + '.pdf');


  // Define the scope
  Logger.log("Storage Space used: " + DriveApp.getStorageUsed());

  // If allowed to send emails, send the email with the PDF attachment
  if (MailApp.getRemainingDailyQuota() > 0) 
    GmailApp.sendEmail(email, subject, body, {
      htmlBody: body,
      attachments:[blobs]     
    });  
}

修改:合并特定工作表并将其转换为和pdf。

首先,您必须获取要合并到pdf

中的所有工作表的工作表对象
var sheet2 = ss.getSheetByName("<Your Sheet name>")
var sheet3 = ss.getSheetByName("<Your Sheet name>")

然后将每张工作表的sheetId传递给%EE%B8%80分隔的网址查询,就像这样

   url += url_ext + sheet.getSheetId()+ "%EE%B8%80"+sheet2.getSheetId()+ "%EE%B8%80"+sheet3.getSheetId()
   var response = UrlFetchApp.fetch(url, {
      headers: {
        'Authorization': 'Bearer ' +  token
      }
    });