I'm having issues figuring out how to move rows if a cell contains X word. I'm trying to manage SKUs and I want all of the ones that start with "lep-" to move sheets.
They should all start with "lep" but have different numbers after ex. lep-4-35-2-24x30 , lep-3-28-3-16x20 , etc.
function onEdit(event) {
// assumes source data in sheet named SHOPIFY
// target sheet of move to named ORG
// test column with yes is col 17 or Q
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "SHOPIFY" && r.getColumn() == 17 && r.getValue() == "lep") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("ORG");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
Solution:
You can use includes() method as this method determines whether a string contains the characters of a specified string:
Instead of using r.getValue() == "lep", use r.getValue().includes("lep") in your if statement. I've tested your code with includes() method and it worked on my end.
Related
This question already has an answer here:
Merging or Combining two onEdit trigger functions
(1 answer)
Closed 1 year ago.
Can someone point me in the right direction please?
I'm attempting to run two onEdit() which both send a range of cells to an archived sheet depending on whether a checkbox is ticked.
My code results in the entire row being moved and deleted rather than the specified cell range irregardless of which of the two checkboxes is selected as TRUE.
function onEdit(event) {
onEdit1();
onEdit2();
function onEdit1() {
// assumes source data in sheet named Active
// target sheet of move to named Found
// test column with yes/no is col 31
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "WORKSHOP" && r.getColumn() == 31 && r.getValue() == true) {
var row = r.getRow();
var numColumns = 11
var targetSheet = ss.getSheetByName("ARCHIVE-DEFECTS");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 23, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
function onEdit2() {
// assumes source data in sheet named Active
// target sheet of move to named Found
// test column with yes/no is col 10
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "WORKSHOP" && r.getColumn() == 10 && r.getValue() == true) {
var row = r.getRow();
var numColumns = 12
var targetSheet = ss.getSheetByName("ARCHIVE-INSP");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
}
Apologies I'm not very knowledgeable and am kind of learning by doing by readin threads in the forum. Cheers.
Try this
function onEdit(event) {
onEdit1(event);
onEdit2(event);
}
function onEdit1(event) {
_________
}
function onEdit2(event) {
_________
}
it seems that there is a lack og event parameter
I have a script that moves the whole row to a different tab and deletes it from the tab it was moved based on a "list" selection.
The script is the following:
// Script Move & Delete
function onEdit(event) {
// assumes source data in sheet named Needed
// target sheet of move to named Acquired
// test column with yes/no is col 4 or D
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
// In Review Trigger
if(s.getName() == "Applied" && r.getColumn() == 1 && r.getValue() == "In Review") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("In Review");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
// In Review Trigger
else if(s.getName() == "Applied" && r.getColumn() == 1 && r.getValue() == "Failed") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Failed");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
So for example, if the row on Tab "Applied" and value of cell on column 1 = In Review then the row is moved to tab "In Review" but if the value of cell on column one = to Failed then row is moved to tab Failed.
That part works like charm and I know having multiple tabs will have a lot of else ifs, I'm not much of a dev, so I was wondering how can I do to move the row if the status (Column 1) is changed to Applied is moved to tab one or failed tab if value is changed to failed?
Basically, move the rows between tabs based on value of column one of each row/tab.
Thanks in advance!
This worked for me:
function onMyEdit(e) {
//e.source.toast('entry');
const s = e.range.getSheet();
if (s.getName() == "Sheet1" && e.range.columnStart == 1 && e.value) {
//e.source.toast('cond')
const names = ['Sheet2', 'Sheet3'];
let idx = names.indexOf(e.value);
if (~idx) {
//e.source.toast('sheet');
let tsh = e.source.getSheetByName(names[idx]);
let tgt = tsh.getRange(tsh.getLastRow() + 1, 1);
s.getRange(e.range.rowStart, 1, 1, s.getLastColumn()).moveTo(tgt);
s.deleteRow(e.range.rowStart);
}
}
}
I made some name changes to make it easier for me to debug so look it over to make sure all of the sheet names are right for your needs.
If you require more complex functionality in moving rows, take a look at the moveRowsFromSpreadsheetToSpreadsheet_ script. It is unfortunately too long to quote here directly.
I am trying to move a row from sheet "Loads" to sheet "Invoiced Loads" based on multiple criteria. The criteria is:
If the load has been Delivered and Invoiced (which is a number greater than 0) move the load/row to sheet "Delivered".
I have seen many people have asked this question and I found a code that works, but only with one criteria. I have tried to modify the code so it checks the two conditions but I can not make it work. I ran out of options. Please if someone can help me I will really appreciate it. I know the solution must be really simple but I am a beginner on this and I am not sure which path to take please!!. The code I got at the moment is the below:
function onEdit(event) {
// assumes source data in sheet named Loads
// target sheet of move to named Invoiced Loads
// test column with "Delivered" is, col 19 or S
// test column with invoice number is, col 27 or AA
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if((s.getName() == "Loads" && r.getColumn() == 19 && r.getValue() == "Delivered") &&
(s.getName() == "Loads" && r.getColumn() == 27 && r.getValue() > 0)) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Invoiced Loads");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
Explanation:
The main issue with the if statement is that r.getColumn() can't be
19 and 27 at the same time but also r.getValue() can't be
"Delivered" and >0 at the same time. As a result, the if
condition will always evaluate to false.
Also you don't need to evaluate twice for the same condition. The if
condition in your code evaluates twice the expression s.getName() == "Loads" which is unnecessary. One is enough.
This is an onEdit function which means that every time you make a
change, event.range.getRow() and event.range.getColumn() will
give you the coordinates of the cell that was edited. There is no
need of using event.source.getActiveRange();.
Solution:
function onEdit(event) {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const row = event.range.getRow();
const col = event.range.getColumn();
const as = event.source.getActiveSheet();
const numColumns = as.getLastColumn();
const targetSheet = ss.getSheetByName("Invoiced Loads");
const target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
if(as.getName() == "Loads" && as.getRange(row,19).getValue() == "Delivered"
&& as.getRange(row,27).getValue() >0) {
as.getRange(row, 1, 1, numColumns).moveTo(target);
as.deleteRow(row);
}
}
I am super novice and wanted to get someone's input on how to make this work. I have been looking around and this has been the main topic. Which would send an ENTIRE row to another tab as the destination tab. But how to do you specify only to have one cell's information to the next tab. For example, I have names on Column A and I would only like to send the name and none of the information after that to the next google sheet tab once I get the value true.
function onEdit(event) {
// assumes source data in sheet named main
// target sheet of move to named Completed
// getColumn with check-boxes is currently set to column 4 or D
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Interviewer Training" && r.getColumn() == 16 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("General IC SWE");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
} else if(s.getName() == "General IC SWE" && r.getColumn() == 16 && r.getValue() == false) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Interviewer Training");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1;
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
To move a single cell value from the current sheet to the next sheet.
function onEdit(e) {
//e.source.toast('flag1');
var sh=e.range.getSheet();
var idx=sh.getIndex();
var tsh=nextSheet(e);
//e.source.toast('Next Sheet: ' + tsh.getName());
if(sh.getName()!="Sheet1")return;
if(e.range.columnStart==1 && e.value=='TRUE' && !e.range.offset(0,1).isBlank() ) {
//e.source.toast('flag2');
tsh.getRange(e.range.rowStart,e.range.columnStart + 1).setValue(e.range.offset(0,1).getValue());
}
}
function nextSheet(e) {
//e.source.toast('flag3');
var sh=e.range.getSheet();
var shts=e.source.getSheets();
return shts[(sh.getIndex())%shts.length];
}
This is my sheet 1:
This is the next sheet:
I'm using the click of a checkbox to trigger the onEdit() function to make the transfer of the data immediately to the right of the selected check box and I move it to the same cell in the next sheet. The next sheet is the sheet immediately to the right or if it's the last sheet on the right it moves to the first sheet.
Sheet.getIndex()
Remainder or Modulo Operator
To move just a single cell you would only need to change your s.getRange()
s.getRange(row, 1, 1, numColumns).moveTo(target);
To get cell in row X column B
s.getRange(row, 2, 1, 1).moveTo(target);
The get range uses these parameters;
.getRange(Row Number, Column Number, Number of Rows, Number of Columns)
You only need to edit Column Number and Number of Columns
So row X column C & D would be
s.getRange(row, 3, 1, 2).moveTo(target);
I've been reading answers on Stack for ages and finally decided to join! So here is my first question:
I found a google apps script here for google sheets that moves a row to another sheet depending on a cell value. This is great. I have tested it and it is working for me with a couple of alterations - first to use a checkbox, and second to copy all columns excluding the 1st column with the checkbox.
However, for it to be really useful to me, what I'd like is to be able to edit a couple of values before adding the row to the new sheet.
So it would be like this:
if Checkbox = true, then copy row, all columns excluding checkbox
Set value of 'location' to 'London' (column 8 for example)
Set value of 'date' to today (column 12 for example)
Add edited row to new sheet at the bottom
Can anyone help me to achieve this? I'm guessing it must be quite simple but I am new to apps script so don't know how I'd do it.
Here is the basic script for moving the row that I found from a 2011 post here:
function onEdit(event) {
// assumes source data in sheet named Needed
// target sheet of move to named Acquired
// test column with checkbox is col 1 or A
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Needed" && r.getColumn() == 1 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Acquired");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 2, 1, numColumns).copyTo(target);
}
}
Hope someone can help. Thanks in advance!
Try this code, please:
function onEdit(event) {
// assumes source data in sheet named Needed
// target sheet of move to named Acquired
// test column with checkbox is col 1 or A
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Needed" && r.getColumn() == 1 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn() - 1;
var data = s.getRange(row, 2, 1, numColumns).getValues();
data[0][8] = 'London';
data[0][12] = new Date();
var targetSheet = ss.getSheetByName("Acquired");
targetSheet.getRange(targetSheet.getLastRow() + 1, 1, 1, numColumns).setValues(data);
}
}
Please, try this and let me know how you go. :)
function onEdit(event) {
//assumes source data in sheet named Needed
//target sheet of move to named Acquired
//test column with checkbox is col 1 or A
var spreadsheet = event.source;
var sheet = spreadsheet .getActiveSheet();
var range = spreadsheet.getActiveRange();
var sheetName = sheet.getName();
var col = range.getColumn();
var value = range.getValue();
if (sheetName === "Needed" && col === 1 && value === true) {
var row = range.getRow();
var numColumns = sheet.getLastColumn();
var targetSheet = spreadsheet.getSheetByName("Acquired");
var lastRow = targetSheet.getLastRow();
var target = targetSheet.getRange(lastRow + 1, 1, 1, numColumns - 1);
//added number of rows and columns to select a range instead of a single cell
var values = sheet.getRange(row, 2, 1, numColumns - 1).getValues();
values[0][7] = values [0][1];
//access value of row 1, column 8 and set to value of row 1, column 2
//the values are captured in an array and are zero-based. The first column is column 0 for example.
values[0][11] = new Date();
//access value of row 1, column 12 and set to date-time value of 'now'.
target.setValues(values);
}
}