Google Docs PDF 导出在末尾添加额外的空白页

Google Docs PDF Export Adds Extra Blank Pages at the End

我使用 google spreadsheet 创建发票并以 PDF 格式通过电子邮件发送,效果很好,我的 spreadsheet 是年度基准,所以每一个新的今年我创建了另一个,我从 2021 年开始的所有传播sheet 仍然工作正常,但是我为 2022 年创建的所有新传播当我尝试打印或导出 PDF 时,额外的空白页被添加到最后的文件。空白页的数量取决于 sheet 上的空白行数,但我以前没有这个问题,即使 sheet 有 1000 行,如果发票只有 1页 PDF/Print 只会生成 1 页,现在如果 sheet 有 1000 行,它会生成 19 个空白页。

有同样问题或解决方案的人吗?

Link 例如:Spreadsheet link

尝试打印或下载页面,您会看到生成的空白页。

function savePDF() {
    const ssh = SpreadsheetApp.getActiveSpreadsheet();
    const invoice = ssh.getSheetByName('invoice');

const request = {
    "method": "GET",
    "headers":{"Authorization": "Bearer 
    "+ScriptApp.getOAuthToken()},    
 }

 const key = ssh.getId();
 const bogus = DriveApp.getRootFolder();
 const fetch='https://docs.google.com/spreadsheets/d/'
 + key
 +'/export?format=pdf&gid=' 
 + invoice.getSheetId() 
 + 
'&size=letter&portrait=true
&printtitle=false
&pagenum=CENTER
&sheetnames=false
&gridlines=false
&top_margin=0.25
&bottom_margin=0.50&left_margin=0.25
&right_margin=0.25';

const name = "invoice.pdf";
let pdf = UrlFetchApp.fetch(fetch, request);
pdf = pdf.getBlob().setName(name);

const fold = "Folder ID goes here";
const folder = DriveApp.getFolderById(fold);
const file = folder.createFile(pdf);
}

问题是它默认打印整个 sheet。所以它包括所有那些空白行,从而添加空白 sheets.

解决方法是 CTRL + A 在 sheet 中,然后使用 选中的单元格

导出:

如您所见,总页数只有1页。

脚本:

我不确定为什么它现在不能正常工作,但可能有一些更改并默认导出整个 sheet。要绕过此问题,您需要将实际范围添加到 link。请参阅以下修改:

修改:

  1. 获取数据范围的A1表示法:
  const ssh = SpreadsheetApp.getActiveSpreadsheet();
  const invoice = ssh.getSheetByName('invoice');
  // add this line
  const range = invoice.getDataRange().getA1Notation();
  1. 将范围附加到 link:
  const fetch = 'https://docs.google.com/spreadsheets/d/' + key
    + '/export?format=pdf&gid=' + invoice.getSheetId()
    + '&size=letter'
    + '&portrait=true'
    + '&printtitle=false'
    + '&pagenum=CENTER'
    + '&sheetnames=false'
    + '&gridlines=false'
    + '&top_margin=0.25'
    + '&bottom_margin=0.50'
    + '&left_margin=0.25'
    + '&right_margin=0.25'
    // add range to link
    + '&range=' + range;

输出: