我需要创建一个脚本来复制4:8行,然后不管工作表的最后一行是什么,将这些行粘贴到其中。
注意事项-为了保持文件小,工作表中的大多数行都是故意删除的。最后一行可能并不总是相同的数字,所以即使没有数据,脚本也必须知道最后一行是哪一行。
示例
如果最后一行为20,则在执行脚本(我将分配给一个按钮)时,将添加5行(可能来自4:8行),将工作表扩展到总共25行。
这些行中有特定的格式,所以它不像添加5个额外行那么简单,它们必须是4:8行的副本。
谢谢。
发布于 2022-03-21 03:06:54
将行复制到底部,包括格式
function copyrows() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
const rg = sh.getRange("4:8");//select line 4 through 8
const drg = sh.getRange(sh.getLastRow() + 2 , 1);//Select destination range. You only have to specify the upper left corner of the range
rg.copyTo(drg);
}演示:

修改为包含行高
function copyrows() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
const rg = sh.getRange("4:8");//select line 4 through 8
let hA = [...Array.from(new Array(rg.getHeight()).keys(),x => sh.getRowHeight(x + 4))]
let lr = sh.getLastRow();
const drg = sh.getRange(lr + 2 , 1);//Select destination range. You only have to specify the upper left corner of the range
rg.copyTo(drg);
hA.forEach((h,i) => sh.setRowHeight(lr + 1 + i, h ));
}发布于 2022-03-21 01:58:15
function copyrow_4_8(){
const ss = SpreadsheetApp.getActive().getActiveSheet();
//replace the columns number with your acutal needed columns
const range = ss.getRange(4,1,5,4);
const range_value = range.getValues();
const dest_range = ss.getRange(ss.getLastRow()+1,1,range_value.length,range_value[0].length);
//get grid id for the dest range
const dest_range_Gid = dest_range.getGridId();
//copy format to the dest range
range.copyFormatToRange(dest_range_Gid,1,4,ss.getLastRow()+1,ss.getLastRow()+range_value.length+1);
//set the values to dest range.
dest_range.setValues(range_value);
}https://stackoverflow.com/questions/71551915
复制相似问题