Copying row to another google sheet by ID - NOT WORKING - google-apps-script

Im trying to copy rows with the word "lead" from the 8th column to another spreadsheet with different ID and I am not getting any results at all. This is what I was able to do so far. I can not find the error of the code. Can you guys help me find it.
function onEdit(event) {
// assumes source data in sheet named Needed
// target sheet of move to named Acquired
// test column with lead is col 8 or H
// target sheet Id is 1nMpOJrq79UZPI3T9ykc415pAHIODg2he5xqi-DY6Wt0
// sheet name of target Id is Sheet1
var ss = SpreadsheetApp.openById("1nMpOJrq79UZPI3T9ykc415pAHIODg2he5xqi-DY6Wt0");
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "List" && r.getColumn() == 8 && r.getValue() == "lead") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Sheet1");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
}
}

Use range.setValues() instead of copyTo.
Note that the format is not copied, only the values.
I changed it that the dimensions of the range match that of the target range.
function onEdit(event) {
// assumes source data in sheet named Needed
// target sheet of move to named Acquired
// test column with lead is col 8 or H
// target sheet Id is 1nMpOJrq79UZPI3T9ykc415pAHIODg2he5xqi-DY6Wt0
// sheet name of target Id is Sheet1
var ss = SpreadsheetApp.openById("1nMpOJrq79UZPI3T9ykc415pAHIODg2he5xqi-DY6Wt0");
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "List" && r.getColumn() == 8 && r.getValue() == "lead") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Sheet1");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1, 1, numColumns);
target.setValues(s.getRange(row, 1, 1, numColumns).getValues());
}
}

Related

Move row to another sheet's first row base on cell value

My code working good to move row to another sheet base on Column9's value.
But it moving the row to the last row of target sheet.
Is there anyway to move the row and insert to the second row of the target sheet ?
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();
if(s.getName() == "TODO" && r.getColumn() == 9 && r.getValue() == "Hoàn thành") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("DONE");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("TestingTesting");
var lastCol = sheet.getLastColumn();
// get the data from source location, in this case row 15 starting with column1, only one row and all columns. rowVal is an array with the values from your source.
let rowVal = sheet.getRange(15, 1, 1, lastCol).getValues();
// this is the empty row you wanted to be in second line
sheet.insertRows(2);
// this is adding data you acquired from source into that new row
sheet.getRange(2,1,1,lastCol).setValues(rowVal);

Move a range of cells from a row to another google sheet and delete it

I got the following code working but not as I would like to.
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Listed" && r.getColumn() == 21 && r.getValue() == "yes") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Sold");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
I have 2 sheets, one for Items listed, and one for items sold. I need to delete a row in "Listed" when the column 21 says "yes" and paste it in my "Sold" sheet.
Is there a way to copy and paste only a range of cells from "Listed" to "Sold" but to delete the whole row ?
Explanation:
Your goal is to copy only a subset of the edit row and paste it to
the destination sheet and then delete that row in the source sheet.
The following script will copy only the first 10 columns of that row. Adjust const numColumns = 10 to your needs.
Solution:
function onEdit(e) {
const ss = e.source;
const s = e.source.getActiveSheet();
const row = e.range.getRow();
const col = e.range.getColumn();
const targetSheet = ss.getSheetByName("Sold");
const numColumns = 10;
if(s.getName() == "Listed" && col == 21 && e.range.getValue() == "yes") {
const s_range = s.getRange(row,1,1,numColumns);
const s_values = s_range.getValues();
s.deleteRow(row);
targetSheet.getRange(targetSheet.getLastRow() + 1, 1,s_values.length,s_values[0].length).setValues(s_values);
}
}

Automatically Copy Rows to Another Sheet Based on Cell Value

I'm pretty new to scripts so I apologize for my potential errors in explaining my issue. I'm working on a spreadsheet that will copy a row to another sheet when it meets a certain criteria (or a certain value in a column) and a second function which will move the row to another sheet upon meeting a certain criteria.
Using related questions/answers, I was able to write a script that deletes a row upon meeting criteria "Outbound" in column 4, see myFunction1 below. For myFunction2, I'm trying to instead just copy the row to another sheet when it meets criteria "M" in column 15, but when I enter that value in column 15 it copies the wrong row information.
Hopefully that makes sense! Code below:
function onEdit(event) {
myFunction1(event);
myFunction2(event);
}
function myFunction1(event){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Trailers in Yard" && r.getColumn() == 4 && r.getValue() == "Outbound") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Outbound");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
function myFunction2(event){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Trailers in Yard" && r.getColumn() == 15 && r.getValue() == "M") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Moves");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
}
}
Appreciate the assistance!
Try it this way:
function onEdit(e) {
//myFunction1(e);
myFunction2(e);
}
function myFunction2(e){
//e.source.toast('Flag1');
var sh=e.range.getSheet();
if(sh.getName()=="Sheet2" && e.range.columnStart==15 && e.range.getValue()=="M") {
//e.source.toast('Flag2');
var targetSheet=e.source.getSheetByName("Sheet1");
var target=targetSheet.getRange(targetSheet.getLastRow()+1,1);
sh.getRange(e.range.rowStart,1,1,sh.getLastColumn()).copyTo(target);
}
}
You'll have to change the sheet names.

Move columns to another sheet based on the cell value in Google Sheets

I'm looking to move a column to another sheet based on the cell value in a given row. I was able to do this for rows but not columns.
function onEdit(event) {
// assumes source data in sheet named CURRENT
// target sheet of move to named COMPLETE
// test column with yes/no is col 1 or A
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Goals 3-6 Months" && r.getColumn() == 4 &&
r.getValue() == "Done") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Goals Done");
if(targetSheet.getLastRow() == targetSheet.getMaxRows()) {
targetSheet.insertRowsAfter(targetSheet.getLastRow(), 20); //inserts 20
rows after last used row
}
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}

Google sheets move row depending on value AND edit values

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);
}
}