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);
}
}
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);
}
}
function onEdit(event) {
// assumes source data in sheet named Backorder LIST
// target sheet of move to named Completed
// getColumn with check-boxes is currently set to column 1 or A
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Backorder LIST" && r.getColumn() == 1 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Completed");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
I am having trouble finding a way to move only the values to COMPLETED sheet. Anytime I change .moveTo(target); to .copyTo(target, {contentsOnly: true}); the script fails. Any suggestions would be appreciated. Thank you!
Move values only
function onEdit(e) {
var sh = e.range.getSheet();
if(sh.getName() == "Backorder LIST" && e.range.columnStart == 1 && e.value == "TRUE") {
var tsh = e.source.getSheetByName("Completed");
let vs = sh.getRange(e.range.rowStart, 1, 1, sh.getLastColumn()).getDisplayValues();
tsh.getRange(tsh.getLastRow() + 1, 1, vs.length, vs[0].length).setValues(vs);
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 about 20 rows in 5 different sheets and I would like the row to copy to a separate sheet once I check the checkbox. I would like the rows to remain in its source sheet when the row is copied.
I found code that copies the entire row over the separate sheet and then copies the row back once it's un-checked, but it obviously needs tweaking to be applicable to my scenario. Here it is:
function onEdit(event) {
// assumes source data in sheets named Brushed Nickel, Chrome, Matte Black, Brass/Gold, Tile and Paint
// target sheet of move to named Desing Specs
// getColumn with check-boxes is currently set to column 11 or K
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Brushed Nickel", "Chrome", "Matte Black", "Brass/Gold", "Tile and Paint" && r.getColumn() == 11 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Desing Specs");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
} else if(s.getName() == "Desing Specs" && r.getColumn() == 11 && r.getValue() == false) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Brushed Nickel", "Chrome", "Matte Black", "Brass/Gold", "Tile and Paint");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
Screen Shot:
Can someone please help?
Thank you in advance!
function onEdit(e) {
var s = e.range.getSheet();
var shts=["Brushed Nickel", "Chrome", "Matte Black", "Brass/Gold", "Tile and Paint"];
if(shts.indexOf(s.getName()!=-1) && e.range.columnStart==11 && e.value == "TRUE") {
var targetSheet=e.source.getSheetByName("Design Specs");
var target=targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(e.range.rowStart, 1, 1, s.getLastColumn()).copyTo(target);
}
}
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);
}
}