google apps script - Copy content from one spreadsheet to another without IMPORTRANGE -
i'm trying create bidirectional sync of content between 2 google spreadsheets.
my script working when:
- the source , destination id of sheets same , have id of active spreadsheet
so, works:
function myfunction() { var sourcespreadsheet = spreadsheetapp.openbyid("id-of-active-spreadsheet"); var destinationspreadsheet = spreadsheetapp.openbyid("id-of-active-spreadsheet"); var sourcesheet = sourcespreadsheet.getsheetbyname("sheet1"); var destinationsheet = destinationspreadsheet.getsheetbyname("sheet2"); var sourcerange = sourcesheet.getrange("a1:a4"); // destination, columnstart, columnend, rowstart, rowend sourcerange.copyvaluestorange(destinationsheet, 2, 2, 3, 7); }
but when replace destination id id of spreadsheet, doesn't work. have permissions? have tried publish both spreadsheets.
this doesn't:
... var sourcespreadsheet = spreadsheetapp.openbyid("id-of-active-spreadsheet"); var destinationspreadsheet = spreadsheetapp.openbyid("id-of-another-spreadsheet"); ...
since i'm trying create bidirectional sync don't think can use importrange
it seems copyvaluestorange doesn't communicate outside spreadsheet, here working solution:
function onchange() { var sourcespreadsheet = spreadsheetapp.getactivespreadsheet().getsheetbyname("sheet1"); var sourcedata = sourcespreadsheet.getrange("a1:a8").getvalues(); var targetspreadsheet = spreadsheetapp.openbyid("id-of-another-spreadsheet").getsheetbyname("sheet1"); targetspreadsheet.getrange("a1:a8").setvalues(sourcedata); }
ps: enables bidirectional sync between spreadsheets, add copy of code on both spreadsheet , b , replace "id-of-another-spreadsheet" id of other spreadsheet. , define from- , range in place of "a1:a8".
Comments
Post a Comment