Delete row in other sheets based on 2 values on main file - google-apps-script

Is it possible to delete rows on other sheets based on 2 values? Say I have 3 sheets. In the main sheet (sheet 1), there will be 2 columns: Branch and Manager same with the remaining sheets.
SAMPLE SPREADSHEET HERE.
Example data:
SHEET 1: (main sheet)
--- BRANCH --- MANAGER ---
California Tom Chang
Brooklyn Jon Sieg
New York Raq Craig
SHEET 2:
--- BRANCH --- MANAGER ---
California Jane Cali
California Tom Chang
San Francisco James Chao
SHEET 2:
--- BRANCH --- MANAGER ---
California Jane Cali
California Tom Chang
New York Daniel Trevor
What should happen is that:
Branch column values should NOT duplicate in all sheets. So what we need to do is delete the row on sheet 2 and 3 if branch column is equal with the main sheet (sheet1) IF AND ONLY IF the manager is not the same/equal. So in my given data above, Branch California and Manager Tom Chang exists in all sheets therefore it should not be touched. But California branch was repeated in the remaining 2 sheets with a different Manager. Therefore, row California ---- Jane Cali should be deleted on Sheets 2 and 3.
Came upon a script borrowed from this post but can't seem to work. Here:
function removeDupsInOtherSheets() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var mainsheet = ss.getSheetByName("Sheet3").getDataRange().getValues();
var sheet2 = ss.getSheetByName("Sheet2").getDataRange().getValues();
var sheet3 = ss.getSheetByName("Sheet3").getDataRange().getValues();
// iterate mainsheet and check in sheet2 & sheet3 if duplicate values exist
var nsheet2 = [];
var nsheet3 = [];
var mainsheetCol1 = [];// data in column1 of main sheet
var mainsheetCol2 = [];// data in column2 of main sheet
for(var n in mainsheet){
mainsheetCol1.push(mainsheet[n][0]); //column1
mainsheetCol2.push(mainsheet[n][3]); //column2
}
for(var n in sheet2){ // iterate sheet2 and test col 1 vs col 1 and co2 1 vs co2 1in sheet2
var noDup1 = checkForDup(sheet2[n],mainsheetCol1,mainsheetCol2)
if(noDup1){nsheet2.push(noDup1)};// if not present in sheet3 then keep
}
for(var n in sheet3){ // iterate sheet3 and test col 1 vs col 1 and co2 1 vs co2 in sheet3
var noDup2 = checkForDup(sheet3[n],mainsheetCol1,mainsheetCol2)
if(noDup2){nsheet3.push(noDup2)};// if not present in sheet3 then keep
}
// view result
Logger.log(nsheet2);
Logger.log(nsheet3);
// clear and update sheets
ss.getSheetByName("Sheet2").getDataRange().clear();
ss.getSheetByName("Sheet3").getDataRange().clear();
ss.getSheetByName("Sheet2").getRange(1,1,nsheet2.length,nsheet2[0].length).setValues(nsheet2);
ss.getSheetByName("Sheet3").getRange(1,1,nsheet3.length,nsheet3[0].length).setValues(nsheet3);
}
//Here can't seem to make it work to check if column 2 is not equal to the other sheets
//item is sheet2[n]
// s is mainsheetCol1
// s2 is mainsheetCol2
function checkForDup(item,s,s2){
Logger.log(s+' = '+item[0]+' ?')
Logger.log(s2+' = '+item[1]+' ?')
if((s.indexOf(item[0])>-1) && (s2.indexOf(item[1])>-1))){
return null;
}
return item;
}
Hoping someone could help/guide me. Thank you!

Try this:
function removeDuplicate(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var mainsheet = ss.getSheetByName("Sheet1");
var sheet2 = ss.getSheetByName("Sheet2");
var sheet3 = ss.getSheetByName("Sheet3");
var masterData = mainsheet.getDataRange().getValues();
var sheetsToCheck = [sheet2,sheet3];
for(var i in sheetsToCheck){
var valuesToCheck = sheetsToCheck[i].getDataRange().getValues();
for(var j=0;j<valuesToCheck.length;j++){
for(var k in masterData){
if(masterData[k][0] == valuesToCheck[j][0] && !(masterData[k][1] == valuesToCheck[j][1])){
sheetsToCheck[i].deleteRow(j+1);
}
}
}
}
}

