【发布时间】:2022-04-26 22:10:40
【问题描述】:
我有轻微的一般编程知识,但对谷歌应用脚本基本一无所知。
我正在尝试在 google 表格中创建动态分页符,或者找到另一种方法在打印时将某些行组合在一起。
在我的数据表上,我有 100 行信息,并且在每一行中,数据的长度可能会有很大差异(从单个数字到多段文本)。我创建了第二张表,它既过滤了我想要的信息,又以一种视觉上吸引人的方式显示它,从每一行中获取原始数据并将其解析为总共 8 行(7 行包含信息,一个空白用于在视觉上分隔一个来自下一个的信息块)每一个原件。问题是数据的长度不同意味着每次更改过滤器时我都必须手动移动分页符。
Here is a blank section of the second sheet for reference.
我希望能够在一个页面上打印尽可能多的 8 行分组,但不想将一个组拆分到下一页。
老实说,我不确定如何开始,但我认为我可以使用空白行以某种方式触发分页符。任何帮助将不胜感激!
更新
我已经能够编写一些基本代码来(大部分)完成我想要的。然而,我能说的最好的一点是 getRowHeight() 不适用于我的包装文本,因为当我有任何空数据集时它会正确格式化,但不是这样。
有人可以确认,并告诉我我错过了什么吗?
function dynamicPageBreaks() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var curRow = 2; //start on Row 2 (1 contains the filter selection)
var curTotPix = ss.getRowHeight(curRow);
var pgPix = 1030;
//loop until end of sheet
var endRow = ss.getLastRow();
do {
//find row that goes past page break
do {
curRow++;
curTotPix = curTotPix + ss.getRowHeight(curRow);
} while (curTotPix <= pgPix);
//get value of cell in column B of that row
var curCell = sheet.getRange(curRow,2).getValue();
//back up until we find an empty row
if (curCell == "") {
break;
} else do {
curTotPix = curTotPix - ss.getRowHeight(curRow);
curRow = curRow - 1;
curCell = sheet.getRange(curRow,2).getValue();
} while (curCell != "");
//expand empty row to match necessary pixels
var addHeight = pgPix - curTotPix;
ss.setRowHeight(curRow - 1, ss.getRowHeight(curRow) + addHeight);
//reset for next iteration
curTotPix = ss.getRowHeight(curRow);
} while (curRow < endRow);
}
【问题讨论】:
-
对新问题提出新问题。不要更新您的旧问题以反映新问题。见How to Ask
标签: google-apps-script google-sheets