简体   繁体   中英

Need to copy both the format and values in another sheet to the next row that is blank

I need to copy this range A1 : BU17 to another sheet in the same spreadsheet in the next rows avaible. I tried to use this:

function Copy() {

 var sss = SpreadsheetApp.openById('x'); //replace with source ID
 var ss = sss.getSheetByName('Analysing'); //replace with source Sheet tab name
 var range = ss.getRange('A1:BU17'); //assign the range you want to copy
 var data = range.getValues();

 var tss = SpreadsheetApp.openById('x'); //replace with destination ID
 var ts = tss.getSheetByName('Archive'); //replace with destination Sheet tab name
 ts.getRange(ts.getLastRow()+1, 1, data.length, data[0].length).setValues(data);

}

I found this searching inside stackoverflow. It does copy my rows to the next one avaible but not the format too so is a total mess when i'm using it. Ofc "X" is the id i know. Thank you in advance if someone can help me.

How about this modification? Please think of this as one of several answers.

You can use copyTo() . But there are some limitations.

By reflecting them, your script can be modified as follows.

Flow :

  1. Retrieve a range which is a source.
  2. Retrieve a range which is a destination.
  3. Copy source sheet to destination sheet using copyTo() of Class Sheet.
  4. Copy the source range to the destination range using copyTo() of Class Range.
  5. Delete the sheet copied using copyTo() of Class Sheet.

Sample script :

function Copy() {
  var sss = SpreadsheetApp.openById('x'); //replace with source ID
  var ss = sss.getSheetByName('Analysing'); //replace with source Sheet tab name
  var srcrange = ss.getRange('A1:BU17'); //assign the range you want to copy

  var tss = SpreadsheetApp.openById('x'); //replace with destination ID
  var ts = tss.getSheetByName('Archive'); //replace with destination Sheet tab name
  var dstrange = ts.getRange(ts.getLastRow()+1, 1, srcrange.getHeight(), srcrange.getWidth());

  var dstSS = dstrange.getSheet().getParent();
  var copiedsheet = srcrange.getSheet().copyTo(dstSS);
  copiedsheet.getRange(srcrange.getA1Notation()).copyTo(dstrange);
  dstSS.deleteSheet(copiedsheet);
}

If I misunderstand your question, I'm sorry.

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