Is there a way to sync two sheets?

There is no way in Google Docs to publish only one sheet of one spreadsheet. So you need to split the whole spreadsheet. So, I was thinking of writing a script to synchronize two sheets (each in a different table). I thought using a function to get strings as an array to do this. Is there a better strategy for this?

+4
source share
2 answers

One way to achieve this is to add a script to both spreadsheets that copy its contents to another table in the change trigger. For example, if you want to add something like below to both spreadsheets, exchanging source and target information.

var sourceSpreadsheetID = "ID HERE";
var sourceWorksheetName = "SHEET NAME HERE";
var destinationSpreadsheetID = "ID HERE";
var destinationWorksheetName = "SHEET NAME HERE";

function importData() {
  var thisSpreadsheet = SpreadsheetApp.openById(sourceSpreadsheetID);
  var thisWorksheet = thisSpreadsheet.getSheetByName(sourceWorksheetName);
  var thisData = thisWorksheet.getDataRange();
  var toSpreadsheet = SpreadsheetApp.openById(destinationSpreadsheetID);
  var toWorksheet = toSpreadsheet.getSheetByName(destinationWorksheetName);
  var toRange = toWorksheet.getRange(1, 1, thisData.getNumRows(), thisData.getNumColumns())
  toRange.setValues(thisData.getValues()); 
}

Just add a change trigger for the importData function, and then when any changes are made to any document, it will copy the contents to another table, thereby maintaining synchronization.

Obviously, if both tables are updated at the same time, you are facing problems.

+4
source

, Google. , , . , , , , "" .

, , "", , Google. , , , , ImportRange() -, 1- .

+3

All Articles