How to define specific sheet - .getsheetbyname error - javascript

I am just wanting to define a specific sheet to this code:
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
var r = s.getActiveCell();
if( r.getColumn() == 3 && r.getValue() == '3')
var nextCell = r.offset(0, 1);
nextCell.setValue("1");
}
I have tried .getSheetByName():
function onEdit() {
var s = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Test");
var r = s.getActiveCell();
if( r.getColumn() == 3 && r.getValue() == '3')
var nextCell = r.offset(0, 1);
nextCell.setValue("1");
}
But it is still working across all sheets. I have a feeling it is to do with .getActiveCell(), .getColumn() and .getValue(). Do I need to specify which sheet these are in too? If so, how? Thanks a lot!

Use the event object to check if current sheet is the one you're trying to work on. Change 'required sheet name' to something you need.
function onEdit(e) {
var range = e.range;
var name = range.getSheet().getName();
// check if 'name' is OK
// if OK, proceed
// if NOT OK, return
if (name != 'required sheet name') return;
if (range.getColumn() == 3 && range.getValue() == '3') {
var nextCell = range.offset(0, 1);
nextCell.setValue('1');
}
}

Related

Moving rows to another sheet with Apps Script, but doesn't work with shared document

I have a script helping me both generate a started date by a conditional column, and also moving a row to another sheet by a "Done" Column. When I use the document it works without any issues, but when others use the document the rows aren't moved to the separate sheet. Anyone got any ideas what it could be?
`
function onEdit(e) {
first(e);
second(e);
function first(e) {
var row = e.range.getRow();
var col = e.range.getColumn();
if(
e.source.getActiveSheet().getName() == 'B2B_LeadList'
&&
col === 22
&&
e.value == 'STARTED'
&&
e.source.getActiveSheet().getRange(row,23).setValue(new Date()== ''
&&
row > 1)){
e.source.getActiveSheet().getRange(row,23).setValue(new Date());
}}
function second(e){
var mainSheet = 'B2B_LeadList'
var targetSheet = 'B2B_Archive'
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveRange();
if(sheet.getName() == 'B2B_LeadList' && e.range.getColumn() == 35 && e.range.getValue() == 'YES'){
var row = e.range.getRow();
var numColumns = sheet.getLastColumn();
var targetSheet = ss.getSheetByName('B2B_Archive');
var target = targetSheet.getRange(targetSheet.getLastRow() + 1,1);
e.source.getActiveSheet().getRange(row,36).setValue(new Date());
sheet.getRange(row,1,1,numColumns).moveTo(target);
sheet.deleteRow(row);
}
}
}
`
I am expecting that other users working in this document can move rows from current sheet to another sheet by selecting "YES" in a "Done?" column.
Try it this way:
function onEdit(e) {
const sh = e.range.getSheet();
if (sh.getName() == 'B2B_LeadList' && e.range.columnStart === 22 && e.range.rowStart > 1 && e.value == 'STARTED') {
sh.getRange(e.range.rowStart, 23).setValue(new Date());
}
if (sh.getName() == 'B2B_LeadList' && e.range.columnStart == 35 && e.value == 'YES') {
var tsh = e.source.getSheetByName('B2B_Archive');
var trg = tsh.getRange(tsh.getLastRow() + 1, 1);
sh.getRange(e.range.rowStart, 36).setValue(new Date());
sh.getRange(e.range.rowStart, 1, 1, sh.getLastColumn()).moveTo(trg);
sh.deleteRow(e.range.rowStart);
}
}
If you are moving the script to their account make sure they authorize it. Otherwise ensure that they have permission to edit the file. Also note that this is a simple trigger and cannot perform functions which require permission.

OnEdit function with google Spreadsheet

