简体   繁体   中英

Script to copy cell with formatting in Google Sheets

I'm trying to copy-paste a range of cells with formatting using the CopyTo function.

var rangeToCopy = sheet1.getRange(2,1,1,6);

for(i=2; i<=500; i++)
{
rangeToCopy.copyTo(sheet2.getRange(i,1));
}

Basically, I need to paste the same data to multiple rows. The for loop takes too much time to execute, so I was wondering if there is a faster way to specify the number of rows to paste the same data to?

You don't need to loop through all rows. You can set a destination range comprising all rows you want to copy the range to:

rangeToCopy.copyTo(sheet2.getRange(2, 1, 499));

That's the same behaviour than in Sheets UI. The source and the target ranges don't need to have the same number of rows. The copied values and format will automatically expand to the rest of your target range.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM