Google Sheets to PDF - 如何以编程方式修剪打印输出底部的空白行?

问题描述 投票:0回答:1

我正在使用以下脚本,该脚本非常适合将特定 Google 表格的 PDF 副本保存到我的 Google 云端硬盘。它专门按计划保存现有文件的覆盖部分,但不会删除然后替换该文件,以维护已给出的共享链接。

function updatePDF2() {
  var folderId = 'google_drive_folder_id';
  var pdfName = 'filename.pdf';

  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = spreadsheet.getSheetByName('PDF_AVAIL_TOOL');
  var folder = DriveApp.getFolderById(folderId);
  var files = folder.getFilesByName(pdfName);
  var existingFileId = files.hasNext() ? files.next().getId() : Drive.Files.create({ name: pdfName, mimeType: MimeType.PDF, parents: [folderId] }).id;
  var url = 'https://docs.google.com/spreadsheets/d/' + spreadsheet.getId() + '/export?exportFormat=pdf&format=pdf';
  var options = '&size=letter' +
    '&portrait=true' +
    '&scale=2' +
    '&top_margin=0.25' + '&bottom_margin=0.25' + '&left_margin=0.25' + '&right_margin=0.25' +
    '&sheetnames=false&printtitle=false&pagenumbers=true&gridlines=false&fzr=true' +
    '&gid=' + sheet.getSheetId(); // Using the specific Sheet ID for exporting
  var token = ScriptApp.getOAuthToken();
  var headers = {
    'Authorization': 'Bearer ' + token,
    'muteHttpExceptions': true
  };
  var response = UrlFetchApp.fetch(url + options, { headers: headers });
  if (response.getResponseCode() == 200) {
    var blob = response.getBlob().setName(pdfName);
    Drive.Files.update({}, existingFileId, blob);
    DriveApp.getFileById(existingFileId).setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
    Logger.log('PDF updated successfully and permissions are set.');
  } else {
    Logger.log('Failed to fetch the PDF: ' + response.getContentText());
  }
}

我的问题是,此表末尾有很多空白行,我不希望/不需要生成 PDF 文件。该列表的长度经常变化,因此我需要动态调整。如果我在纸张本身上放置一个过滤器以删除空白,则在应用过滤器时打印将是完美的,但如果列表变短,底部将出现空白,如果列表变长,这些附加行会更糟不会显示在打印输出上。

如何调整脚本,以便在导出 PDF 之前应用过滤器以仅包含工作表 B 列中的非空白值?

google-sheets google-apps-script google-drive-api google-sheets-api
1个回答
0
投票

关于

How can I adapt the script so that it will apply a filter to only include non blank values in Column B of the sheet prior to exporting the PDF?
,当你的展示脚本修改后,下面的修改如何?

修改后的脚本:

function updatePDF2() {
  var folderId = 'google_drive_folder_id';
  var pdfName = 'filename.pdf';

  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = spreadsheet.getSheetByName('PDF_AVAIL_TOOL');

  // --- I added the below script.
  var maxRow = sheet.getMaxRows();
  sheet.showRows(1, maxRow);
  var values = sheet.getRange("B1:B" + maxRow).getDisplayValues();
  values.forEach(([b], i) => {
    if (!b) {
      sheet.hideRows(i + 1);
    }
  });
  SpreadsheetApp.flush();
  // ---

  var folder = DriveApp.getFolderById(folderId);
  var files = folder.getFilesByName(pdfName);
  var existingFileId = files.hasNext() ? files.next().getId() : Drive.Files.create({ name: pdfName, mimeType: MimeType.PDF, parents: [folderId] }).id;
  var url = 'https://docs.google.com/spreadsheets/d/' + spreadsheet.getId() + '/export?exportFormat=pdf&format=pdf';
  var options = '&size=letter' +
    '&portrait=true' +
    '&scale=2' +
    '&top_margin=0.25' + '&bottom_margin=0.25' + '&left_margin=0.25' + '&right_margin=0.25' +
    '&sheetnames=false&printtitle=false&pagenumbers=true&gridlines=false&fzr=true' +
    '&gid=' + sheet.getSheetId(); // Using the specific Sheet ID for exporting
  var token = ScriptApp.getOAuthToken();
  var headers = {
    'Authorization': 'Bearer ' + token,
    'muteHttpExceptions': true
  };
  var response = UrlFetchApp.fetch(url + options, { headers: headers });
  if (response.getResponseCode() == 200) {
    var blob = response.getBlob().setName(pdfName);
    Drive.Files.update({}, existingFileId, blob);
    DriveApp.getFileById(existingFileId).setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
    Logger.log('PDF updated successfully and permissions are set.');
  } else {
    Logger.log('Failed to fetch the PDF: ' + response.getContentText());
  }

  sheet.showRows(1, maxRow); // Added
}
  • 通过此修改,导出的 PDF 文件中仅显示“B”列具有值的行。
© www.soinside.com 2019 - 2024. All rights reserved.