Home > Enterprise >  Google Apps Script - copy row & paste as values
Google Apps Script - copy row & paste as values

Time:10-22

I am trying to copy one range to another using GAS - but I need the range to 'paste as values'

This code works (but pastes as formulas):


var sheet = SpreadsheetApp.openById("16S3INZFMQDY3yguNZ2QHvzMQXI1Kf97DkSvcYyZeiHM").getSheets()[1]

var source_range = sheet.getRange("D12:M12");
var target_range = sheet.getRange("D10:M10");
source_range.copy(target_range);

This code does not work (error = Exception: The parameters (SpreadsheetApp.Range) don't match the method signature for SpreadsheetApp.Range.copyValuesToRange.)

function copyRange() {

var sheet = SpreadsheetApp.openById("16S3INZFMQDY3yguNZ2QHvzMQXI1Kf97DkSvcYyZeiHM").getSheets()[1]

var source_range = sheet.getRange("D12:M12");
var target_range = sheet.getRange("D10:M10");
source_range.copyValuesToRange(target_range);
}

Can anyone please help as to why?

CodePudding user response:

In your script, how about the following modification?

From:

source_range.copy(target_range);

To:

source_range.copyTo(target_range, { contentsOnly: true });

or, when you want to use copyValuesToRange, how about the following modification?

source_range.copyValuesToRange(sheet, 4, 10, 10, 10);

or

source_range.copyValuesToRange(target_range.getGridId(), 4, 10, 10, 10);

or, when getValues and setValues are used, how about the following modification?

target_range.setValues(source_range.getValues());

References:

  • Related