I am trying to create a script that will automatically delete the row if the cell contains, but does not exactly match, the condition. For example, the script would delete the rows if the cell contains gmail.com, as part of a larger email
123#gmail.com
123#yahoo.com
456#gmail.com
456#yahoo.com
The two yahoo emails would be saved because they do not meet the condition. However, I am unsure of the proper conditional dictation to use when writing my script. Here is what I have so far.
function deleteRows() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName('Sheet1');
var r = s.getRange('B:B');
var v = r.getValues();
for(var i=v.length-1;i>=0;i--)
if(v[0,i] 'gmail.com')
s.deleteRow(i+1);
};
I would put a ==, but this will only delete rows that match exactly with gmail.com. What would I use in place of the == to make it essentially a text contains condition?
Try this:
function deleteMatchingRows(string) {
var string=string||'gmail.com';
var ss = SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Sheet1');
var lngth=sh.getLastRow();
var rg=sh.getRange(1,2,lngth,1);
var vA=rg.getValues();
var n=0;
for(var i=1;i<vA.length;i++){
if(vA[i][0] && vA[i][0].indexOf(string)>-1){
sh.deleteRow(i-n+1);
n++;
}
}
}
Related
Beware, I may be overthinking this.
I keep getting into cyclic thought loops when I'm trying to figure out what to do in this situation, So I will try to explain my thinking and where I am at.
Form is filled out on Google Sheets
Form replies are added to the main Form sheet in the "form responses tab"
Code actives, checking to see if the form was filled correctly (columns A and B match)
if they match, it finds the respective google spreadsheet ID that that row needs to go to, by looking at the directory tab.
That item is then sent over to the appropriate list, which is in it's own sheet
This continues for the rest of the rows of the Main QA Forms Responses tab, until all rows have been checked and there are no more entries.
I've been trying to understand this for hours on end, but might be approaching this all from the wrong angle.
As of right now, this is how far i've gotten in the code:
function onEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var r = s.getActiveRange();
var columnSearchNum = 3;
var columnDatastarts = "C";
var formSheetName = "QA Form Responses";
var directorySheetName = "Program Directory";
var matchingProgramSheetIDColumn = 1;
if(s.getName() == formSheetName && r.getColumn(0) == r.getColumn(1)) {
var sourceRow = r.getRow();
var matchingProgram = sourceRow.getRange(0,0).getValue();
var matchingProgramSheetID = s.getName(Programdirectory)....//[code needed1]
//^^^^ I need a line here to pull matching the data inSheetID column where Matching program's string is
//from this code line, Go to that 'program's sheet'
var programSheet = ss.getSheetByID(matchingProgramSheetID);
var programSheetNumRows = programSheet.getLastRow();
//console.log(programSheetNumRows);
var formSheetNumColumns = s.getLastColumn();
var targetRange = programSheet.getActiveRange()
var targetValue = +s.getRange(columnDatastarts+sourceRow).getValue()
//console.log(targetValue);
var programSheetRange = programSheet.getRange(1,columnSearchNum,programSheetNumRows,1);
//console.log(programSheetRange.getNumRows() +" " +programSheetRange.getNumColumns() + " " + programSheetRange.getValues());
var targetRow = findIndex(programSheetRange.getValues(), targetValue);
//console.log(targetRow);
var target = programSheet.getRange(targetRow, 1);
s.getRange(sourceRow, 2, 1, formSheetNumColumns).moveTo(target);
;
}
}
function findIndex(array, search){
//console.log(array);
if(search == "") return false;
for (var i=0; i<array.length; i++){
//console.log("comparing " + +array[i] + " to "+ +search);
if (+array[i] == +search){
return i+1;
}
}
return -1;
}
You want to find a match for a value on the Form Responses sheet with values in a given column on another sheet, and then return the value in the cell adjacent to the matching cell.
There are probably many ways to do this, but the Javascript method indexOf is an obvious choice if using a script. The following is untested, but the logic is sound.
Insert at [code needed1]
// define the Program sheet
var progsheet = ss.getSheetByName(directorySheetName)
// define the first row of data on the program sheet
var firstRowofData = 3
// get the data for columns one and two of the program sheet.
// starting row=3, starting column=1, number of rows = lastrowminus first row plus 1, number of columns = 2
var progsheetdata = progsheet.getRange(firstRowofData,1,progsheet.getLastRow()-firstRowofData+1,2).getValues()
// get Program manager Column
var ProgManager = progsheetdata.map(function(e){return e[0];})
// search the Program manager Column for the first instance of the matching program value
// indexOf returns index if found, or -1 if not found
var result =ProgManager.indexOf(matchingProgram);
if (result == -1){
// couldn't find a matching program
// do something to quit/error message
return
}
else
{
// the id will be in the row returned by IndexOf and in the adjacent column to the right.
var id = progsheetdata[result][1]
}
I've been trying to create a Submit sheet that has 42 rows. They have a border around it indicating where I type my data in. My goal is once Submit is typed in at the end of the row it'll paste that row of data into the bottom of my Database sheet
Here is the code ive been writing and having problems with. Sorry if it looks whacky I'm rookie at this
function submit1(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet");
var range = sheet.getDataRange();
var colVal = "Submit";
var colToSearch = 19;
var dataRangeVals = range.getValues();
for(var i = dataRangeVals.length; i <= 173; i++){
if([i][colToSearch] === colVal){
var sourceVal = sheet.getRow(i+[colToSearch]).getValues();
Logger.log(sourceVal);
var targetSheet = ss.getSheetByName("DataBase");
var lastRow = targetSheet.getLastRow();
targetSheet.getRange(lastRow + 1,1,1,18).setValues(sourceVal);
};
};
};
the var dataRangeVals works and gets all the values of my submit sheet, but when I iterate through it and log sourceVal to check if it grabbed the row of data when I typed Submit into Column T
No values show in the execution log which is probably why nothing gets pasted into my DataBase sheet but it still shows execution complete without any errors which is confusing me . The problem is I don't understand why that's happening.
If you guys could help me out and take a look at it i'd greatly appreciate it.
Here is the link if you'd like it.
Stock Database updated link
Issues:
for(var i = dataRangeVals.length; i <= 173; i++){ will start at 131 and no data will be captured. Instead, replace it with for(var i = 0; i < dataRangeVals.length; i++){.
[i][colToSearch] wont return anything. To read the element of an array it should be dataRangeVals[i][colToSearch].
I tried to print the column T of your sheet and the Submit text has extra space in it. if([i][colToSearch] === colVal){ won't work because Submit is not equal to Submit . Instead use String.match().
sheet.getRow(i+[colToSearch]) wont work because Class Sheet has no getRow() method. Replace getRow() with getRange(row, column, numRows, numColumns)
Code:
function submit1(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet");
var range = sheet.getDataRange();
var colVal = "Submit";
var colToSearch = 19;
var dataRangeVals = range.getValues();
for(var i = 0; i < dataRangeVals.length; i++){
if(dataRangeVals[i][colToSearch].toString().match(colVal)){
var sourceVal = sheet.getRange(i+1, 1, 1, 19).getValues();
var targetSheet = ss.getSheetByName("DataBase");
var lastRow = targetSheet.getLastRow();
sourceVal[0].splice(0,1)//remove the empty first element of subarray
targetSheet.getRange(lastRow + 1,1,1,18).setValues(sourceVal);
};
};
};
Output:
I tested your code and found that on your IF condition if([i][colToSearch] === colVal), the [i][colToSearch] has a null or undefined value as seen here on the execution log result. Thus, your IF statement automatically ends the execution of your code and no values are shown.
I did some tweaks on your code to be able to proceed on the IF statement (to find if the last row matches the word "Submit") then copy the last row data from "Sheet" to the "DataBase" sheet's last row.
Please refer to my sample code here: Updated
function submit1(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet");
var range = sheet.getDataRange();
var targetSheet = ss.getSheetByName("DataBase");
var lastRow = targetSheet.getLastRow()+1;
var colVal = "Submit";
var colToSearch = 20;
var dataRangeVals = range.getValues();
for(var i = 1; i <= dataRangeVals.length; i++){//Loop to check column 20 with "Submit" value
if(sheet.getRange(i,colToSearch).getValue().valueOf(colVal)){//Check each rows on column 20 to find ones that has "Submit" value
for(var x = 2; x<=colToSearch-1; x++){//When a row has "Submit" value, it will get all row data and copies it to "DataBase" sheet
var sourceVal = sheet.getRange(i,x).getValues();
targetSheet.getRange(lastRow,x-1).setValues(sourceVal);
}
lastRow = targetSheet.getLastRow()+1;//Refreshes the new last row on "DataBase" sheet
Logger.log("Done copying row data on \"Sheet\" Row #" + i + " to \"DataBase\"");
}else{
//Do nothing
}
}
}
After running the code, you will be able to see all of the data from the row on "Sheet", copied and added on every last row of the "Database" Sheet. You Should this sample execution log result once it is successful.
Updated Result
Here's my test spreadsheet, based on your spreadsheet design:
Here's the result on DataBase sheet:
I have two spreadsheets with different types of data. The only similarity is a list of names spread out across the spreadsheets.
I want to create a script that lets me copy specific rows from Spreadsheet 2 to Spreadsheet 1 with two conditions:
Only rows marked Ready in Spreadsheet 2 should be copied over to Spreadsheet 1
No rows containing names already in Spreadsheet 1 should be copied over from Spreadsheet 2
I'm probably missing something simple, but I can't get Point 2 to work.
function CopyClick() {
var sheet = SpreadsheetApp.getActiveSheet();
var originalnames = sheet.getRange("A1:A").getValues();
var otherss = SpreadsheetApp.openById("Spreadsheet 2 ID HERE");
var copysheet = otherss.getSheetByName("Spreadsheet 2 Sheet Name");
var copynames = copysheet.getRange("C1:C").getValues();
var statusdata = copysheet.getRange("D1:D").getValues(); //First condition has to be "Ready"
var copylastrow = copysheet.getLastRow();
for(var i=0; i<copylastrow; i++) {
if(statusdata[i][0].toString().match(/Ready/ig) && (copynames[i][0].indexOf(originalnames) === -1)){ //Don't want duplicates, but indexOf isn't working
var lastRow = sheet.getLastRow();
sheet.getRange(lastRow+1, 1).setValue(copynames[i][0]); //back to the original spreadsheet
}
}
}
As you can see, I wasn't sure of how to keep the second condition, ending up with a placeholder indexOf.
How could I best solve this?
Explanation:
You can take advantage of the includes() function. It returns true if there is a match. However, you need to flat() the originalnames array first. I also changed the range of originalnames to avoid taking empty cells:
var originalnames = sheet.getRange("A1:A"+sheet.getLastRow()).getValues().flat(1);
and inside the if condition use:
!originalnames.includes(copynames[i][0])
this will evaluate to true if copynames[i][0] is not included in originalnames array. I also replaced statusdata[i][0].toString().match(/Ready/ig) to statusdata[i][0] == 'Ready' but feel free to use your own version if this does not work.
Solution:
function CopyClick() {
var sheet = SpreadsheetApp.getActiveSheet();
var originalnames = sheet.getRange("A1:A"+sheet.getLastRow()).getValues().flat(1);
var otherss = SpreadsheetApp.openById("Spreadsheet 2 ID HERE");
var copysheet = otherss.getSheetByName("Spreadsheet 2 Sheet Name");
var copynames = copysheet.getRange("C1:C").getValues();
var statusdata = copysheet.getRange("D1:D").getValues(); //First condition has to be "Ready"
var copylastrow = copysheet.getLastRow();
for(var i=0; i<copylastrow; i++) {
if( statusdata[i][0] == 'Ready' && !originalnames.includes(copynames[i][0]) ){
var lastRow = sheet.getLastRow();
sheet.getRange(lastRow+1, 1).setValue(copynames[i][0]); //back to the original spreadsheet
}
}
}
The challenge has been getting cell values from the document header together with a range underneath this header to Sheet2, having it as a table, so that later, I can filter, update and delete records via scripts.
So far, I got the below code copying what's within the range underneath the header:
function copyrange() {
var sourceSheet = 'NewItem';
var destinationSheet = 'DW';
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sourceSheet);
var LastRowSource = sheet.getLastRow();
var LastColumnSource = sheet.getLastColumn();
var values = sheet.getRange(11,1,LastRowSource,LastColumnSource).getValues();
var csh = ss.getSheetByName(destinationSheet);
var data = [];
var j =[];
//var itemheader = [sheet.getRange("C4:4").getValues(),sheet.getRange("A3:3").getValues(),sheet.getRange("i3:3").getValues(),sheet.getRange("i4:4").getValues(),sheet.getRange("c5:5").getValues(),sheet.getRange("e5:e").getValues(),sheet.getRange("i5:5").getValues(),
// sheet.getRange("C7:7").getValues(),sheet.getRange("c8:8").getValues(),sheet.getRange("c9:9").getValues(),sheet.getRange("e7:7").getValues(),sheet.getRange("e8:8").getValues(),sheet.getRange("i7:7").getValues(),sheet.getRange("i8:8").getValues(),
// sheet.getRange("e9:9").getValues()];
for (var i = 1; i < values.length; i++) {
if ( values[i][0] != '') {
data.push(values[i]);
//sheet.deleteRow(i+1)
}
}
//Copy data array to destination sheet
csh.getRange(csh.getLastRow()+1,17,data.length,data[0].length).setValues(data);
Now, getting different cell values from the header into a row and copying it to Sheet2 as many times/rows as there is in the range copied by the above script seems impossible to me.
Here is a link to a practical example:
https://docs.google.com/spreadsheets/d/1GlD_VIOFHj7PGfCCnfVZqdZbE22aVoUw91BSCZecsRc/edit?usp=sharing
Thanks a lot for your help. Believe me, I've hit my head against the wall enough with this one before I turn to you guys!
Following a previous question
I want to classify text entries by adding a tag in the next column.
I could do it using regex but it will take too much time writing all conditions like :
if(String(data[i][0]).match(/acme|brooshire|dillons|target|heb|costco/gi))
{
labValues[i][0]='Supermarket';
}
Instead I created a named list with all stores names (in another sheet).
If an entry matches a term in the list, the next column is set to "Supermarket".
I am using this script below... No bugs but nothing happens when executed !
function tagStore() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange('A2:A655')
var store = range.getValues();
var tag = sheet.getRange('B2:B655');
var tagvalues= tag.getValues();
var storeList= SpreadsheetApp.getActive().getRangeByName("store_list");
for (var i = 0; i<store.length; i++)
{
if(String(store[i][0]).match(storeList))
{
tagvalues[i][0]='Supermarket';
}
}
tag.setValues(tagvalues);
}
Edit:
It is important to use a Regex as the "store" Values are not exactly the same as the "store_list".
Store Values : ["Acme Store", "HEB PLaza", "Dillons Group"...]
Store_List : [acme, heb, dillons...]
Instead of trying to go with the regEx approach there is a more straightforward approach by retrieving the range as a list.
// For a Column
var storeList = SpreadsheetApp.getActive().getRangeByName("store_list").getValues().map(function(r){return r[0];});
// For a Row
var storeList = SpreadsheetApp.getActive().getRangeByName("store_list").getValues()[0];
And then look if the values you are looking for are in this list with indexOf().
Try this:
function tagStore() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange('A2:A655')
var store = range.getValues();
var tag = sheet.getRange('B2:B655');
var tagvalues= tag.getValues();
var storeList= SpreadsheetApp.getActive().getRangeByName("store_list").getValues().map(function(r){return r[0];});//if it's a column
//var storeList=SpreadsheetApp.getActive().getRangeByName("store_list").getValues()[0];//if it's a row
for (var i=0;i<store.length; i++) {
if(storeList.indexOf(store[i][0])!=-1) {
tagvalues[i][0]='Supermarket';
}
}
tag.setValues(tagvalues);
}