mutkan
mutkan

Reputation: 954

Calling copyValuesToRange method twice

I want to copy some columns to another sheet. When i call the copyValuesToRange method twice, one is working, but the other is not.

    function myFunction() {
  var s = SpreadsheetApp.getActiveSpreadsheet();
  var target = SpreadsheetApp.openById("f3y53y54y45h45jh454");

  var target_sheet = target.getSheetByName("Sheet2");
  var sheet = s.getSheetByName("Sheet1");

  var sheet_last_row = sheet.getLastRow() + 1;

  var source_range = sheet.getRange("B1:H"+sheet_last_row);
  var source_range2 = sheet.getRange("K1:P"+sheet_last_row);

  var sWidth=source_range.getWidth() + 1;
  var sHeight=source_range.getHeight() + 1;

  var sWidth2=source_range2.getWidth() + 1;
  var sHeight2=source_range2.getHeight() + 1;

var last_row=target_sheet.getLastRow();

source_range.copyValuesToRange(target_sheet , 1, sWidth, last_row + 1, last_row + sHeight );
source_range2.copyValuesToRange(target_sheet , 8, sWidth2, last_row + 1, last_row + sHeight2 );

}

How can i fix this problem or is there another solution for copying cells the another sheet.

Upvotes: 0

Views: 750

Answers (1)

Guy
Guy

Reputation: 12929

Shift the width of the second range from the starting point to (the end point of the first range+1) by sWidth2, as it should be the column number of the target range right border:

source_range2.copyValuesToRange(target_sheet , sWidth1+1, sWidth1+1+sWidth2, last_row + 1, last_row + sHeight2 );

Upvotes: 1

Related Questions