Code given at Post is not working. I have modified it little bit to make it work.
Here it is,
function removeDupsInOtherSheets() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s1 = ss.getSheetByName("Sheet1").getDataRange().getValues();
var s2 = ss.getSheetByName("Sheet2").getDataRange().getValues();
var s3 = ss.getSheetByName("Sheet3").getDataRange().getValues();
// iterate s3 and check in s1 & s2 if duplicate values exist
var nS1 = [];
var nS2 = [];
var s3Col1 = [];// data in column1 of sheet3
for(var n=0; n<s3.length; ++n){
s3Col1.push(s3[n][0]);
}
for(var n=0; n<s1.length; ++n){ // iterate sheet1 and test col 1 vs col 1 in sheet3
var noDup1 = checkForDup(s1[n],s3Col1)
if(noDup1){nS1.push(noDup1)};// if not present in sheet3 then keep
}
for(var n=0; n<s2.length; ++n){ // iterate sheet2 and test col 1 vs col 1 in sheet3
var noDup2 = checkForDup(s2[n],s3Col1)
if(noDup2){nS2.push(noDup2)};// if not present in sheet3 then keep
}
Logger.log(nS1);// view result
Logger.log(nS2);
ss.getSheetByName("Sheet1").getDataRange().clear();// clear and update sheets
ss.getSheetByName("Sheet2").getDataRange().clear();
var nS1Length = nS1.length;
var nS2Length = nS2.length;
// This is the change needed in code you have copied from,
**if(nS1 != undefined){
data = []
data[0] = nS1;
var range = ss.getSheetByName("Sheet1").getRange(1,1);
range.setValues(data);
}
if(nS2 != undefined){
data = []
data[0] = nS1;
var range = ss.getSheetByName("Sheet2").getRange(1,1);
range.setValues(data);
}**
}
function checkForDup(item,s){
Logger.log(s+' = '+item[0]+' ?')
if(s.indexOf(item[0])>-1){
return null;
}
return item;
}

Related

Copying cells from sheet1 to sheet2 if it does not exist in sheet2

