Home > Mobile >  2-Way Sync between tabs in Google Sheets [closed]
2-Way Sync between tabs in Google Sheets [closed]

Time:09-17

I have a script that is supposed to sync 2 columns in two different tabs. For some reason it's not working. Thanks! Here is the script:

function onEdit(e){
  var ss = SpreadsheetApp;
  var sheet = ss.getActiveSpreadsheet();

  var sheet1 = sheet.getSheetByName('Sheet1');
  var sheet2 = sheet.getSheetByName('Sheet2');

  var cell = e.range;
  var value = cell.getValue();
  var currentRow = cell.getRow();
  var currentColumn = cell.getColumn();

  var activeWorksheet = ['Sheet1','Sheet2'];
  var columnEdit = [1,2];

  if( activeWorksheet.indexOf(ss.getActiveSheet().getName()) > -1 && columnEdit.indexOf(currentColumn) > -1 && currentRow > 1){
    sheet1.getRange(currentRow. currentColumn).setValue(value);
    sheet2.getRange(currentRow, currentColumn).setValue(value);
  };
};

CodePudding user response:

Typo mistake

sheet1.getRange(currentRow, currentColumn).setValue(value);

CodePudding user response:

function onEdit(e) {
  //e.source.toast('entry');
  const sh = e.range.getSheet();
  const shts = ['Sheet1', 'Sheet2'];
  const cols = [1, 2];
  if (~shts.indexOf(sh.getName()) && ~cols.indexOf(e.range.columnStart) && e.range.rowStart > 1) {
    //e.source.toast(sh.getName());
    shts.forEach(s => {
      e.source.getSheetByName(s).getRange(e.range.rowStart,e.range.columnStart).setValue(e.value);
    });
  }
}

You can add more columns and sheet names if you wish

  • Related