Need to find duplicate data in another sheet and delete row - google-apps-script

Need some help.
I need Script to check data in B3 Sheet 2
and find Data in colum A in Sheet 1 and Delete row duplicate data
Thank you.
Sample
https://i.stack.imgur.com/3sT3X.jpg
https://i.stack.imgur.com/zTRtg.jpg

I believe your goal is as follows.
From I need Script to check data in B3 Sheet 2 and find Data in colum A in Sheet 1 and Delete row duplicate data, you want to delete rows of "Sheet1" by searching the values of column "A" using a value of "B3" of "Sheet2".
You want to achieve this using Google Apps Script.
In this case, how about the following sample script?
Sample script 1:
In this sample, TextFinder is used. If the number of deleting rows is small, this script might be useful. Please copy and paste the following script to the script editor of Spreadsheet. And, please confirm your sheet names again. And, please run sample1.
function sample1() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet1 = ss.getSheetByName("Sheet1");
const sheet2 = ss.getSheetByName("Sheet2");
const search = sheet2.getRange("B3").getDisplayValue();
sheet1.getRange("A2:A" + sheet1.getLastRow()).createTextFinder(search).findAll().reverse().forEach(r => sheet1.deleteRow(r.getRow()));
}
Sample script 2:
In this sample, filter and setValues are used. If the number of deleting rows is large, this script might be useful. Please copy and paste the following script to the script editor of Spreadsheet. And, please confirm your sheet names again. And, please run sample2.
function sample2() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet1 = ss.getSheetByName("Sheet1");
const sheet2 = ss.getSheetByName("Sheet2");
const search = sheet2.getRange("B3").getValue();
const range = sheet1.getDataRange();
const values = range.getValues().filter(([a]) => a != search);
range.clearContent().offset(0, 0, values.length, values[0].length).setValues(values);
}
Note:
When this script is run, I think that your showing goal will be achieved.
But, if your actual Spreadsheet is different from your question and/or if your question has hidden requests, this script might not be able to be used. Please be careful about this.
References:
createTextFinder(findText)
filter()
setValues(values)

Related

Implementing a Dynamic "Lookup Criteria" for a Column in Google Sheets

In google sheet, I list dropdown options in a column. I get the options of this dropdown from a certain range in the other sheet.
Data validation for single cell(g2)
As you can see in the example, my data validation lookup criterion for the G2 cell is F133 to F260 in the "Lookup" Sheet.
What I want here is to get this data from the next column in the Lookup sheet in each cell that continues as G3-G4-G5.... For instance: "G133-G260" for G3, "H133-H260" for G4.
I can do this manually one by one with creating a individual data validation per cell, but is there a quick way to do it?
I believe your goal is as follows.
You want to create the data validation rules into the cells "G3:G" of the target sheet.
The data validation rules use the range of Lookup!F133:F260, Lookup!G133:G260, Lookup!H133:H260,,, to "G2", "G3", "G4",,, of the target sheet, respectively.
You want to achieve this using Google Apps Script.
In this case, how about the following sample script?
Sample script:
Please copy and paste the following script to the script editor of Google Spreadsheet and set your target sheet name. And, save the script.
function myFunction() {
const sheetName = "Sheet1"; // Please set your target sheet name.
let offset = 6;
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName(sheetName);
const loopupSheet = ss.getSheetByName("Lookup");
const range = sheet.getRange("G2:G" + sheet.getLastRow());
const dataValidations = range.getValues().map(_ => [SpreadsheetApp.newDataValidation().requireValueInRange(loopupSheet.getRange(133, offset++, 128)).build()]);
range.setDataValidations(dataValidations);
}
When this script is run, the ranges of Lookup!F133:F260, Lookup!G133:G260, Lookup!H133:H260,,, are used as the data validation rules, and they are put to the cells "G2", "G3", "G4",,,.
In this case, sheet.getRange("G2:G" + sheet.getLastRow()) is used as the destination range. If you want to manually set the range, please modify it with like sheet.getRange("G2:G10").
let offset = 6; means that the 1st column is column "F". If you want to change this, please modify it.
References:
map()
setDataValidations(rules)
You could do this without script if you're willing to have an auxiliary sheet. Just put in your Auxiliary sheet: =TRANSPOSE(Lookup!G133:260)
Then, go back and select your range in column G. Select all the cells and put in Data Validation from a Range: =Auxiliary!1:1 - Be sure you don't put any "$", that way each cell will go and look in the next row and so on

