I have two Sheets that I will be moving Data between frequently. On Sheet1, I have all my data and checkboxes are used to send that same data to Sheet2. When the data is sent over, empty rows are left behind so I know what positions are available when I rebuild the sheet at the end of the day.
The data on Sheet2 will be modified and at the end of the day I'd like to move everything back to Sheet1 taking the first available empty row. I have the first part working fine, but I cannot seem to get the data to move back to Sheet1 into the first available (empty Cell).
Currently the issue is when I uncheck the box to give the row a value of false, the data is removed from Sheet2 but not moved into Sheet1.
Here is what I have so far:
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Foss Test 2" && r.getColumn() == 4 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Foss Test");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
} else if(s.getName() == "Foss Test" && r.getColumn() == 4 && r.getValue() == false) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Foss Test 2");
var target = targetSheet.getRange(targetSheet.getDataRange().getHeight() + 1, 1)
targetSheet.appendRow(target.getValues()[0]);
s.deleteRow(row);
}
}
I feel like my error is in the else block as currently no data is moved back to Sheet1.
Although you didn't define the term "source" I assume this is what you want.
function onEdit(e) {
e.source.toast('flag0');
var sh=e.range.getSheet();
if(sh.getName()=="Sheet1" && e.range.columnStart==4 && e.value=="TRUE") {
var targetSheet=e.source.getSheetByName("Sheet2");
var target=targetSheet.getRange(targetSheet.getLastRow() + 1,1);
sh.getRange(e.range.rowStart,1,1,sh.getLastColumn()).moveTo(target);
} else if(sh.getName()=="Sheet2" && e.range.columnStart==4 && e.value=="FALSE") {
var targetSheet=e.source.getSheetByName("Sheet1");
var vA=targetSheet.getDataRange().getValues();
for(var i=0;i<vA.length;i++) {
if(vA[i].join('').length==0) {
var target=targetSheet.getRange(i+1,1);
sh.getRange(e.range.rowStart,1,1,sh.getLastColumn()).moveTo(target);
break;
}
}
}
}
If that's not what you want please let me know.
Related
I'm new to the world of javascript & google apps script and I'm struggling to get a row of data to move from one spreadsheet to another. I have it moving a row from sheet to sheet within the same spreadsheet but not across spreadsheets.
I know I need to use SpreadsheetApp.openById("insertID") rather than SpreadsheetApp.getActiveSpreadsheet() but I can't seem to make it work.
Here is the code I have that's working based on moving sheet to sheet, how do I modify it to move to a new spreadsheet?
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Timeline Submit" && r.getColumn() == 117 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("WIP");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target, {contentsOnly:true});
} else if(s.getName() == "WIP" && r.getColumn() == 117 && r.getValue() == false) {
var row = r.getRow();
var numColumns = s.getColumn();
var targetSheet = ss.getSheetByName("Timeline Submit");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target, {contentsOnly:true});
}
}
I couldn't figure out everything in the second section of your code but it had too many errors so I just guessed and some of it.
Don't forget to go create a trigger for this in the Script Editor Edits/Current Project Triggers.
function installableOnEdit(e) {
var sh=e.range.getSheet();
if(sh.getName()=="Timeline Submit" && e.range.columnStart==117 && e.value==true) {
var ss=SpreadsheetApp.openById('id')
var tsh= ss.getSheetByName("WIP");
var trg=tsh.getRange(tsh.getLastRow()+1,1);
sh.getRange(e.range.rowStart,1,1,sh.getLastColumn()).copyTo(trg,{contentsOnly:true})
}else if(sh.getName()=="WIP" && e.range.columnStart==17 && e.value==false) {
var ss=SpreadsheetApp.openById('id')
var tsh=ss.getSheetByName('Timeline Submit');
var trg=sh.getRange(tsh.getLastRow()+1,1);
sh.getRange(e.range.rowStart,1,1,sh.getLastColumn()).copyTo(trg,{contentsOnly:true});
}
}
I am using the code posted here that moves a row based on the value of a column, but I want to edit it so that it does the opposite: moves a column to a new sheet based on the value of a row.
This is what I have so far:
function onEdit(event) {
// source data in sheet named Blocks
// sheet column is moved to named Ordered
// test row with word "Done" is row 1
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Blocks" && r.getRow() == 1 && r.getValue() == "Done") {
var column = r.getColumn();
var numRows = s.getLastRow();
var targetSheet = ss.getSheetByName("Ordered");
var target = targetSheet.getRange(targetSheet.getLastColumn() + 1, 1);
s.getRange(column, 1, 1, numRows).copyTo(target);
s.deleteColumn(column);
}
}
The problem is, when I tested it, it only moved the first row of that column. I want it to move the entire column and then delete it from the original sheet.
Can anyone tell me where I went wrong?
Moving Selected Columns
function onEdit(e) {
//e.source.toast('Entry');
var sh = e.range.getSheet();
if(sh.getName() == "Blocks" && e.range.rowStart == 1 && e.value == "Done") {
//e.source.toast('Ordered1')
var tsh = e.source.getSheetByName("Ordered");
var target = tsh.getRange(1,tsh.getLastColumn() + 1);
sh.getRange(2, e.range.columnStart, sh.getLastRow() -1).copyTo(target);
sh.deleteColumn(e.range.columnStart);
}
}
I used Active/Done datavalidation on row one and I only moved from row two to last row and then deleted the entire column including the validated cell.
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'm new to the world of javascript & google apps script and I'm struggling to get a row of data to move from one spreadsheet to another. I have it moving a row from sheet to sheet within the same spreadsheet but not across spreadsheets.
I know I need to use SpreadsheetApp.openById("insertID") rather than SpreadsheetApp.getActiveSpreadsheet() but I can't seem to make it work.
Here is the code I have that's working based on moving sheet to sheet, how do I modify it to move to a new spreadsheet?
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Timeline Submit" && r.getColumn() == 117 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("WIP");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target, {contentsOnly:true});
} else if(s.getName() == "WIP" && r.getColumn() == 117 && r.getValue() == false) {
var row = r.getRow();
var numColumns = s.getColumn();
var targetSheet = ss.getSheetByName("Timeline Submit");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target, {contentsOnly:true});
}
}
I couldn't figure out everything in the second section of your code but it had too many errors so I just guessed and some of it.
Don't forget to go create a trigger for this in the Script Editor Edits/Current Project Triggers.
function installableOnEdit(e) {
var sh=e.range.getSheet();
if(sh.getName()=="Timeline Submit" && e.range.columnStart==117 && e.value==true) {
var ss=SpreadsheetApp.openById('id')
var tsh= ss.getSheetByName("WIP");
var trg=tsh.getRange(tsh.getLastRow()+1,1);
sh.getRange(e.range.rowStart,1,1,sh.getLastColumn()).copyTo(trg,{contentsOnly:true})
}else if(sh.getName()=="WIP" && e.range.columnStart==17 && e.value==false) {
var ss=SpreadsheetApp.openById('id')
var tsh=ss.getSheetByName('Timeline Submit');
var trg=sh.getRange(tsh.getLastRow()+1,1);
sh.getRange(e.range.rowStart,1,1,sh.getLastColumn()).copyTo(trg,{contentsOnly:true});
}
}
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.