我需要創建一個復制行 4:8 的腳本,然后無論作業表的最后一行是什么,都將這些行粘貼進去。
注意- 為了保持檔案較小,作業表故意洗掉了大部分行。最后一行可能并不總是相同的數字,因此即使其中沒??有資料,腳本也必須知道最后一行是哪一行。
例子
如果最后一行是 20,則在執行腳本(我將分配給一個按鈕)時,將添加 5 個額外的行(這將來自行 4:8),將作業表擴展到總共 25 行。
這些行中有特定的格式,所以它不像添加 5 行那么簡單,它們必須是 4:8 行的副本。
謝謝。
uj5u.com熱心網友回復:
將行復制到底部,包括格式
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 ));
}
uj5u.com熱心網友回復:
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://www.uj5u.com/ruanti/447625.html
