Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to keep two sheets synchronized?

There isn't a way to share only one sheet of one spreadsheet in Google Docs. So, you have to share an entire spreadsheet. So, I was thinking in writing a script to synchronize two sheets (each one in a different spreadsheet). I thought using a function to get rows as array to do this. Is there a better strategy to do that?

like image 906
craftApprentice Avatar asked Dec 07 '13 17:12

craftApprentice


People also ask

How do you keep two Excel sheets syncing?

Sync Excel Spreadsheets Using the Paste Link Feature Start by opening your Excel spreadsheet, clicking on the cell that you want to link to, and then selecting the “Copy” button on the “Home” tab. Select the cell that you are linking from, click the “Paste” list arrow, then select “Paste Link.”

Can you link 2 Google Sheets?

If you keep data in separate Google Sheets, copy a range of data from one spreadsheet to another with the IMPORTRANGE function. For example, you may track quarterly sales data for a product in a different spreadsheet for each region.


1 Answers

One way you could accomplish this is by adding a script to both spreadsheets that copies it's contents to the other spreadsheet on a change trigger. For example if you were to add something like the below to both spreadsheets, swapping the source and destination information around.

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 either document it will copy the contents to the other spreadsheet, thus keeping the both synced.

Obviously if both spreadsheets are being updated at the same time you will run into trouble.

like image 189
dev Avatar answered Sep 24 '22 02:09

dev