Form submission Delete row - google-apps-script

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

Related

Google Sheet Script to Copy and Paste Row

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')){

How to move rows based on cell value to other sheets?

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.

Google Sheets onEdit moving rows between sheets with different columns

I'm trying to create a simple shipment processing flow and want to move rows between two sheets(tabs) using an onEdit checkbox trigger.
Here's a link to my project: https://docs.google.com/spreadsheets/d/1SPO21kAyNX5fEqwkXVjmPASkfHvZ5Tzbq4s7e22VdYg/edit#gid=2005756436
The basic concept is that shipments made up of multiple pieces are input on the first sheet and then copied to the WB_Log sheet. When planning what to load into a truck, a checkbox is used to move the pieces from the WB_Log sheet to the Load_Plan sheet where their loading location will be determined in a column to the right of the checkbox. If a piece or pieces of a shipment will not fit in a particular truck, I want to uncheck the box on Load_Plan and have the row moved back to the WB_Log Sheet.
Here is the onEdit function I found that almost works:
function onEdit(event) {
// assumes source data in sheet named WB_Log
// target sheet of move to named Load_Plan
// getColumn with check-boxes is currently set to column 14 or N
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "WB_Log" && r.getColumn() == 14 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Load_Plan");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
} else if(s.getName() == "Load_Plan" && r.getColumn() == 14 && r.getValue() == false) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("WB_Log");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
My issue is that Load_Plan has columns to the right of the checkbox that I don’t want moved back to WB_Log. How do I adjust the code so that only the information from columns A:N move from Load_Plan back to WB_Log when the checkbox is unchecked (False)?
Any advice or ideas anyone can lend would be very much appreciated.
Modification points:
When I saw your shared Spreadsheet, "WB_Log" sheet has the checkboxes in the column "N". And the checkboxes are put to the bottom of the sheet. By this, targetSheet.getLastRow() + 1 returns the next row of the buttom of sheet. I think that this is the reason of your issue.
I think that in your situation, when the last row of after row of "B4" is retrieved, the row number can be used.
And, in your script, the columns "A" to "O" in "Load_Plan" sheet are retrieved by var numColumns = s.getLastColumn(). But "WB_Log" sheet has the columns "A" to "N". I think that the retrieved columns are required to be reduced 1 column.
When above points are reflected to your script, it becomes as follows.
Modified script:
Please modify your script as follows.
From:
} else if(s.getName() == "Load_Plan" && r.getColumn() == 14 && r.getValue() == false) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("WB_Log");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
To:
} else if(s.getName() == "Load_Plan" && r.getColumn() == 14 && r.getValue() == false) {
var row = r.getRow();
var numColumns = s.getLastColumn() - 1; // Modified
var targetSheet = ss.getSheetByName("WB_Log");
var lastRow = targetSheet.getRange("B4").getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow(); // Added
var target = targetSheet.getRange(lastRow + 1, 1); // Modified
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
Reference:
getNextDataCell(direction)

Add timestamp AFTER a edit function is run in the target sheet-Google Sheets

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

My google scripts no longer runs

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