Google Sheets: Row not copying to another sheet - google-apps-script

I have two sheets. Pipeline and Archive. I also have a data validation list on column 5 that switches from blank to Yes only. When I debug it, it doesn't throw an error. But when I go to my sheet and change column 5 to yes, it doesn't move to the Archive sheet, nor does it delete anything.
function onEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var c = s.getActiveCell();
if (s.getName() == "Pipeline" && c.getColumn() == 5 && c.getValue() == "Yes") {
var targetSheet = ss.getSheetByName("Archive");
var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(c.getRow(), 1, 1, sheet.getLastColumn()).copyTo(targetRange, {contentsOnly:true});
s.deleteRow(c.getRow());
}
}

Change:
s.getRange(c.getRow(), 1, 1, sheet.getLastColumn()).copyTo(targetRange, {contentsOnly:true});
s.deleteRow(c.getRow());
To:
s.getRange(c.getRow(), 1, 1, s.getLastColumn()).copyTo(targetRange, {contentsOnly:true});
s.deleteRow(c.getRow());
Hopefully this appeases the PTB...

Related

Trying to Clear Columns based on time to trigger onEdit script automatically with an ArrayFormula in place

So I am trying to get my onEdit script to run automatically as rows are added to my Google Spreadsheet. I already have the onEdit script working, and I have an arrayFormula set to add to new rows when added, but this isn't enough to trigger the onEdit. I can clear the contents of the column and that will trigger it, but I need to set a daily clearColumn function based on those rows marked "TRUE" and everything I try just keeps giving me an null/undefined.
**Here is my onEdit script: **
function onEdit(event) {
// assumes source data in sheet named Initial SS Addition
// target sheet of move to named Google to Mailshake
// getColumn with check-boxes is currently set to column 9 or H
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Initial SS Addition" && r.getColumn() == 9 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Google to Mailshake");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
} else if(s.getName() == "Google to Mailshake" && r.getColumn() == 9 && r.getValue() == false) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Initial SS Addition");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
**Here is my arrayFormula: **
=ArrayFormula(IF(ROW(H:H)=1,"Appt Done",IF(ISBLANK(H:H),"",TODAY()>=datevalue(H:H))))
**And this is my currenty clearColumn script that is giving me issues: **
function clean0() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var where = ss.getSheetByName('Initial SS Addition');
var triggerCell = where.getRange('I:I').getValue();
if (triggerCell == 'FALSE'){} else // If the cell is true do nothing
if (triggerCell == 'TRUE'){ // If the cell is false run the script
sheet.getRangeList(range).clearContent();
}
}
Any help you can offer would be greatly appreciated! TIA
Managed to figure it out:
Set up a time trigger as follows:
function createTimeTrigger() {
// Creates a trigger to run the moveRows function every hour
ScriptApp.newTrigger('moveRows')
.timeBased()
.everyMinutes(5)
.create();
}
The removed the onEdit function and changed it to just a moveRows function:
function moveRows() {
// assumes source data in sheet named Initial SS Addition
// target sheet of move to named Google to Mailshake
// getColumn with check-boxes is currently set to column 9 or H
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName("Initial SS Addition");
if (!s) {
return;
}
var columnI = s.getRange("I:I"); // gets the range of cells in column I
var values = columnI.getValues(); // gets the values in the range
for (var i = 0; i < values.length; i++) {
var value = values[i][0];
if (value == true) {
var row = i + 1; // row index is 1-based
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Google to Mailshake");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
}
Now it works perfectly. Thanks to everyone for the ideas :)

Is there a way for me to use the 1st cell of a row as a getsheetbyname parameter in google sheet?

function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Sheet1" && r.getColumn() == 3 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("id1");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
// s.deleteRow(row);
}
}
This is the code so far and I was wondering if there was a way for me to automatically append all of the row or "id" to their specific tab if their checkbox have been checked using the 1st cell of the row as the parameter for the getsheetbyname?
I tried to create a variable and put it in the getSheetByName() like the example below but it didn't work. I'm very new to javascript/apps script.
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Sheet253" && r.getColumn() == 2 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var a = row[0];
var targetSheet = ss.getSheetByName(a);
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
// s.deleteRow(row);
}
}
This is an example of the google sheet:
https://docs.google.com/spreadsheets/d/19TzSKyY4616sWpY2ONPj01F5CJDfsTJ7DmbFXPVKvJE/edit?usp=sharing
The result should look like the result in tab id1.
Modification points:
In your script, I thought that the event object can be used.
In your sample Spreadsheet, it seems that the checkbox is put to the column "C". When you want to run the script, when the checkbox is checked, the checked column is the column "C".
When these points are reflected in your script, how about the following modification?
Modified script:
From your provided sample Spreadsheet, this script supposes that the source sheet name is "Sheet1". Please be careful about this.
function onEdit(event) {
var ss = event.source;
var s = ss.getActiveSheet();
var r = event.range;
if (s.getName() == "Sheet1" && r.columnStart == 3 && r.isChecked()) {
var sheetName = r.offset(0, -2).getValue();
var targetSheet = ss.getSheetByName(sheetName);
if (!targetSheet) {
Browser.msgBox(`"${sheetName}" was not found.`);
return;
}
var row = r.rowStart;
var numColumns = s.getLastColumn();
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
// s.deleteRow(row);
}
}
In this script, when the checkbox of the column "C" of "Sheet1" is checked, the row is copied.
Note:
In your showing script, the edited row is used. So I modified your script by following your showing script. But, about This is the code so far and I was wondering if there was a way for me to automatically append all of the row or "id" to their specific tab if their checkbox has been checked using the 1st cell of the row as the parameter for the getsheetbyname?, if you want to copy all rows by one execution of the script, how about the following sample script? When this script is run, the checkboxes of column "C" of "Sheet1" are checked, and each value are copied to each sheet using the values of column "A".
function sample() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const srcSheet = ss.getSheetByName("Sheet1");
const obj = srcSheet.getRange("A2:C" + srcSheet.getLastRow()).getValues().reduce((o, r) => {
if (r[2] === true) {
o[r[0]] = o[r[0]] ? [...o[r[0]], r] : [r];
}
return o;
}, {});
Object.entries(obj).forEach(([name, values]) => {
const sheet = ss.getSheetByName(name);
const range = sheet.getRange(sheet.getLastRow() + 1, 1, values.length, values[0].length);
range.setValues(values).offset(0, 2, values.length, 1).insertCheckboxes();
});
}
Note:
This sample script is for your provided sample Spreadsheet. So, when you change the structure of the Spreadsheet, the script might not be able to be used. So, please be careful about this.
References:
Event Objects
getValues()
setValues(values)

