John ED
John ED

Reputation: 55

How to Set Data Range

I'm currently getting all values from a Google Sheet with the code below, but how can I, after getting all values from there, now set them in another one?

var ss = SpreadsheetApp.openById(ID);

var values = ss.getDataRange().getValues();

var ss1= SpreadsheetApp.openById(ID1);

I wasn't able to do it, is there a way to do it with a for loop?

Upvotes: 0

Views: 931

Answers (2)

John ED
John ED

Reputation: 55

In the end, I got it working, thank you Karan, you were right about having to specify the sheet, however, I didn't have to specify the first opened sheet, but had to in the second one and for some reason it wouldn't work via getSheetByName('Sheet1'):

var ss = SpreadsheetApp.openById(ID);
var values = sourceSheet.getDataRange().getValues();

 var ss1 = SpreadsheetApp.openById(ID1).getActiveSheet();

targetSheet.getRange(1, 1, values.length, values[0].length).setValues(values);

Upvotes: 0

Karan
Karan

Reputation: 1187

I think you might need to get the sheet(s) first. With that:

var ss = SpreadsheetApp.openById(ID);
var sourceSheet = ss.getSheetByName('sourceSheetName');
var values = sourceSheet.getDataRange().getValues();

var ss1= SpreadsheetApp.openById(ID1);
var targetSheet = ss1.getSheetByName('targetSheetName');
targetSheet.getRange(1, 1, values.length, values[0].length).setValues(values);
//change 1,1 to desired row and columns if not starting from row 1 and col 1.

Sheet.getRange()

Range.setValues()

Upvotes: 1

Related Questions