Review of personal code - google-apps-script

I found this code and implemented it into my spreadsheet. My problem is that the code overwrites some data validation I had in my empty list. I wish to keep that drop down menu since I want the users to have it as easy as possible. I thought I could add the lines below (see text between **)
function onEdit(event) {
// assumes source data in sheet named Masterlist
// target sheet of move to named Actionlist
// test column with yes/no is col 13 or M;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Masterliste"
&& r.getColumn() == 13 && r.getValue() == "Ja") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Actionlist");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
// **
var cellrange = SpreadsheetApp.getActive().getRange(target);
var range = SpreadsheetApp.getSheetByName("Dropdown").getRange('A16:B18'); //This is the range for the dropdown I want to hand over.
var rule = SpreadsheetApp.newDataValidation().requireValueInRange(range).build();
cell.setDataValidation(rule); //Include a data validation menu in each line automatically
// **
s.getRange(row, 1, 1, numColumns).copyTo(target); //This executes the copy order with the data provided before
}
}
I know this might be easy for some of you but I have just started with google 4 months ago and it's driving me crazy. I am familiar to excel and VBA, but not to Java at all.
Yes I did already do the codeacademy courses. They really didn't help me a lot. Sorry for my tone I am getting really annoyed by this. Thanks for your help. Enjoy the weekend!

You need to add an option to copyTo. Here is what that looks like:
s.getRange(row, 1, 1, numColumns).copyTo(target, {contentsOnly:true});
Alternatively you could use: copyValuesToRange(sheet, column, columnEnd, row, rowEnd)
https://developers.google.com/apps-script/reference/spreadsheet/range#copyTo(Range,Object)

Related

How do I copy data from one row in a google sheet to another tab in the same sheet?

To expand on the surface level detail of my question; I have one google sheet tab which updates automatically with data each Monday. What I need to be able to do is copy that row from it's tab within the Google Sheet to another tab within the same over arching sheet. This copied row needs to be placed one down from the previous row (week 1 would be row 1, week 2 row 2 etc).
So far I have set up a trigger for this script to run each Monday (so I've managed the easy part...) but I still cannot figure out how to do the main body of work! I've had a look at some similar bits of code (one of which I've pasted down below) where data gets moved across to a new sheet when a box is ticked or when a certain value is noticed in a cell, but I couldn't modify either of these to work in my case.
I'm completely new to using Google Script and with no background in any language so help on this would be amazing! Or if anybody knows a way of performing this function without the need of Script that would be even better! (I've looked into using =today() and using the dates i would be looking for as triggers, but of course that comes up with an error as the text of the cell is '=today()' and not the actual date...)
function onEdit(event) {
// assumes source data in sheet named main
// target sheet of move to named Completed
// getColumn with check-boxes is currently set to colu 4 or D
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "main" && r.getColumn() == 4 && 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);
} else if(s.getName() == "Completed" && r.getColumn() == 4 && r.getValue() == false) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("main");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}

Google Sheets Script Help Request: Copy check boxed rows from multiple sheets to new sheet

Full disclosure: I have zero programming experience, but I'm a quick study.
I am trying to be able to copy individual rows from a number of different sheets to be copied to one specific sheet. I spent some time looking at previous questions/answers on this (great) site, and I was able to piece together a script that seems to do PART of what I'm looking for.
I can see where in the script I could/should change to get the functionality I'm looking for -- BUT I don't know what to put in the code to actually do what I'm hoping to do. I come to this community humbly asking for help/guidance.
Here's the script that I pieced together and a link to the example I've been working with:
function onEdit(event) {
// assumes source data in sheet named ABC
// target sheet of copy to named Interdisciplinary
// getColumn with check-boxes is currently set to colu 15 or O
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "ABC" && r.getColumn() == 15 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Interdisciplinary");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
} else if(s.getName() == "Interdisciplinary" && r.getColumn() == 4 && r.getValue() == false) {
}
}
I know that I'm identifying a specific sheet to pull data from (ABC), what might I put there to pull from any sheet where a checkbox has been clicked?
I also know that I'm having the rows copied to a row that is some defined place away from the LastRow. What might I be able to put there to have the row copied either a row w/in my semester headings OR to have the copied to the top of the new sheet?
Finally, I'd truly appreciate any other comments, feedback, guidance that the community might have for my request.
Thanks much!
Kd
Your code works. The problem is that the code in your spreadsheet is looking at column 21, not column 15.
Also, I suggest that you review the properties available to you in the event object. You should use them instead of the first three declarations you wrote. You can see the change in my version below.
function onEdit(event) {
var sheets = ["ABC", "DEF", "GHI", "JKL", "MNO"]; // Sheet names that will trigger execution. Case-sensitive.
// target sheet of copy to named Interdisciplinary
// getColumn with check-boxes is currently set to colu 15 or O
var ss = event.source;
var s = event.range.getSheet();
var r = event.range;
if (sheets.indexOf(s.getName()) != -1 && r.getColumn() == 15 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Interdisciplinary");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
} else if(s.getName() == "Interdisciplinary" && r.getColumn() == 4 && r.getValue() == false) {
}
}
EDIT: I've modified the code to show you how to allow triggering from multiple sheets.

