I’m looking for the Google App Script to move row values from one tab to anther tab.
What I Wanted:
I wanted to move the row values from Tab:1 to Tab:2 when “M Status” is Married. Again I wanted to move row values from Tab:2 to Tab:1 when I change the “M Status” value to Single.
My Project
[enter image description here](https://i.sstatic.net/L4sHgVdr.png)
App Script
`
function onEdit(e){
let range = e.range;
let col = range.getColumn();
let row = range.getRow();
let val = range.getValue();
let source = e.source.getActiveSheet();
if (col == 4 && val != '' && val == 'Married'){
let ss = SpreadsheetApp.getActiveSpreadsheet();
let ssourceSheet = ss.getSheetByName('Singles');
let mtargetSheet = ss.getSheetByName('Married');
let sdata = ssourceSheet.getRange(row,1,1,ssourceSheet.getLastColumn()).getValues();
sdata[0][0] = '';
mtargetSheet.appendRow(sdata[0]);
ssourceSheet.deleteRow(row);
}
else if (col == 4 && val != '' && val == 'Single'){
let ms = SpreadsheetApp.getActiveSpreadsheet();
let msourceSheet = ms.getSheetByName('Married');
let stargetSheet = ms.getSheetByName('Singles');
let mdata = msourceSheet.getRange(row,1,1,msourceSheet.getLastColumn()).getValues();
mdata[0][0] = '';
stargetSheet.appendRow(mdata[0]);
msourceSheet.deleteRow(row);
}
}
`
Issue What I’m facing with this script
The row values got moved to respective tab. But the next row(Pair of rows) also getting moved to the next tab even though the value for M Status is different.
Example:
I have changed the value for M Status of row 1201 to ‘Married’ in Tab:1. After that the value for 1201 & 1202 rows got moved to Tab:2 even though value is ‘Single’.
Tab:1(Singles)
S No | Name | Age | M Status |
---|---|---|---|
1200 | Bird | 24 | Single |
1201 | Petrol | 22 | Single |
1202 | TVP | 23 | Single |
1203 | Daddy | 46 | Single |
Actual:
Tab:2(Married)
S No | Name | Age | M Status |
---|---|---|---|
1201 | Petrol | 22 | Married |
1202 | TVP | 23 | Single |
Expected:
Tab:2(Married)
S No | Name | Age | M Status |
---|---|---|---|
1201 | Petrol | 22 | Married |
Poovarasan Rajenthran is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.