I am still a newbie in this, but I want to run Onedit function script on google spreadsheet in a way that I will only get value on column E if the value on column D is "completed" and at the same time I will get value on column F depending on the value on B1 ( and that's only if the column D is"completed"). here is my spreadsheet: https://docs.google.com/spreadsheets/d/1-xLH3mb0fGngocOleCNExEmP6FnDMEFg2uLFXC10XEk/edit#gid=0
function onEdit(e){
var s = SpreadsheetApp.getActiveSpreadsheet();
if( s.getName() == "Hub" ){
var r = s.getActiveCell();
if (r.getColumn() == 4) {
var timecell = r.offSet(0, 1);
var assldapv = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Hub").getRange("B1").getValue();
var assldapo = r.offset(0, 2);
if ( r.getValue() === 'completed' ) {
timecell.setValue(new Date());
assldapo.setValue(assldapv); }
However this function is not working, so can you please give me a template on how to proceed with such script.
Try this
function onEdit(e) {
var sh = e.range.getSheet();
if (sh.getName() == "Hub" && e.range.columnStart == 4 && e.value == "completed") {
var timecell = e.range.offSet(0, 1);
var assldapv = sh.getRange("B1").getValue();
var assldapo = e.range.offset(0, 2);
timecell.setValue(new Date());
assldapo.setValue(assldapv);
}
}

Apps Script for Google Sheets (onedit function)

On google sheets trying to hide/unhide rows based on the values in particular cells, and trying to write a script in AppsScript for that. Found that one that works in isolation (if B55=NO it hides 64 next rows):
function onEdit(e) {
var activeSheet = e.source.getActiveSheet();
var range = e.range;
if (activeSheet.getName() !== 'Inputs' || range.getA1Notation() !== 'B55') return;
if (e.value === 'No') {
activeSheet.hideRows(56, 64);
} else if (e.value === 'Yes') {
activeSheet.showRows(56, 64);
}
}
But I need the same for Multiple Cells and multiple rows, as soon as I expand it only last part of the code works and not the first:
function onEdit(e) {
var activeSheet = e.source.getActiveSheet();
var range = e.range;
if (activeSheet.getName() !== 'Inputs' || range.getA1Notation() !== 'B55') return;
if (e.value === 'No') {
activeSheet.hideRows(56, 64);
} else if (e.value === 'Yes') {
activeSheet.showRows(56, 64);
}
}
function onEdit(e) {
var activeSheet = e.source.getActiveSheet();
var range = e.range;
if (activeSheet.getName() !== 'Inputs' || range.getA1Notation() !== 'B121') return;
if (e.value === 'No') {
activeSheet.hideRows(122, 67);
} else if (e.value === 'Yes') {
activeSheet.showRows(122, 67);
}
}
From here Cell B121 is working, but my B55 Stops working. Any tips? thanks!
You need to merge the 2 functions onEdit, try
function onEdit(e) {
var activeSheet = e.source.getActiveSheet();
var range = e.range;
if (activeSheet.getName() !== 'Inputs') return;
if (range.getA1Notation() == 'B55'){
if (e.value === 'No') {
activeSheet.hideRows(56, 64);
} else if (e.value === 'Yes') {
activeSheet.showRows(56, 64);
}
}
if (range.getA1Notation() == 'B121') {
if (e.value === 'No') {
activeSheet.hideRows(122, 67);
} else if (e.value === 'Yes') {
activeSheet.showRows(122, 67);
}
}
}
Answers have already been provided, but consider the more general case:
function myFooBar() {
console.log("the first one")
}
function myFooBar() {
console.log("the second one")
}
function doMyFooBar() {
myFooBar();
}
When you run doMyFooBar(), the console shows the second one. Reading from the top of the script to the bottom, the last declaration of function myFooBar() replaces any previous declarations of that function.
Mike is correct, you can only have on onEdit() trigger, but cannot just merge as he shows since there are returns if the condition is not found
function onEdit(e) {
var activeSheet = e.source.getActiveSheet();
var range = e.range;
if (activeSheet.getName() == 'Inputs' && range.getA1Notation() == 'B55') {;
if (e.value === 'No') {
activeSheet.hideRows(56, 64);
} else if (e.value === 'Yes') {
activeSheet.showRows(56, 64);
}
}
if (activeSheet.getName() == 'Inputs' && range.getA1Notation() == 'B121') {
if (e.value === 'No') {
activeSheet.hideRows(122, 67);
} else if (e.value === 'Yes') {
activeSheet.showRows(122, 67);
}
}
}

Using IF to evaluate an specific string

I have a Google sheet job planning project and I want to move a row of data to another sheet but only if the job was done by me. I did something like
if(s.getName() == "Job Planning" && r.getColumn() == 4 && r.getValue() == "Complete and Sent") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Complete and Sent");
var target = targetSheet.getRange(targetSheet.getLastRow() + 2, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
}
the problem is that I can't do something like --
if(name.getValue() == "Miguel") {
}
As there is more that 1 name in the same textbox or cell.
You need to use the event object user
This will return you the active user
Mind that due to restricitons of simple triggers you will need to set up an installable onEdit trigger by going on Edit > Current project's triggers
Sample code snippet to bind to an installable trigger:
function InstallableEdit(e) {
var ss = SpreadsheetApp.getActive();
var r = e.range;
var s = r.getSheet();
var user = e.user;
if(user == "EMAIL_OF_THE_USER" && s.getName() == "Job Planning" && r.getColumn() == 4 && r.getValue() == "Complete and Sent") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Complete and Sent");
var target = targetSheet.getRange(targetSheet.getLastRow() + 2, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
}
}
This looks like an onEdit() function which could be written something like this and requires the event object of the onEdit() simple trigger.
function onEdit(e) {
const s=e.range.getSheet();
if(s.getName() == "Job Planning" && e.range.columnStart==4 && e.value=="Complete and Sent") {
var targetSheet = e.source.getSheetByName("Complete and Sent");
var target = targetSheet.getRange(targetSheet.getLastRow() + 2, 1);
s.getRange(e.range.rowStart,1,1,s.getLastColumn).moveTo(target);
}
}
onEdit event object

Javascript consolidating if statements

I was told I should consolidate my if statements. I'm not sure how to do this? Also, is there anything else wrong in this script? It is for a google doc script.
function onEdit(e) {
var colorA = "yellow";
var colorB = "#dddddd";
var colorC = "#dddddd";
var sheet = e.source.getActiveSheet();
var range = e.source.getActiveRange();
// 3 is column C
if (range.getColumn() == 3) {
if (range.getValue() != "") {
sheet.insertRowAfter(range.getRow());
var r = range.getRow() + 1;
sheet.getRange("A" + r + ":H" + r).setBackgroundColor(colorC);
}
}
if (e.source.getActiveRange().getColumn() == 3 ||
e.source.getActiveRange().getColumn() == 8) {
var rows = sheet.getMaxRows();
//two ranges
//column C
var rangeC = sheet.getRange("C1:C"+rows);
var valuesC = rangeC.getValues();
//column H range
var rangeH = sheet.getRange("H1:H"+rows);
var colorH = rangeH.getBackgroundColors();
var valuesH = rangeH.getValues();
//iterate over each row in column C and H
//then change color
for (var row = 0; row < valuesC.length; row++) {
//check for columnC and column H
if (valuesC[row][0] != "" && valuesH[row][0] == "") {
colorH[row][0] = colorA;
} else if (valuesH[row][0] != "") {
colorH[row][0] = colorB;
}
}
sheet.getRange("H1:H" + rows).setBackgroundColors(colorH);
}
}
​
Here is the other one
ss = SpreadsheetApp.getActiveSpreadsheet();
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [ {name: "New PO", functionName: "NewPO"}];
ss.addMenu("New PO", menuEntries);
}
function NewPO() {
SpreadsheetApp.getActiveSheet().insertRowsBefore(1,6);
// Adjust this range accordingly, these are the cells that will be
// copied. Format is getRange(startRow, startCol, numRows, numCols)
ss.getSheetByName("PO Form").getRange(1, 1, 6, 8)
.copyTo(SpreadsheetApp.getActiveSheet().getRange(1, 1, 6, 8));
}
function onEdit(e) {
var ss = e.source.getActiveSheet();
var r = e.source.getActiveRange();
// 1 is A, 2 is B, ... 8 is H
if (r.getColumn() == 8 && r.getValue() == "x") {
r.setValue(Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd"));
}
}
​
Besides what murray noted, there are several instances where you repeat the same expression:
if (e.source.getActiveRange().getColumn() == 3 ||
e.source.getActiveRange().getColumn() == 8) {
could be:
var col = e.source.getActiveRange().getColumn();
if(col == 3 || col == 8) {
This applies to a lesser extent to:
if (valuesC[row][0] != "" && valuesH[row][0] == "") {
colorH[row][0] = colorA;
} else if (valuesH[row][0] != "") {
colorH[row][0] = colorB;
}
which could be (for instance):
var hRow = colorH[row];
if (valuesC[row][0] != "" && valuesH[row][0] == "") {
hRow[0] = colorA;
} else if (valuesH[row][0] != "") {
hRow[0] = colorB;
}
only thing i can see:
// 3 is column C
if (range.getColumn() == 3) {
if (range.getValue() != "") {
// 3 is column C
if (range.getColumn() == 3 && range.getValue() != "") {

Categories