I am still fairly new to Apps Script and am trying to do the following:
Loop through Sheet1 Column C, which contains user_names. I want to compare the list of user_names in Sheet1 Column C to the list contained in Sheet 2 Column A. If there are user_names that exist in Sheet 1 Column C that do not exist in Sheet 2 Column A, I want to copy them to the end of the list in Sheet 2 Column A. If there are user_names that exist in both lists, I do not want to copy them.
I want the order of the user_names in Sheet 2 Column A to stay the same regardless of how Sheet1 Column C changes. I want any new additions to the list in Sheet1 Column C copied to the end of the list in Sheet 2 Column A, even if the new additions to Sheet1 Column C are found in the middle of that list (see examples below).
The order of the user_names in Sheet1 Column C changes daily.
Examples:
Given:
Sheet1 Column C | Sheet 2 Column A
-----------------------------------
username1 |
username2 |
username3 |
I want:
Sheet1 Column C | Sheet 2 Column A
-----------------------------------
username1 | username1
username2 | username2
username3 | username3
The next day, when the usernames update:
Given:
Sheet1 Column C | Sheet 2 Column A
-----------------------------------
username1 | username1
username3 | username2
username5 | username3
username4 |
username2 |
I want:
Sheet1 Column C | Sheet 2 Column A
-----------------------------------
username1 | username1
username3 | username2
username5 | username3
username4 | username5
username2 | username4
(in the last example username4 and username 5 can be in any order)
Here's a draft of a function -- I've played around with many versions but none of them worked. I used this question as a reference.
function copy_new_names() {
var spreadsheet = SpreadsheetApp.openById('MY ID IS HERE -- THIS IS A PLACEHOLDER');
var sheet = spreadsheet.getSheetByName('Sheet1');
var sheet2 = spreadsheet.getSheetByName('Sheet2');
var startingrow = 2;
var endrow = sheet.getLastRow();
var endcol = sheet.getLastColumn();
var endrow2 = sheet2.getLastRow();
var endcol2 = sheet2.getLastColumn();
var data = sheet.getDataRange().getValues();
var data2 = sheet2.getDataRange().getValues();
var resultArray = [];
for (var n in data){
var keep = true
for (var p in data2){
if( data[n][2] == data2[p][0]){
keep=false ; break ;
}
}
Logger.log(keep);
if(keep){ resultArray.push(data[n])};
sheet2.getRange(endrow2 + 1, 1,resultArray.length,resultArray[0].length).setValues(resultArray);
Logger.log(resultArray);
}
}
Any help would be great!! Thanks so much!
This code does what you want. I've tested it, and it works:
function copy_new_names() {
var spreadsheet, sheet, sheet2, endrow, endcol,endrow2,endcol2,data, data2,resultArray,n,l,l2,p,doesNotExist,
sheetOneValue, numberOfMissingValues, innerArray,i,thisValue;
spreadsheet = SpreadsheetApp.openById('');
sheet = spreadsheet.getSheetByName('Sheet1');
sheet2 = spreadsheet.getSheetByName('Sheet2');
endrow = sheet.getLastRow();
endcol = sheet.getLastColumn();
endrow2 = sheet2.getLastRow();
endcol2 = sheet2.getLastColumn();
data = sheet.getRange(1, 3, endrow, 1).getValues();
//getRange(start row, start column, number of Rows, number of Columns)
data2 = sheet2.getRange(1, 1, endrow2, 1).getValues();
data2 = data2.toString().split(","); //Flatten the 2D array to one D
resultArray = [];
innerArray = [];
l = data.length;
l2 = data2.length;
for (n=0;n<l;n+=1) {
sheetOneValue = data[n][0];
innerArray = []; //Reset
for (p=0;p<l2;p+=1) {
doesNotExist = data2.indexOf(sheetOneValue) === -1;//If the value is NOT found, indexOf returns -1
if (!doesNotExist) break; //If it exists in the list, no need to go any further
if (doesNotExist) {//If the value in the cell from data one does NOT exist in data two, add it to the array
innerArray.push(sheetOneValue)
resultArray.push(innerArray);
Logger.log('sheetOneValue: ' + sheetOneValue);
break ;
};
};
};
endrow2 = 0;
for (i=0;i<l;i+=1) {
thisValue = data2[i];
if (thisValue==="" || thisValue=== undefined) {//There is nothing in this cell
continue;
};
endrow2+=1;
};
//Append the data in the array to the bottom of the data two list
sheet2.getRange(endrow2+1, 1,resultArray.length,1).setValues(resultArray);
Logger.log(resultArray);
};
I had found a solution a little different but still works ...who can most can least ;)
If you want test...
// You can activate a trigger on function copy_new_names()
// Assuming that row 1 in each sheet is not included in the values to be processed...
function copy_new_names() {
var spreadsheet = SpreadsheetApp.openById('spreadsheet_id');
var sheet = spreadsheet.getSheetByName('Sheet1');
var sheet2 = spreadsheet.getSheetByName('Sheet2');
var endrow = sheet.getLastRow(), endcol = sheet.getLastColumn();
var endrow2 = sheet2.getLastRow(), endcol2 = sheet2.getLastColumn();
// assuming that row 1 is not included in the values to be processed
var valuesSheet1 = sheet.getRange(2, 3, endrow-1, 1).getValues(), valuesSheet2 = [];
if ( endrow2 > 1 ) valuesSheet2 = sheet2.getRange(2, 1, endrow2-1, 1).getValues();
var newlist = checkList(valuesSheet1, valuesSheet2);
//Below to paste new list in sheet2
sheet2.getRange(2, 1, newlist.length, 1).setValues(newlist);
}
function checkList(valuesSheet1, valuesSheet2) {
var values1 = valuesSheet1.map(function(d) { return d.join()}), values2 = [];
if ( valuesSheet2.length ) values2 = valuesSheet2.map(function(d) { return d.join()});
var newList = [];
for ( var i in values1 ) {
if ( values2.indexOf(values1[i]) === -1 ) newList.push([values1[i]]);
}
newList = valuesSheet2.concat(newList);
//Logger.log(newList)
return newList;
}

