I am working on this spreadsheet and I want it to move the entire row to a separate tab once it is marked as "complete" and if it is marked anything else while on that tab, I want it to move BACK to the original tab.
I've written a script and it works. However the only problem with it is that every single time I write anything on the sheet, everything shifts around. Is there any way to avoid this? I am going to be writing on the sheet constantly and I would like it to ONLY move things around when that specific cell that shows the status (pending, In progress, Complete") changes.
This is what i have written on the script for the Spreadsheet so far:
`
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Current Tasks" && r.getColumn() == 3 && r.getValue() == 'Complete') {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Archived Tasks");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
} else if(s.getName() == "Archived Tasks" && r.getColumn() == 3 && r.getValue() == 'In Progress', 'Pending') {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Current Tasks");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
`
Maybe it's not it, but could you try changing your second if statement?:
else if(s.getName() == "Archived Tasks" && r.getColumn() == 3 && (r.getValue() == 'In Progress' || r.getValue() == 'Pending')){
Related
I need some assistance please. I have a spreadsheet that moves data from one sheet(JDE worklist) to the next (Printed) by clicking the textbox next to the row to be moved. When the tickbox is un-ticked after its moved the first time it moves it to a final sheet (PrintedArchive). This part works fine.
The problem is that I need a static date stamp next to the row once it gets moved to the next sheet as to capture start and end dates. The date can be added to column BE.
If I try and add a separated script to add a date stamp the script for moving the data stops functioning. So is there a way to incorporate this into the script that moves the data?
Please note that don't have much experience and I'm still learning and would really appreciate the help.
This is the script im currently using to move the data
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "JDE Worklist" && r.getColumn() == 1 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Printed");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
} else if(s.getName() == "Printed" && r.getColumn() == 1 && r.getValue() == false) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("PrintedArchive");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
Sample sheet
Try this:
function onEdit(e) {
e.source.toast("Entry");
const s = e.range.getSheet();
if (s.getName() == "JDE Worklist" && e.range.columnStart == 1 && e.value == "TRUE") {
var tsh = e.source.getSheetByName("Printed");
var trg = tsh.getRange(tsh.getLastRow() + 1, 1);
s.getRange(e.range.rowStart, 1, 1, s.getLastColumn()).moveTo(trg);
tsh.getRange("BE" + e.range.rowStart).setValue(new Date());
s.deleteRow(e.range.rowStart);
} else if (s.getName() == "Printed" && e.range.columnStart == 1 && e.value == "FALSE") {
var tsh = e.source.getSheetByName("PrintedArchive");
var trg = tsh.getRange(tsh.getLastRow() + 1, 1);
s.getRange(e.range.rowStart, 1, 1, s.getLastColumn()).moveTo(trg);
tsh.getRange("BE" + e.range.rowStart).setValue(new Date());
s.deleteRow(e.range.rowStart);
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 months ago.
Improve this question
I have a spread sheet that has several pages.
(Working on, Missed, Submitted)
When working with a row they have checkboxes to mark them to be moved based on the boxes checked. Submitted moved to submitted page and so on. When I check the box I get the error shown below.
TypeError: Cannot read property 'source' of undefined
at onEdit(Code:9:17)
First part of the if statement checks the page the event took place, checks that the edit happened in the correct column, if yes move to correct page and delete row from active sheet. Below is the code im using
function onEdit(event) {
// assumes source data in sheet named Submited Routes source
// target sheet of move to named Sumbited Routes
// getColumn with check-boxes is currently set to column 9 or i
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = SpreadsheetApp.getActiveSpreadsheet();
var r = event.source.getActiveRange();
if(s.getName() == "Working on" && r.getColumn() == 9 && r.getValue() == TRUE) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Submited Routes");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
} else if(s.getName() == "Submited Routes" && r.getColumn() == 9 && r.getValue() == FALSE) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Working on");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
function onCheck(event){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = SpreadsheetApp.getActiveSpreadsheet();
var r = SpreadsheetApp.getActiveRange();
if(s.getName()== 'Working on' && r.getColumn() == 11 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Missed");
var target = targetSheet.getRange(targetSheet.getLastRow() +1,1);
s.getRange(row, 1,1,numColumns).moveTo(target);
s.deleteRow(row);
}else if(s.getName() == "Missed" && r.getColumn() == 11 && r.getValue() == false) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Working on");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
EDIT:
The answer below is the prefect fix for my code.
Try it this way:
function onEdit(e) {
var sh = e.range.getSheet();
if(sh.getName() == "Working on" && e.range.columnStart == 9 && e.value == "TRUE") {
var numColumns = sh.getLastColumn();
var tsh = e.source.getSheetByName("Submited Routes");
var trg = tsh.getRange(tsh.getLastRow() + 1, 1);
sh.getRange(e.range.rowStart, 1, 1, numColumns).moveTo(trg);
sh.deleteRow(e.range.rowStart);
} else if(sh.getName() == "Submited Routes" && e.range.columnStart == 9 && e.value == "FALSE") {
var numColumns = sh.getLastColumn();
var tsh = e.source.getSheetByName("Working on");
var trg = tsh.getRange(tsh.getLastRow() + 1, 1);
sh.getRange(e.range.rowStart, 1, 1, numColumns).moveTo(trg);
sh.deleteRow(e.range.rowStart);
}
}
My first task was to move data from one sheet to another when a certain entry is made in a certain column. That I did successfully taking help from online sources.
The next task is to add a timestamp to the now moved data(in target sheet). I have tried multiple ways to do that but it's not working at all. I have added my code below, what am I doing wrong?
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Task" && r.getColumn() == 4 && r.getValue() == "Done") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("DoneTask");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
else if (s.getName() == "DoneTask" && r.getColumn() == 4 && r.getValue() == "Done")
{var nextCell = r.offset(0, 4);
nextCell.setValue(new Date());
}
}
I think it's because the active sheet is still the "Task" sheet and so the elseif is not working, but I don't know how to verify it.
I don't really see why you would put this in a an else if. Seems like you want to stamp the date when the if statement runs?
Try this.
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Task" && r.getColumn() == 4 && r.getValue() == "Done") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("DoneTask");
var lr = targetSheet.getLastRow();
var target = targetSheet.getRange(lr +1 , 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
targetSheet.getRange(lr + 1, numColumns +1).setValue(new Date());
}
}
I have been using the following code to move and delete row upon completion of line item,
I have now moved from manual entries to line entries via google form and have not been able to achieve the same results.
Any Ideas or help would be greatly appreciated.
I assume that the form submissions are behaving differently and I just have not found the right commands to make it work correctly
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Open Tickets" && r.getColumn() == 16 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Closed Tickets");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
} else if(s.getName() == "Closed Tickets" && r.getColumn() == 16 && r.getValue() == false) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Open Tickets");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
As you moved from manual entry to the use of a Google Form you can't use anymore an "on edit" trigger but you could use an form submit installable trigger.
Related
Google Form + Apps Script onEdit
Reference
https://developers.google.com/apps-script/guides/triggers
I want to add a script that moves a row to another tab if a column isn"t Blank
Here is the script i am using. The "NOT BLANK" is the part i need to figure out. i understand how to move it if it has a specific value but want to know how to get it to move if it has ANY text.
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Current" && r.getColumn() == 1 && r.getValue() == "NOT BLANK") {
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);
}
}
if the targeted column isn't blank to move the row to a second tab
Change this line:
if(s.getName() == "Current" && r.getColumn() == 1 && r.getValue() == "NOT BLANK")
To this:
if(event.range.getSheet().getName()=="Current" && event.range.columnStart==1 && event.value.length>0)
This is what I came up with after playing with it a bit.
function onEdit(e) {
var ss=e.source;
var rg=e.range;
var sh=rg.getSheet();
var col=rg.columnStart;
if(sh.getName()=="Sheet46" && col==1 && e.value.length>0) {
var row=rg.rowStart;
var numColumns = sh.getLastColumn();
var targetSheet = ss.getSheetByName("Sheet50");
var target=targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
sh.getRange(row, 1, 1, numColumns).moveTo(target);
sh.deleteRow(row);
}
}