You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Sheets导出PDF右侧截断问题求助(已设横向布局)

Google Sheets导出PDF右侧内容截断的解决方法

问题描述

从Google Sheets导出PDF后,文档右侧内容全部被截断。已设置横向(Landscape)布局,但仍无法完整显示整个表格。

使用的原始代码

/**
 * Creates a PDF for the customer given sheet.
 * @param {string} ssId - Id of the Google Spreadsheet
 * @param {object} sheet - Sheet to be converted as PDF
 * @param {string} pdfName - File name of the PDF being created
 * @return {file object} PDF file as a blob
 */
function createPDF(ssId, sheet, pdfName) {
  const fr = 0, fc = 0, lc = 9, lr = 27;
  const url = "https://docs.google.com/spreadsheets/d/" + ssId + "/export" +
    "?format=pdf&" +
    "size=0&" +
    "fzr=true&" +
    "portrait=false&" +
    "fitw=true&" +
    "gridlines=false&" +
    "printtitle=false&" +
    "top_margin=0.5&" +
    "bottom_margin=0.25&" +
    "left_margin=0.5&" +
    "right_margin=0.5&" +
    "sheetnames=false&" +
    "pagenum=UNDEFINED&" +
    "attachment=true&" +
    "gid=" + sheet.getSheetId() + '&' +
    "r1=" + fr + "&c1=" + fc + "&r2=" + lr + "&c2=" + lc;

  const params = { method: "GET", headers: { "authorization": "Bearer " + ScriptApp.getOAuthToken() } };
  const blob = UrlFetchApp.fetch(url, params).getBlob().setName(pdfName + '.pdf');

  // Gets the folder in Drive where the PDFs are stored.
  // const folder = getFolderByName(OUTPUT_FOLDER_NAME);
   const folders = DriveApp.getFoldersByName('(P) HIVE EOS');

   if (folders.hasNext()) {
    const folder = folders.next();

    const pdfFile = folder.createFile(blob);
    return pdfFile;
  } else {
    throw new Error('Folder could not be found');
  }
}

问题分析与解决方案

核心问题点

  1. 硬编码打印范围:代码中lc=9(结束列)是固定值,若实际表格列数超过9,会直接截断右侧内容。
  2. 纸张尺寸与边距:默认用size=0(Letter纸张),配合0.5的左右边距,可能无法容纳较宽的表格。
  3. 适配逻辑:fitw=true虽能让内容适应宽度,但受限于纸张和边距,仍可能出现截断。

修改后的代码

/**
 * 创建指定工作表的PDF文件
 * @param {string} ssId - Google表格ID
 * @param {object} sheet - 要转换为PDF的工作表对象
 * @param {string} pdfName - PDF文件名
 * @return {file object} PDF文件对象
 */
function createPDF(ssId, sheet, pdfName) {
  // 动态获取表格实际范围,避免硬编码遗漏内容
  const fr = 0;
  const fc = 0;
  const lr = sheet.getLastRow() - 1; // URL参数行列从0开始计数,需减1
  const lc = sheet.getLastColumn() - 1;

  const url = "https://docs.google.com/spreadsheets/d/" + ssId + "/export" +
    "?format=pdf&" +
    "size=1&" + // 改用A4纸张(1=A4),宽幅表格可换3=A3
    "fzr=true&" +
    "portrait=false&" + // 保持横向布局
    "fitw=true&" + // 内容自适应页面宽度,仍截断可改为false
    "gridlines=false&" +
    "printtitle=false&" +
    "top_margin=0.25&" + // 减小边距,释放更多显示空间
    "bottom_margin=0.25&" +
    "left_margin=0.25&" +
    "right_margin=0.25&" +
    "sheetnames=false&" +
    "pagenum=UNDEFINED&" +
    "attachment=true&" +
    "gid=" + sheet.getSheetId() + '&' +
    "r1=" + fr + "&c1=" + fc + "&r2=" + lr + "&c2=" + lc;

  const params = { method: "GET", headers: { "authorization": "Bearer " + ScriptApp.getOAuthToken() } };
  const blob = UrlFetchApp.fetch(url, params).getBlob().setName(pdfName + '.pdf');

  const folders = DriveApp.getFoldersByName('(P) HIVE EOS');

  if (folders.hasNext()) {
    const folder = folders.next();
    const pdfFile = folder.createFile(blob);
    return pdfFile;
  } else {
    throw new Error('未找到指定文件夹');
  }
}

关键修改说明

  • 动态获取打印范围:用sheet.getLastRow()和sheet.getLastColumn()自动捕获表格的实际边界,确保所有内容都被包含。
  • 调整纸张尺寸:将size=0(Letter)改为size=1(A4),如果表格宽度超出A4范围,可替换为size=3(A3)。
  • 减小边距:把上下左右边距从0.5/0.25统一调整为0.25,减少留白区域,给表格更多显示空间。
  • 适配逻辑可选调整:若fitw=true仍无法完整显示,可改为fitw=false,此时内容按实际宽度打印,需配合大尺寸纸张使用。

内容的提问来源于stack exchange,提问作者Jorge Garcia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 19:47:01