Copy row into new sheet(s) if two different cell values are within a defined range

I would like to create a Google Spreadsheets document that maintains:
Sheet 1 (All Entries): Unfiltered
Sheet 2 (1st Level Filter): Copies rows from Sheet 1 that Column B >/= 5000 OR Column C >/= 50000
Sheet 3 (2nd Level Filter): Copies rows from Sheet 1 that Column B >/= 5000 AND Column C >/= 50000
I created a sample doc for reference that's editable here.
Fairly limited programming/script experience.
In case you were looking for a script. This is what it'll look like although you'll have to do the mapping yourself. I haven't opened your doc but this will make it work if you need a script to do your bidding.
function sheetMaintain()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s1 = ss.getSheetByName("Sheet1");
var s2 = ss.getSheetByName("Sheet2");
var s3 = ss.getSheetByName("Sheet3");
var r1 = s1.getDataRange();
var r2 = s2.getDataRange();
var r3 = s3.getDataRange();
var data1 = r1.getValues();
for (var i=0; i<r1.getLastRow(); i++)
{
if (data1[i][1]>=5000 || data[i][2]>=50000)
{
var colB = data[i][1];
var colC = data[i][2];
var lr2 = r2.getLastRow();
s2.getRange(lr2+1, 1).setValue(colB); //Mapping Col B value to Col A of Sheet 2
s2.getRange(lr2+1, 2).setValue(colC); //Mapping Col C value to Col B of Sheet 2
}
if (data1[i][1]>=5000 && data[i][2]>=50000)
{
var colB = data[i][1];
var colC = data[i][2];
var lr3 = r3.getLastRow();
s3.getRange(lr3+1, 1).setValue(colB); //Mapping Col B value to Col A of Sheet 3
s3.getRange(lr3+1, 2).setValue(colC); //Mapping Col C value to Col B of Sheet 3
}
}
}
if you'd like to not have any duplicates (from running the function time and again) in Sheet2 or Sheet3 I would also suggest you look into using the clear() function and set your Sheet2 and Sheet3 to clear before you parse through Sheet1 again.
I have added a formula to both sheets to achieve something like you may want
i.e. the first one below is the OR and the second is the AND
=filter('Sheet 1 - All Entries'!A:C,(('Sheet 1 - All Entries'!B:B>=5000) + ( 'Sheet 1 - All Entries'!C:C >=5000)))
=filter('Sheet 1 - All Entries'!A:C,(('Sheet 1 - All Entries'!B:B>=5000) * ( 'Sheet 1 - All Entries'!C:C >=5000)))
PS... I am not sure if a formula is sufficient for you or if you wanted this done through script

Delete row values in more than 1 sheet if exists in another sheet