How to move Row to another sheet when a value in Col 11 = Yes with a Google Script

i have a summary in Sheet 2 ("Members") which is fed from a form in sheet 1 ("Reception"). Once the task has been updated and completed is set to Yes in sheet 1 and then save auto in sheet 2, i would like to run a script to move row from "Members" to "Archive" if value updated in Reception for completed is set to Yes. Actualy to make it work i put a button with a script in "members" and i must be place on cell with value Yes to move the row to "Archive"... I want it to be automated..
Can you help me?
Here's the Script i use:
// callFunctionB() cause i use onEdit(e) { callFunctionA(e);callFunctionB(e) }
function callFunctionB(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var r = s.getActiveRange();
if(s.getName() == "Members" && r.getColumn() == 11 && r.getValue() == 'Yes') {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Archive");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
function callB(e) {
var s=e.range.getSheet();
if(s.getName()=="Members" && e.range.columnStart==11 && e.value=='Yes') {
e.source.getSheetByName("Archive").appendRow(s.getRange(e.range.rowStart,1,1,s.getLastColumn()).getValues()[0]);
s.deleteRow(e.range.rowStart);
}
}

Auto Copying Rows in Google Sheets

I'm attempting to write a few functions that
copy google form submission to from "form submission" sheet to "All Leads" sheet
move whole row from "All Leads" sheet to "Open Leads" sheet based on a cell value = "open"
move whole row data either back to "All Leads" or "closed","lost" sheets based on cell value ="closed/lost/blank"
var sheetNameToWatch = "Form Responses";
var columnNumberToWatch = 3;
var sheetNameToMoveTheRowTo = "All Leads";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();
if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && isBlank(range) == FALSE) {
var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).moveTo(targetRange);
//sheet.deleteRow(range.getRow());
}
}
But I'm not getting this to trigger onEdit or onFormSubmission. Any help is appreciated, and as you can tell i'm new to scripting.
Here is the shared google spreadsheet
https://docs.google.com/spreadsheets/d/13090GHrXY17lRzA9ppQHZZGyT9yUNk6hMMXDQIKmddI/edit?usp=sharing
Since you already have entries stored in your Spreadsheet, you should just run this snippet once to copy the already stored values in their place. This works by gathering all the data wanted from the Form Responses sheet by using the getRange(row, col, numrows, numcols) method and then pasting the values by using the setValues() method.
function copyStuff() {
var spreadsheet = SpreadsheetApp.openById("ID_OF_YOUR_SPREADSHEET");
var allLeads = spreadsheet.getSheetByName("All Leads");
var formSub = spreadsheet.getSheetByName("Form Responses");
var valsFromForm = formSub.getRange(3, 1, 53, 13).getValues();
allLeads.getRange(2, 1, 53, 13).setValues(valsFromForm);
}
Afterwards, you should replace the above snippet with this one:
function onFormSubmit() {
var spreadsheet = SpreadsheetApp.openById("ID_OF_YOUR_SPREADSHEET");
var allLeads = spreadsheet.getSheetByName("All Leads");
var formSub = spreadsheet.getSheetByName("Form Responses");
var openSheet = spreadsheet.getSheetByName("Open Leads");
var closedSheet = spreadsheet.getSheetByName("Closed");
var lostSheet = spreadsheet.getSheetByName("Lost");
var lastVals = formSub.getRange(formSub.getLastRow(), 1, 1, 13).getValues();
allLeads.getRange(allLeads.getLastRow(), 1, 1, 13).setValues(lastVals);
var cellValue = spreadsheet.getSheetByName("THE_NAME_OF_YOUR_SHEET_WHERE_CELL_VALUE_IS").getValue();
if (cellValue == "OPEN")
openSheet.getRange(openSheet.getLastRow(), 1, 1, 13).setValues(lastVals);
else if (cellValue == "CLOSED")
closedSheet.getRange(closedSheet.getLastRow(), 1, 1, 13).setValues(lastVals);
else if (cellValue == "LOST")
lostSheet.getRange(lostSheet.getLastRow(), 1, 1, 13).setValues(lastVals);
var lastVals = formSub.getRange(formSub.getLastRow(), 1, 1, 13).getValues();
allLeads.getRange(allLeads.getLastRow(), 1, 1, 13).setValues(lastVals);
}
The above snippet works by getting the last row from the Spreadsheet and then pasting it in the right place based on the cellValue. To get the last row of the sheet, the getLastRow() method has been used.
Since you want this to run on every form submission, you should use an onFormSubmit() installable trigger, and configure it like this:
Note: You will have to have the script linked to the form.
Reference
Sheet Class Apps Script - getRange();
Sheet Class Apps Script - getLastRow();
Installable Triggers Apps Script.

Make an onEdit script read other columns to determine where to send data based on edits

This script currently works, but I would like to add the ability to read data in Column A of Sheet1 (usernames) so that the row is moved to the corresponding sheet. For instance, if "Jim" is the username listed in Column A, the script will send the row to "Jim's" Sheet. There will be 9 different users on this sheet and I would like to move away from everyone viewing the "Master" tab so to speak.
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if (s.getName() == "Sheet1" && r.getColumn() == 2 && r.getValue() == "Yes") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Sheet2");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}