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.
Related
I have the script below that only works for the first condition. I can switch the conditions order and then the top one will work but not the bottom. Any ideas why?
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Master" && r.getColumn() == 16 && r.getValue() == "Yes") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Complete");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Master" && r.getColumn() == 16 && r.getValue() == "No") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Late");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
You can only have one active onEdit(e) simple trigger in a script project. To move rows based on two sets of criteria, you will need to combine the two functions into one.
I would recommend that you use a tried and tested solution such as the moveRowsFromSpreadsheetToSpreadsheet_ script. The code is almost 800 lines long so it is impractical to quote here, but it is simple enough to configure using the parameters section.
I have managed to remove a row and place in a sheet within the same Google spreadsheet. However when I modify the script to copy a row onto a different sheet, nothing happens:
function onEdit(event) {
var ss =SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "CONTRACT REGISTRY DOCUMENT" && r.getColumn() == 58 && r.getValue() == "X") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var destination = SpreadsheetApp.openById('...');
var targetSheet = destination.getSheetByName("NOT RENEWED");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
The moveTo method can only move data within the same spreadsheet. It is best practice to use range.getValues() and range.setValues() to achieve the same behavior in different spreadsheets.
function onEdit(event) {
var ss =SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "CONTRACT REGISTRY DOCUMENT" && r.getColumn() == 58 && r.getValue() == "X") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var data = r.getValues();
// please supply the destination file ID as parameter here
var destination = SpreadsheetApp.openById('...');
var targetSheet = destination.getSheetByName("NOT RENEWED");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
target.setValues(data);
s.deleteRow(row);
}
}
It seems the problem here is that you're trying to access another file from your trigger (onEdit), but that is not possible because it'd require permission to change another spreadsheet: https://developers.google.com/apps-script/guides/triggers#restrictions
According to a related question, an installable trigger is the way to go here.
In this case, you need to add something like this to your code and run it to create the trigger:
function createSpreadsheetOnEditTrigger() {
var ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger('moveRowToAnotherSheet')
.forSpreadsheet(ss)
.onEdit()
.create();
}
Based on this trigger, it is necessary to make a few changes to the original code you posted:
function moveRowToAnotherSheet() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet(); // Changed
var r = s.getActiveRange(); // Changed
if(s.getName() == "CONTRACT REGISTRY DOCUMENT" && r.getColumn() == 58 && r.getValue() == "X") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var destination = SpreadsheetApp.openById('...');
var targetSheet = destination.getSheetByName("NOT RENEWED");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1, 1, targetSheet.getLastColumn()); // Changed
target.setValues(s.getRange(row, 1, 1, numColumns).getValues()); // Added to use setValues instead of moveTo
s.deleteRow(row);
}
}
I have 2 different scripts, the first one copies the row when I select the name of the sheet from the drop down list and pastes it on the sheet with that name, the second script is a checkbox that when ticked then copies the entire row to another sheet and deletes it from the original sheet. So after I have first selected the option from the dropdown menu and then ticked the checkbox that row should be copied to 2 different sheets.
My trouble is that although both these scripts work when they're on their own, they don't work when I have them both.
Script 1
function onEdit(e) {
var sh=e.range.getSheet();
if(sh.getName()=='Approval' && e.range.columnStart==11) {
var v=sh.getRange(e.range.rowStart,1,1,sh.getLastColumn()).getValues();
var tsh=e.source.getSheetByName(e.value);
if(tsh) {
tsh.getRange(tsh.getLastRow() + 1,1,1,v[0].length).setValues(v);
}
}
}
Script 2
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Approval" && r.getColumn() == 12 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("ZSD Sheet");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
I've looked at script merging but it's going waaaaaaay over my head so any help is hugely appreciated.
You can't have two or more onEdit() triggers. But you can do the following simple workaround. Add the following function to your scripts:
function onEdit(e) {
onEdit1(e)
onEdit2(e)
}
and then, modify your scripts as follows:
Script 1:
function onEdit1(e) {
var sh=e.range.getSheet();
if(sh.getName()=='Approval' && e.range.columnStart==11) {
var v=sh.getRange(e.range.rowStart,1,1,sh.getLastColumn()).getValues();
var tsh=e.source.getSheetByName(e.value);
if(tsh) {
tsh.getRange(tsh.getLastRow() + 1,1,1,v[0].length).setValues(v);
}
}
}
Script 2:
function onEdit2(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Approval" && r.getColumn() == 12 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("ZSD Sheet");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
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);
}
}
I previously used this google script below to move information from one sheet to another sheet based on conditional values in one column (column#12). However, it just stopped responding/running recently and I cannot figure out why. I played with the triggers, I tried to use it in a new script and changing up some of the names and column#, but nothing seems to work. Any suggestions on where I can look to find out what's going on?
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var r = ss.getActiveRange();
if(s.getName() == "Phx-DFF" && r.getColumn() == 12 && r.getValue() == "no")
{
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("NY-DFF");
if(targetSheet.getLastRow() == targetSheet.getMaxRows())
{
targetSheet.insertRowsAfter(targetSheet.getLastRow(), 5); //inserts 20 rows after last used row
}
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 14, numColumns).moveTo(target);
s.deleteRow(row);
}
}
Have you tried to use logging or running the function in debug mode to see what exacly is happening when you run this function.
I want to suggest the follwing changes to the function as a lot of information is provided by the eventinfo JSON:
function onEdit(e) {
var ss = e.source;
var r = e.Range;
if(ss.getSheetName() == "Phx-DFF" && r.getColumn() == 12 && e.value == "no")
{
var s = ss.getActiveSheet();
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("NY-DFF");
if(targetSheet.getLastRow() == targetSheet.getMaxRows())
{
targetSheet.insertRowsAfter(targetSheet.getLastRow(), 5); //inserts 20 rows after last used row
}//I dont think this is necessary, GAS should takes care of this auto. Remove this part
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 14, numColumns).moveTo(target);
s.deleteRow(row);
}
}