I need to sync all sheets in the same Google Sheets file. I only want to sync the first row and the first column (which are frozen), while the rest of the contents should not be synced.
I'd also like to make it so that if I insert/remove a row to/from a sheet, the same action is performed to all other synced sheets too. Also, if other modifications are done, e.g. a row is frozen, the same should be done on all sheets.
I have seen the codes in these questions:
- How to sync two sheets with =importrange() in two googlespreadsheet?
- Is there a way to keep two sheets synchronized?
In short, I want to be able to use a "master" sheet form, and have several sheets with different values in them. I a change to the master is applied, changes to the sheets should reflect the changes in the master. Two way sync would be nice but is not a stringent requirement.
So far I have managed to put down this:
var masterSheetN = 2; /*2 means the master sheet is the second sheet. Sheets before the master sheet are ignored*/
function importData() {
/*Input sheet*/
var fromSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var fromSheets = fromSpreadsheet.getSheets();
var fromWorksheet = fromSheets[masterSheetN-1];
var fromData = fromWorksheet.getDataRange();
var fromRowsN = fromData.getNumRows()
var fromColsN = fromData.getNumColumns()
var fromRow1 = fromWorksheet.getRange(1, 1, 1, fromColsN);
var fromCol1 = fromWorksheet.getRange(1, 1, fromRowsN);
for (i=masterSheetN; i<fromSheets.length; i++) {
/*Output sheet*/
var toWorksheet = fromSheets[i];
var toRow1 = toWorksheet.getRange(1, 1, 1, fromColsN);
var toCol1 = toWorksheet.getRange(1, 1, fromRowsN);
/*Sync row and col 1*/
toRow1.setValues(fromRow1.getValues());
toCol1.setValues(fromCol1.getValues());
/*Sync format for the whole sheet*/
var toGID = toRow1.getGridId()
fromData.copyFormatToRange(toGID, 1, fromColsN, 1, fromRowsN)
}
}
This successfully syncs all sheets with the master sheet for the first row and column, and also for formatting. However, it is quite slow, especially for syncing the formatting (why? syncing the first row/column seems much faster), and it does not sync things like frozen/protected rows, etc. Is there a way to sync everything from the Master sheet when a new sheet is created?
=IMPORTRANGE(file_key, "sheet1!A1:B")
You will only be able to edit this part in the main sheet and you do NOT want to re-sort the data or insert lines unless your other sheets only use functions on the initial data, else items will not line up. – Karl_S