Reputation: 55
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
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
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.
Upvotes: 1