Google sheets move row depending on value AND edit values - google-apps-script

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

Related

In Google Sheets, how can I copy a row based on a specific column value into a new tab, and then append checkboxes to the row?

I'm trying to write a script which would allow me to copy a row into a new tab based on a specific cell value. In this case, it is a checkbox of being set to TRUE.
I have no issue with copying the row to a new sheet based on the cell value, but now I'm unsure as to how I can use insertCheckboxes to append 5 checkboxes to the copied row in the new sheet.
This is the code I have at the moment:
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Client Database" && r.getColumn() == 9 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Referrals");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
}
}
Column 9 contains checkboxes which when set to true, copies the row from "Client Database" into my new tab ("Referrals").
I'm scratching my head over two things:
What do I need to change in the code to only copy columns 1 and 4 from the original sheet? (as it is currently copying all columns from the row).
Unless there is an easier workaround, how can I append checkboxes into the adjacent columns within the new sheet using insertCheckboxes? (Illustration below). This is simply to avoid having to manually insert the boxes each time (as I will not be the one using the sheet)
Any advice is greatly appreciated.
Checkboxes
Add to your script
var checkbox = SpreadsheetApp.newDataValidation().requireCheckbox().build();
targetSheet.getRange(targetRow,3,1,4).setDataValidation(checkbox).setValue("FALSE");
where targetRow is the row and targetSheet the sheet where you copy
code
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if (s.getName() == "Client Database" && r.getColumn() == 9 && r.getValue() == true) {
var row = r.getRow();
var targetSheet = ss.getSheetByName("Referrals");
targetSheet.insertRowBefore(2)
var targetRow = 2
targetSheet.getRange(targetRow,1).setValue(s.getRange(row,1).getValue())
targetSheet.getRange(targetRow,2).setValue(s.getRange(row,4).getValue())
var checkbox = SpreadsheetApp.newDataValidation().requireCheckbox().build();
targetSheet.getRange(targetRow,3,1,4).setDataValidation(checkbox).setValue("FALSE");
}
}

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 data from one tab to another, and paste data in the last row ignoring formulas

The goal is once I check a box in tab 1, it will copy information from rows 21 to 29 onto Tab 2. Tab 2 it will find the first row in which column A has a blank value and paste the information there. LastRow() does not work as I have formulas in columns J-K in Tab 2. I am guessing I have the wrong format or order of IF statements.
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
var targetSheet = ss.getSheetByName("Tab 2");
var firstCell = targetSheet.getRange("A1");
if(!firstCell.isBlank()){
var firstEmptyRow = firstCell.getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow() + 1;
} else{
var firstEmptyRow = firstCell.getRow();
}
if(s.getName() == "Tab 1" && r.getColumn() == 18 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var target = targetSheet.getRange(firstCell);
s.getRange(row, 21, 1, numColumns).copyTo(target,{contentsOnly:true});
You want to find the first blank cell in column A
A useful method to do so is getNextDataCell().
This method finds the edge between data containing and empty cells going along the specified direction.
Sample:
var firstCell = targetSheet.getRange("A1");
if(!firstCell.isBlank()){
var firstEmptyRow = firstCell.getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow() + 1;
} else{
var firstEmptyRow = firstCell.getRow();
}
Note:
isBlank() on its own can also be used to find the first empty cell, but in your case it might not be the most elegant method.

Copy over a specific range not whole row to another google sheet based on cell value

I would like to have select cells of a row copied from "Pending" sheet to "Live" sheet based on when a specific cell is edited to say "Yes".
So far I'm able to copy the whole row over when a specific cell in a column is edited to say "Yes".
I would like the specific cells in that row to copy and move onEdit, but not the whole row. For example copy, the contents of column 2,3,5,6 but not 4,7,8 etc. of the row edited in the "Pending" sheet over to the "Live" sheet.
Also though, when copying over to the "Live" sheet having column 2 of the "Pending" sheet line up with column 2 of the "Live" Sheet etc. But for column 5 and 6, they should both move over one column since column 4 wasn't copied over. So "Pending" 5 should take the place of "Live" 4 and "Pending" 6 should take the place of "Live" 5.
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Pending" && r.getColumn() == 1 && r.getValue() == "Yes") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Live");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
}
}
Try This:
function onEdit(e)
{
var ss=e.source;
var sh=ss.getActiveSheet();
var rg=sh.getActiveRange();
var row=rg.getRow();
var col=rg.getColumn();
Logger.log(col);
if(sh.getName()=="Pending" && col==1 && rg.getValue()=="Yes")
{
var row = rg.getRow();
var liveSht = ss.getSheetByName("Live");
var vA=sh.getRange(row,col,1,6).getValues();
var liveA=['',vA[0][1],vA[0][2],vA[0][4],vA[0][5]];
liveSht.appendRow(liveA);
}
}

Copying row to another google sheet by ID - NOT WORKING

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