To copy Cell values from a range and paste it in a single row of another sheet

Wanted to accomplish below mentioned task,
Having 2 Sheets(Sheet1 and Sheet 2)
On clicking the Checkbox on Sheet 1:J1, data present in the table form on the Sheet 1 should get pasted in the single row of Sheet2
For e.g Product 1 data (P1) to the Product 1 Column, Product 2 data (P2) to the Product 2 Column and so on...
For every new submit, new data to be recorded on the next row of Sheet2.
SS link:https://docs.google.com/spreadsheets/d/1tmnDOMyupjeO8d65qHQsxb5KrrKeq7pMYIE8h2VmEJ4/edit?usp=sharing
Thanks in advance
I believe your goal is as follows.
When a checkbox of "H1" of "Sheet1" is checked, you want to copy the values from the cells "A3:G6" to the columns "B" to "AC" in one row of "Sheet2".
And, you want to copy "B1" and "B10" of "Sheet1" to the columns "A" and "AD" of "Sheet2".
In your Spreadsheet, there are no merged cells.
In this case, how about the following sample script?
Sample script:
function onEdit(e) {
const range = e.range;
const sheet = range.getSheet();
if (sheet.getSheetName() != "Sheet1" || range.getA1Notation() != "H1" || !range.isChecked()) return;
const srcRange = sheet.getRange("A3:G6");
const [[b1], , , , , , , , , [b10]] = sheet.getRange("B1:B10").getValues();
const values = [b1, ...srcRange.getValues().flat(), b10];
const dstSheet = e.source.getSheetByName("Sheet2");
dstSheet.appendRow(values);
range.uncheck();
// srcRange.clearContent();
}
This script is automatically run with the simple trigger of OnEdit trigger. So, when you use this script, please check the checkbox of "H1" of "Sheet1". By this, the script is run. When you directly run the script, an error like Cannot read property 'range' of undefined occurs. Please be careful about this.
If you want to clear the source range after the values were copied, please use srcRange.clearContent();.
Note:
This sample script is for your sample Spreadsheet. So, when you changed your Spreadsheet, this script might not be able to be used. Please be careful about this.
Reference:
Simple Triggers

Print multiple data per ID in Google Sheet

In this spreadsheet, I have a sheet called Form Responses 1 that contains the students' data and a sheet called TEMPLATE that contains the template that needs to be printed per student. The problem is that I can only print the spreadsheet one at a time by changing the data in the C2 cell. Is there any way for me to print it quickly and easily to save time and effort?
In Google Cloud Print, I want it to say Page 1 = Student 1, Page 2 = Student 2, Page 3 = Student 3, and so on. Is it possible?
In your situation, how about creating a new Google Spreadsheet including each page? When this is reflected in a sample script, it becomes as follows.
Sample script:
function myFunction() {
const srcSs = SpreadsheetApp.getActiveSpreadsheet();
const sheet = srcSs.getSheetByName("TEMPLATE");
const values = sheet.getRange("C2").getDataValidation().getCriteriaValues()[0].getValues().flat().filter(String);
const dstSs = SpreadsheetApp.create("tempSpreadsheet");
values.forEach(v => {
sheet.getRange("C2").setValue(v);
SpreadsheetApp.flush();
const tempSheet = sheet.copyTo(srcSs);
const range = tempSheet.getDataRange();
range.copyTo(range, {contentsOnly: true});
tempSheet.getRange("B2:2").clear().clearDataValidations();
tempSheet.getDrawings().forEach(e => e.remove());
tempSheet.copyTo(dstSs);
srcSs.deleteSheet(tempSheet);
});
dstSs.deleteSheet(dstSs.getSheets()[0]);
}
When this script is run, a new Google Spreadsheet of tempSpreadsheet is created to the root folder. When you see it, each tab has each sheet without using the formulas. By this, you can print out the Spreadsheet by one manual process.
Note:
When I saw your sample Spreadsheet, /** #OnlyCurrentDoc */ is used in your script. When you use my proposed script, please remove /** #OnlyCurrentDoc */. By this, the scope of https://www.googleapis.com/auth/spreadsheets is automatically detected and used it. Please be careful about this.
This sample script is for your sample Spreadsheet. So when the spreadsheet is changed, this script might not be able to be used. Please be careful about this.
References:
create(name)
copyTo(spreadsheet) of Class Sheet
copyTo(destination) Class Range