Google Sheets two onEdits, second onEdit conflicts with first [duplicate]

This question already has an answer here:
Merging or Combining two onEdit trigger functions
(1 answer)
Closed 1 year ago.
I'm working in Google sheets trying to set up two onEdit scripts, one to work right after the other. I'm not really a coder but can at least somewhat read coding and for the most part, figured out what and where to change things to make scripts work for my spreadsheet. I've looked all over StackOverflow and Google's help forum but nothing exactly has helped with me with the issue I'm facing.
Here is what I'm trying to do. Column 12 (L) is a status column with the options; In progress, No Resolution, Resolved.
When anything gets flipped to Resolved, I'd like it to move to the bottom of the spreadsheet and then that bottom row gets highlighted.
Here are the two scripts I'm working with, on their own they do what I want but together the second script runs first highlighting the row, and then it moves to the bottom. Also for some reason when the second script is active there is a 50-50 that when the entry is moved to the bottom is moves up one row.
First script:
function onEdit(e){
// assumes source data in sheet named Problems
// target sheet of move to named Problems, Problems
// test column with yes/no is col 12 or L
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Problems" && r.getColumn() == 12 && r.getValue() ==
"Resolved") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Problems");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
Second script:
function onEdit2(e) {
if (e) {
var ss = e.source.getActiveSheet();
var r = e.source.getActiveRange();
if (r.getRow() != 1 && ss.getName() == "Problems") {
status = ss.getRange(r.getRow(), 12).getValue();
rowRange = ss.getRange(r.getRow(),1,1, 12);
if (status == 'Resolved') {
rowRange.setBackgroundColor("#99ccff");
}
}else if (status == '') {
rowRange.setFontColor("#000000");
}
}
}
I've set the onEdit2 to a trigger but that hasn't worked :(
Any help is appreciated and please let me know if any more information is needed.
Try this function. It'll move the Resolved row to bottom then highlight it.
function onEdit(event) {
var sheet = event.source.getActiveSheet();
var range = event.source.getActiveRange();
// move
if (sheet.getName() == 'Problems' && range.getColumn() == 12 && range.getValue() == 'Resolved') {
var row = range.getRow();
var numColumns = sheet.getLastColumn();
var tRange = sheet.getRange(sheet.getLastRow() + 1, 1, 1, numColumns);
tRange.setValues(sheet.getRange(row, 1, 1, numColumns).getValues());
tRange.setBackgroundColor('#99ccff');
sheet.deleteRow(row);
}
}
The onEdit(e) function is a reserved function by Google AppScript just like the other trigger functions (onOpen(), onChange, ...). Calling it onEdit2(e) will not work as it is not recognized as a trigger function.
Simply change the name to onEdit() so that Google AppScript can trigger it.
Let me know if that works for you

Google Sheet Copy Row to New Sheet IF Formula Trigger

I'm trying to use a pretty standard copy row from one sheet to another sheet code when Column B shows 'Start', but Column B has the following formula in it:
=IF(A2 = "Complete","Start","Not Yet")
and this code doesn't seem to recognize the formula changing from 'Not Yet' to Start'. Any help would be greatly appreciated. Thanks!
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();
if(s.getName() == "Temp" && r.getColumn() == 1 && r.getValue() == "Complete") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Start");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
The issue is not that it does not "recognize" the formula changing value. If you look at your formula again after it is copied you should notice that the reference changes from A2 to Temp!A2.
moveTo works like if you individually cut-n-paste each cell into their new operation. This results in this "maintaining" of the reference to their original cells.
If this is not desired, maybe you should switch to copyTo. Which might have other side effects in your formulas. But should work fine for this simple one you exemplified.
s.getRange(row, 1, 1, numColumns).copyTo(target); //try copyTo here

TypeError: Cannot read property 'source' from undefined

I am quite new to coding and am running into an issue I can't work around. I've seen similar questions in the forums but can't seem to work out my issue based on those answers. I'm attempting to set up a script that will move a line to another sheet when "Closed" is entered into a specific column. I keep getting the following error when I run the script from my GoogleSheet:
"TypeError: Cannot read property "source" from undefined."
This is the code I am working with:
function onEdit(event) {
// assumes source data in sheet named Open
// target sheet of move to named Closed
// test column with yes is col 8 or H
var ss = SpreadsheetApp.getActiveSheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Open" && r.getColumn() == 8 && r.getValue() == "Closed") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Closed");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
Any assistance would be greatly appreciated!
I’ve never used google script before but from the looks of things “.source” is not being recognised, and that is because you’ve used it as a function of event; which when you’ve declared it as a pass you haven’t given the type and a name. Try “event e”, and then use e.source?