Using IF to evaluate an specific string - javascript

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

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

How to define specific sheet - .getsheetbyname error

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

OnEdit need to send emails from a column

When in column O cell is mentioned "Shipped" I want to send emails in column C (with corresponding row)
Need support to get this script right
function sendEmail(e) {
var thisSheet = e.source.getActiveSheet();
if (thisSheet.getName() !== 'CRM' || e.range.columnStart !== 15 || e.range.rowStart == 1 || e.value !== 'Shipped') return;
var body, headers = thisSheet.getRange(1, 1, 1, 6)
.getValues()[0],
thisRow = thisSheet.getRange(e.range.rowStart, 1, 1, 6)
.getValues()[0],
recipientsEMail = (thisSheet.getRange.columnStart !==3 || e.range.rowStart ==1)
.getValues()[0],
recipients = recipientsEMail,
subject = "Your Vehicle is shipped " + e.source.getName(),
body = "",
i = 0;
while (i < 6) {
body += headers[i] +' - ' + thisRow[i] +'\n';
i++;
}
MailApp.sendEmail(recipients, subject, body);
}
My sheet
See if this works:
function sendEmail(e) {
var thisSheet = e.source.getActiveSheet();
if (thisSheet.getName() !== 'CRM' || e.range.columnStart !== 15 || e.range.rowStart == 1 || e.value !== 'Shipped') return;
var body, headers = thisSheet.getRange(1, 1, 1, 15)
.getValues()[0],
thisRow = thisSheet.getRange(e.range.rowStart, 1, 1, 15)
.getValues()[0];
var recipients = thisRow[2];
var subject = "Your Vehicle is shipped " + e.source.getName(),
body = "Dear Customer,\n\nYour vehicle has been shipped from Japan";
MailApp.sendEmail(recipients, subject, body);
}
You didn't say how much columns you need to use to construct the body of the email but that can be easily changed if you increase the number in the while loop.

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