Can i have a simple script on sheet1 and sheet2?

I have made a script that automatically sorts data when i make a change. It workds fine on the one sheet but i cannot find a way to apply it on sheet2 too.
So i need it to sort both sheet1 and 2 (the sheets are simular so the same sort range and everything needs to be the same - i just need it to apply on sheet2 also-
I have tried to make a copy of the code and change sheet_name to sheet2.
I Insert picture of the code. I would be very happy if you could help.
Explanation:
Take advantage of the event object. This will give you very important information regarding the edit/event which took place.
The idea is to use the script for multiple sheets. One way to do that is to construct an array of the sheet names you want to include:
const sheet_names = ["Management Associate","Finance Associate"];
and check if the name of the active sheet is included in this list:
if(sheet_names.includes(sheet.getName())){
// the rest of the code here
}
As a more complete implementation, I modified the last line of your code to include the name of the sheet that was edited. To do that, I used template literals but of course this approach is optional:
ss.toast(`Sort complete in ${sheet.getName()}`);
Solution:
function onEdit(e) {
const sheet_names = ["Management Associate","Finance Associate"]; // add sheets here
const sort_data_range = "A2:H999";
const sort_order = [{column:6, ascending:true}];
const ss = e.source;
const sheet = ss.getActiveSheet();
if(sheet_names.includes(sheet.getName())){
const range = sheet.getRange(sort_data_range);
range.sort(sort_order);
}
ss.toast(`Sort complete in ${sheet.getName()}`);
}

google apps script - paste in next empty row

with google apps script I am collecting data from the google analytics API.
This is set to collect data every night 1 am for the previous day and writing that in to a sheet “original”
In my second script I want to copy a range A1:G1 from the sheet “original” to my second sheet “copy”
And what I want is continuously to copy the data to the next empty row, but how?
So far I have this as a basic solution, but this is always pasting the selected data into the defined cell (in this case cell A10)
function copytestdata() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName("original");
var sheet2 = ss.getSheetByName("copy");
sheet1.getRange("A1:G1").copyTo(sheet2.getRange("A10"), {contentsOnly:true});
}
I would like a dynamic solution on the second sheet “copy”.
So new data should be pasted in to the next empty row starting from cell A1 and the go down to next empty row.
But how do I get the script to go down to the next empty row?
sheet1.getRange("A1:G1").copyTo(sheet2.[DYNAMIC SOLUTION]), {contentsOnly:true});
Have anyone got a brilliant idea? I would really appreciate the help.
Ohh and by the way how can I actually go to a specific cell sort of like:
sheet1.getActiveRange(“B8”)
Thanks a lot in advance for your help
Pelikan76
why not using getLastRow()+1 ?
function copytestdata() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName("original");
var sheet2 = ss.getSheetByName("copy");
sheet1.getRange("A1:G1").copyTo(sheet2.getRange(sheet2.getLastRow()+1,1,1,7), {contentsOnly:true});
}