javascript - 仅生成一张电子表格的 PDF

标签 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);
}

最佳答案

这个问题或多或少已经得到解决here

简而言之,您可以修改Url queryString中的导出参数来获取特定的sheet。然后将 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 += url_ext + sheet.getSheetId()+ "%EE%B8%80"+sheet2.getSheetId()+ "%EE%B8%80"+sheet3.getSheetId()
   var response = UrlFetchApp.fetch(url, {
      headers: {
        'Authorization': 'Bearer ' +  token
      }
    });

关于javascript - 仅生成一张电子表格的 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49197358/

相关文章:

javascript - array.slice() 如果我从 0 以外的任何地方开始,它就无法运行

javascript - 将 JavaScript 中的数学语句中的数字更改为带分隔符的数字

javascript promise 失败了

javascript - Ajax 添加选项以选择每个 json 数组

pdf - 使用pdftk和Ghostscript进行PDF比较

pdf - 使用 Ghostscript 生成缩略图可旋转我的设备尺寸定义以实现横向 pdf 页面

google-chrome - 通过 REST 接口(interface)托管的 PDF 在 Chrome 88+ 中返回 ERR_BLOCKED_BY_CLIENT

javascript - 使用 Google 表格中的数据在 Google Apps 脚本中自动完成

javascript - 如何在谷歌表格中获取更新的单元格值

javascript - 您无权使用 copyTo