I am new to GAS and I am working on synchronising 3 Google Sheets, based on
Is there a way to keep two sheets synchronized?
I am currently struggling with the problem that I cannot synchronise both sheets at the same time on a Master Sheet, that I got only one of datasets from either sheets. (E.g.: Sheet 1 data of A1:C5 is copied twice on Master A1:C5 and D1:F5)
The codes for Sheet 1 are as shown below.
var sourceSpreadsheetID = "Sheet ID 1";
var sourceWorksheetName = "Sheet1";
var destinationSpreadsheetID = "Master ID";
var destinationWorksheetName = "Master";
function syncData() {
var thisSpreadsheet = SpreadsheetApp.openById(sourceSpreadsheetID);
var thisWorksheet = thisSpreadsheet.getSheetByName(sourceWorksheetName);
var thisData = thisWorksheet.getRange("A1:C5");
var toSpreadsheet = SpreadsheetApp.openById(destinationSpreadsheetID);
var toWorksheet = toSpreadsheet.getSheetByName(destinationWorksheetName);
var toRange = toWorksheet.getRange("A1:C5");
toRange.setValues(thisData.getValues());
}
For Sheet 2, nothing much above changes(only "Sheet 1 ID" to "Sheet 2 ID") but its location on Master will be as shown below.
function syncData1() {
var thisSpreadsheet = SpreadsheetApp.openById(sourceSpreadsheetID);
var thisWorksheet = thisSpreadsheet.getSheetByName(sourceWorksheetName);
var thisData = thisWorksheet.getRange("A1:C5");
var toSpreadsheet = SpreadsheetApp.openById(destinationSpreadsheetID);
var toWorksheet = toSpreadsheet.getSheetByName(destinationWorksheetName);
var toRange = toWorksheet.getRange("D1:F5");
toRange.setValues(thisData.getValues());
}