The code below is from an answer from this post regarding copying row values to a new sheet if it exist in another sheets.
Now, what if instead of copying the duplicate values to sheet 3, I want to delete them from sheets 1 and 2 if it exists in Sheet 3. With the same spreadsheet, I have 3 sheets. The unique value that will be compared on the first 2 sheets is the first column, "ID NUMBER".
Given the values, 784 | John Steep | I.T Department, which exists in all 3 sheets, the same row value should be deleted in Sheet 1 and 2 and retain the same value on Sheet 3.
function copyRowtoSheet3() {
var s1 = SpreadsheetApp.openById("ID").getSheetByName('Sheet1');
var s2 = SpreadsheetApp.openById("ID").getSheetByName('Sheet2');
var s3 = SpreadsheetApp.openById("ID").getSheetByName('Sheet3');
var values1 = s1.getDataRange().getValues();
var values2 = s2.getDataRange().getValues();
var resultArray = [];
for(var n=0; n < values1.length ; n++){
var keep = false;
for(var p=0; p < values2.length ; p++){
Logger.log(values1[n][0]+' =? '+values2[p][0]);
if( values1[n][0] == values2[p][0] && values1[n][3] == values2[p][4]){
resultArray.push(values1[n]);
Logger.log('true');
break ;// remove this if values are not unique and you want to keep all occurrences...
}
}
}
s3.getRange(+1,1,resultArray.length,resultArray[0].length).setValues(resultArray);
}
Can't seem to find the right solution. Tried several scripts but failed to make it work.
Thank you for any advice/suggestion.
Although the other answer works (I didn't test but I guess it does) it uses a lot of spreadsheetApp calls and might be slow if you have a lot of data.
It is possible to get this result using only arrays (if you don't need to keep sheet formatting and/or formulas).
The approach is slightly different as it is easier to keep data instead of removing it.
There are for sure many possible solutions, below is the one I tried : I created a special array that contains only the first column of sheet3 to make the duplicate search simpler.
function removeDupsInOtherSheets() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s1 = ss.getSheetByName("Sheet1").getDataRange().getValues();
var s2 = ss.getSheetByName("Sheet2").getDataRange().getValues();
var s3 = ss.getSheetByName("Sheet3").getDataRange().getValues();
// iterate s3 and check in s1 & s2 if duplicate values exist
var nS1 = [];
var nS2 = [];
var s3Col1 = [];// data in column1 of sheet3
for(var n in s3){
s3Col1.push(s3[n][0]);
}
for(var n in s1){ // iterate sheet1 and test col 1 vs col 1 in sheet3
var noDup1 = checkForDup(s1[n],s3Col1)
if(noDup1){nS1.push(noDup1)};// if not present in sheet3 then keep
}
for(var n in s2){ // iterate sheet2 and test col 1 vs col 1 in sheet3
var noDup2 = checkForDup(s2[n],s3Col1)
if(noDup2){nS2.push(noDup2)};// if not present in sheet3 then keep
}
Logger.log(nS1);// view result
Logger.log(nS2);
ss.getSheetByName("Sheet1").getDataRange().clear();// clear and update sheets
ss.getSheetByName("Sheet2").getDataRange().clear();
ss.getSheetByName("Sheet1").getRange(1,1,nS1.length,nS1[0].length).setValues(nS1);
ss.getSheetByName("Sheet2").getRange(1,1,nS2.length,nS2[0].length).setValues(nS2);
}
function checkForDup(item,s){
Logger.log(s+' = '+item[0]+' ?')
if(s.indexOf(item[0])>-1){
return null;
}
return item;
}
Sheet1
ID NUMBER NAME DEPARTMENT
784 John Steep I.T.
901 Liz Green H.R.
Sheet2
ID NUMBER NAME DEPARTMENT
784 John Steep I.T.
653 Bo Gore Marketing
Sheet3
ID NUMBER NAME DEPARTMENT
784 John Steep I.T.
999 Frank White Sales
121 Abid Jones Engineering
901 Liz Green H.R.
Script
function main() {
var ss = SpreadsheetApp.openById("ID");
var s1 = ss.getSheetByName("Sheet1");
var s2 = ss.getSheetByName("Sheet2");
var s3 = ss.getSheetByName("Sheet3");
var idCol = 1; // Assuming location of ID column is same in all sheets.
var s1RowCount = s1.getLastRow();
for (var i = 2; i <= s1RowCount; i++) { // Start at var i = 2 to skip the
// first row containing the header.
var id = s1.getRange(i, idCol, 1, 1).getValue();
deleteDuplicates(s2, id);
deleteDuplicates(s3, id);
}
}
function deleteDuplicates(sheet, id) {
var idCol = 1; // Assuming location of ID column is same in all sheets.
var rowCount = sheet.getLastRow();
for (var i = 2; i <= rowCount; i++) {
var data = sheet.getRange(i, idCol, 1, 1).getValue();
if (data === id) {
// Use this to test out the function.
Logger.log("Duplicate of ID " + id + " in sheet " +
sheet.getSheetName() + " at row " + i);
// Uncomment the next line when ready.
// sheet.deleteRow(i);
}
}
}
Logging Output
[14-11-04 09:16:04:551 PST] Duplicate of ID 784 in sheet Sheet2 at row 2
[14-11-04 09:16:04:587 PST] Duplicate of ID 784 in sheet Sheet3 at row 2
[14-11-04 09:16:04:727 PST] Duplicate of ID 901 in sheet Sheet3 at row 5

Copy row values to a new sheet if it exist in another sheet

I am new to Google Script and I have a script to create. I found this question, where in he should delete row if a value in it exists in another sheet. Now, my situation is different. Here is my sample Spreadsheet. In that Google Spreadsheet, I have 3 sheets. The unique value that will be compared on the first 2 sheets is the first column, "ID NUMBER".
Given the values, 784 | John Steep | I.T Department exists in the first 2 sheets therefore the whole row should be copied to sheet3.
Sum up, if that ID NUMBER exists in Sheet 1 and 2, it should be copied on Sheet 3.
I tried to modify the script but I can't make it work:
function copyRowtoSheet3() {
var s1 = SpreadsheetApp.openById("1RlQTLZyPLasoJGplKemKg9qgcLcvCZZ_tPn6lWXEePw").getSheetByName('Sheet1');
var s2 = SpreadsheetApp.openById("1RlQTLZyPLasoJGplKemKg9qgcLcvCZZ_tPn6lWXEePw").getSheetByName('Sheet2');
var s3 = SpreadsheetApp.openById("1RlQTLZyPLasoJGplKemKg9qgcLcvCZZ_tPn6lWXEePw").getSheetByName('Sheet3');
var values1 = s1.getDataRange().getValues();
var values2 = s2.getDataRange().getValues();
var resultArray = [];
for(var n in values1){
var keep = true
for(var p in values2){
if( values1[n][0] == values2[p][0] && values1[n][1] == values2[p][1]){
keep=false ; break ;
}
}
if(keep){
resultArray.push(values1[n])};
}
s1.clear()
s1.getRange(1,1,resultArray.length,resultArray[0].length).setValues(resultArray);
}
Thanks. Any help/advice is greatly appreciated.
Not sure your condition works... Getting the values that are equal in both sheets is easier than the example you refer to, just keep the data where equality==true.
Try like this (change the ID's to yours):
function copyRowtoSheet3() {
var s1 = SpreadsheetApp.openById("1x8buwr______w7MeqZAiJJIX0yC-oITBAtykBAM").getSheetByName('Sheet1');
var s2 = SpreadsheetApp.openById("1x8buwr______w7MeqZAiJJIX0yC-oITBAtykBAM").getSheetByName('Sheet2');
var s3 = SpreadsheetApp.openById("1x8buwr______w7MeqZAiJJIX0yC-oITBAtykBAM").getSheetByName('Sheet3');
var values1 = s1.getDataRange().getValues();
var values2 = s2.getDataRange().getValues();
var resultArray = [];
for(var n=0; n < values1.length ; n++){
var keep = false;
for(var p=0; p < values2.length ; p++){
Logger.log(values1[n][0]+' =? '+values2[p][0]);
if( values1[n][0] == values2[p][0] && values1[n][1] == values2[p][1]){
resultArray.push(values1[n]);
Logger.log('true');
break ;// remove this if values are not unique and you want to keep all occurrences...
}
}
}
s3.getRange(+1,1,resultArray.length,resultArray[0].length).setValues(resultArray);
}
Try:
var s1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet 1');
var s2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet 2');
var s3 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet 3');
and further:
s3.clear()
s3.getRange(1,1,resultArray.length,resultArray[0].length).setValues(resultArray);

Google Script: Delete row if a value in it exists in more than one sheet

I had posted a question months ago regarding deleting a row if a value in it exists in another sheet.
This is the accepted answer in my previous post.
function deleteRowInSheet1() {
var s1 = SpreadsheetApp.openById("SS_ID").getSheetByName('Sheet1');
var s2 = SpreadsheetApp.openById("SS_ID").getSheetByName('Sheet2');
var values1 = s1.getDataRange().getValues();
var values2 = s2.getDataRange().getValues();
var resultArray = [];
for(var n in values1){
var keep = true
for(var p in values2){
if( values1[n][0] == values2[p][0] && values1[n][1] == values2[p][1]){
keep=false ; break ;
}
}
if(keep){ resultArray.push(values1[n])};
}
s1.clear()
s1.getRange(1,1,resultArray.length,resultArray[0].length).setValues(resultArray);
}
Now, my question is what if there are more than 1 sheet to be compared to. Let's say I have 4 sheets.
Sheet1:
NAME | PLACE | AGE
Carl | Florida | 45
Mike | Florida | 41
Suzy | Florida
Sheet 2:
NAME | PLACE | AGE
Mike | Florida | 41
Sheet 3:
NAME | PLACE | AGE
Mike | Florida | 41
Sheet 4:
NAME | PLACE | AGE
Mike | Florida | 41
The script should delete Mike Florida row in Sheet 2 to 4 since it has duplicate data in Sheet 1. Column 1 and 2, again are the basis to compare all the data in the 4 sheets since the values will be unique. I tried to modify the answered script but I can't seem to make it run. I understood the logic but not sure which part I missed:
This is my code:
function deleteRowInSheets1to3() {
var s1 = SpreadsheetApp.openById("SS_ID").getSheetByName('Sheet1'); //this is the basis of all sheets. values to be compared is column 2
var s2 = SpreadsheetApp.openById("SS_ID").getSheetByName('Sheet2');
var s3 = SpreadsheetApp.openById("SS_ID").getSheetByName('Sheet3');
var s4 = SpreadsheetApp.openById("SS_ID").getSheetByName('Sheet4');
var values1 = s1.getDataRange().getValues();
var values2 = s2.getDataRange().getValues();
var values3 = s3.getDataRange().getValues();
var values4 = s4.getDataRange().getValues();
var resultArray = [];
for(var n in values1){
var keep = true
for(var p in values2){
if( values1[n][0] == values2[p][0] && values1[n][1] == values2[p][1]){
keep=false ; break ;
}
for(var q in values3){
if( values1[n][0] == values3[q][0] && values1[n][1] == values3[q][1]){
keep=false ; break ;
}
for(var r in values4){
if( values1[n][0] == values4[r][0] && values1[n][1] == values4[r][1]){
keep=false ; break ;
}
}
}
}
if(keep){ resultArray.push(values1[n])};
}
s1.clear()
s1.getRange(1,1,resultArray.length,resultArray[0].length).setValues(resultArray);
}
Thanks a bunch for any advice! :)
I think we almost have the same situation. This is a possible duplicate question. See this post.
The answer provided was below (not my code):
function removeDupsInOtherSheets() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s1 = ss.getSheetByName("Sheet1").getDataRange().getValues();
var s2 = ss.getSheetByName("Sheet2").getDataRange().getValues();
var s3 = ss.getSheetByName("Sheet3").getDataRange().getValues();
// iterate s3 and check in s1 & s2 if duplicate values exist
var nS1 = [];
var nS2 = [];
var s3Col1 = [];// data in column1 of sheet3
for(var n in s3){
s3Col1.push(s3[n][0]);
}
for(var n in s1){ // iterate sheet1 and test col 1 vs col 1 in sheet3
var noDup1 = checkForDup(s1[n],s3Col1)
if(noDup1){nS1.push(noDup1)};// if not present in sheet3 then keep
}
for(var n in s2){ // iterate sheet2 and test col 1 vs col 1 in sheet3
var noDup2 = checkForDup(s2[n],s3Col1)
if(noDup2){nS2.push(noDup2)};// if not present in sheet3 then keep
}
Logger.log(nS1);// view result
Logger.log(nS2);
ss.getSheetByName("Sheet1").getDataRange().clear();// clear and update sheets
ss.getSheetByName("Sheet2").getDataRange().clear();
ss.getSheetByName("Sheet1").getRange(1,1,nS1.length,nS1[0].length).setValues(nS1);
ss.getSheetByName("Sheet2").getRange(1,1,nS2.length,nS2[0].length).setValues(nS2);
}
function checkForDup(item,s){
Logger.log(s+' = '+item[0]+' ?')
if(s.indexOf(item[0])>-1){
return null;
}
return item;
}
Hope this helps.