I'd like to be able to copy a colum (well 3) from a spreadsheet to another spreadsheet (google docs spreadsheets). I'd also like there to be some sort of trigger that looks for modifications and automatically copies them to the new spreadsheet.
here is the code I have, that is not working! Thanks!
function copytest()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var target = SpreadsheetApp.openById("0AvxaM_p22RvwdHl2MFl4MkhteVhmVzFDY2FOblNpVEE");
var source_sheet = ss.getSheetByName("Sheet1");
var target_sheet = target.getSheetByName("Sheet1");
var source_range = source_sheet.getRange("A1:B2");
source_range.copyTo("A1:B2");
}
I think its all working except the last line source_range.copyTo("A1:B2"); I'm new to this scripting business, so thanks for your help.
I have found that it is easier to just copy the source_sheet to the target_spreadsheet, copy the necessary range using
copyTo
thendelete
the copied source_sheet.the
copyTo()
method works only when destination is in the same spreadsheet. To copy data from one spreadsheet to another you will have to get the values (and whatever format, colors, fontsize...) from the source range into a variable (a 2D array or as indicated in the tutorial that Srik was referring to) and write it back to the target range.for example :
NEW UPDATED CODE IS AS FOLLOWS (EDIT BY QUESTION AUTHOR)
EDIT 2 (BY ANSWERER)
to get a full column, change the range definition like this :
if you want to copy multiple contiguous columns just define the source sheet accordingly, in A1 notation column A+B+C would be
.getRange("A2:C")
;.Try to use the integer definition for range, IMO it's more easy and more readable : for example a range of full columns A+B+C would be
sh.getRange(1,1,sh.getLastRow(),3)
For non contiguous columns it's becoming a bit more complicated.... maybe a new thread ?
Since you are copying the data into the other spreadsheet you have to specify the other spreadsheet's range object. So, instead of your last line: