是否可以使用Apps脚本在Google Spreadsheet中添加分页符? [英] Is it possible to add page breaks into Google Spreadsheet using Apps Script?

查看:105
本文介绍了是否可以使用Apps脚本在Google Spreadsheet中添加分页符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Apps Script在导出到PDF之前以编程方式在Google Spreadsheet中设置分页符

I would like to programmatically set page breaks in my Google Spreadsheet before exporting to PDF, using Apps Script

应该可以在打印电子表格时手动设置分页符(

It should be possible as you can manually set the page breaks when you print the Spreadsheet (https://support.google.com/docs/answer/7663148?hl=en)

我发现Google文档中有可能( https://developers.google.com/apps-script/reference/document/page-break ),但他们没有在工作表中提及它.

I found that it's possible in Google Docs (https://developers.google.com/apps-script/reference/document/page-break) but they don't mention it on the sheet.

即使是"hack",有没有办法做到这一点?

Is there a way to do it, even if it's a "hack"?

推荐答案

我使用了解决方法.我通过更改行高以适合所需的纸张尺寸来调整页面尺寸(A4). 导出为pdf时,google会更改尺寸以适合宽度.我将列的大小加起来,然后相应地设置行高.编号是通过反复试验选择的.

I use a work around. I adjust the page size by altering the row height to fit the paper size I want (A4). When exporting to pdf google changes sizes to fit the width. I add up the size of the columns and then set the row heights accordingly. Numbers were chosen by trial and error.

var width = 0;
for(var z = 0; z < s4.getLastColumn(); z++){
  width += s4.getColumnWidth(z+1);
}
var a4PageHeightPixels = 1050 * width / 800;

因为我希望所有行的高度都相同,所以我将行高度设置为页面高度除以行数.确保最后一行为空白后,我调整了最后一行以解决舍入误差.

Because I wanted the rows all the same height I set the row height dividing my page height by the number of rows. Having ensured the last row was blank, I adjusted the last row to take up the rounding error.

  rowHeight= Math.floor(a4PageHeightPixels/(numDataRows ));
  lastRowHeight = a4PageHeightPixels - (numDataRows -1) * rowHeight;
  s4.setRowHeights(pageFirstRow,numDataRows-1,rowHeight);
  s4.setRowHeight(pageFirstRow+numDataRows-1,lastRowHeight);

(s4是我正在使用的工作表)但是,我希望大多数人只是在每页底部插入一个空白行并调整其大小以适合pdf纸张大小.

(s4 is the sheet I am using)However, I would expect most people would simply want to insert a blank line at the bottom of each page and adjust its size to fit the pdf paper size.

这篇关于是否可以使用Apps脚本在Google Spreadsheet中添加分页符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