How do I make my script work only in specific rows? - google-sheets

I have an automated time stamp script that works when clicking a checkbox, a time stamp is populated in the cell next to it. How do I make my script work on certain rows?
Example: Column C has check boxes, Column D is populated with dates when Check boxes are marked.
What I want my TimeStamp script to do:
Work only on Row 11, all the way down.
var COLUMNTOCHECK = 3;
var DATETIMELOCATION = [0, 1];
var SHEETNAME = 'Training Dash'
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
if( sheet.getSheetName() == SHEETNAME ) {
var selectedCell = ss.getActiveCell();
if (selectedCell.getColumn() >= COLUMNTOCHECK && selectedCell.getColumn() % 2 == 1) {
var dateTimeCell = selectedCell.offset(DATETIMELOCATION[0],DATETIMELOCATION[1]);
dateTimeCell.setValue(new Date());
}
}
}

if I understood correctly, this should help.
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
if( sheet.getSheetName() == SHEETNAME ) {
var selectedCell = ss.getActiveCell();
if (selectedCell.getColumn() >= COLUMNTOCHECK && selectedCell.getColumn() %
2 == 1 && selectedCell.getRow() > 10) {
var dateTimeCell =
selectedCell.offset(DATETIMELOCATION[0],DATETIMELOCATION[1]);
dateTimeCell.setValue(new Date());
}
}
}

Related

Copy formula to last row of column A (In Sheets )

I am very new to using macro's in google sheets, and struggling to replicate easy VBA codes
I am trying to find the last row of column A, then add formulas to various columns and copy down to the last row.
In VBA it is as simple as **("B2:B" & Lastrow).select
However, this is not so straight forward.
I have found and manipulated the following, however it always goes to the very last row rather than the last row of data
var sheetName = "Week 1";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName);
spreadsheet.getRange('E7').activate()
.setFormula('=iferror(if(match($B7,Names,0),VLOOKUP($B7,Special,3,false),0),0)');
var lr = sheet.getLastRow();
var fillDownRange = sheet.getRange(7, 5, lr-1);
sheet.getRange('e7').copyTo(fillDownRange);
What needs to be done to the above so that it only fills down to the last row of data in column A
All and any help greatly appreciated
EDIT-
I have update the code, (Obviously wrongly) and added to the s/sheet under tools, copy on tab
var sheetName = "Week 1";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName);
var lastRow = sheet.getLastRow();
var range = sheet.getRange("A7" + lastRow);
if (range.getValue() !== "") {
return lastRow;
} else {
return range.getNextDataCell(SpreadsheetApp.Direction.UP).getRow();
}
var lr = getLastDataRow(sheet,"A")
//Saturday Formula
spreadsheet.getRange('E7').activate()
.setFormula('=iferror(if(match($B7,Names,0),VLOOKUP($B7,Special,3,false),0),0)');
var fillDownRange = sheet.getRange(7, 5, lr-1);
sheet.getRange('E7').copyTo(fillDownRange);
as between these two rows is a light bulb sayingabout unreachable code, and everything after and including Var = lr** is greyed out
}
var lr = getLastDataRow(sheet,"A")
Correction of your first code
function myFunction() {
var sheetName = "Week 1";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName);
sheet.getRange('E7').setFormula('=iferror(if(match($B7,Names,0),VLOOKUP($B7,Special,3,false),0),0)');
var lr= sheet.getRange('A7').getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow()
var fillDownRange = sheet.getRange(7, 5, lr-6,1);
sheet.getRange('E7').copyTo(fillDownRange);
}
Instead of var lr = sheet.getLastRow(); , try
lr = sheet.getRange('A1').getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow()
change A by E if needed.
edit : Change A1 by A7 si A4 and A6 are empty.
If you have discontinous value, prefer
function getLastDataRow(sheet,col) {
// col in letter
var lastRow = sheet.getLastRow();
var range = sheet.getRange(col + lastRow);
if (range.getValue() !== "") {
return lastRow;
} else {
return range.getNextDataCell(SpreadsheetApp.Direction.UP).getRow();
}
}
and
lr = getLastDataRow(sheet,"A")

Deleting Row Based on Cell Values

I am attempting to create a additional menu option that allow me to automatically delete all rows that contain the text "Complete" in Column D. The script I have runs and finishes with no errors but does not delete the rows. I tried to find help on line and tweaked the code but same result
function readRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var rowsDeleted = 'Complete';
for (var i = 'Complete'; i <= numRows - 1; i++) {
var row = values[i];
if (row[2] == 'Complete' || row[2] == '') {
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}
};
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Delete Rows where column shows Complete Text",
functionName : "readRows"
}];
sheet.addMenu("Script Center Menu", entries);
};

Auto hide/unhide rows Google Spreadsheet

I'm trying to make a google spreadsheet script.
When the value of a cell changes to a specified value I want it to trigger hide/unhide rows.
Here is my script so far:
function autoHide() {
var ss = SpreadsheetApp.getActive()
var sheet = SpreadsheetApp.getActiveSheet()
var cell = ss.getActiveCell()
var cell1 = ("C12");
if (cell1 == "Others" ) {
ss.getActiveSheet().showRows(14, 3);
}
else if (cell1 == "Additional Discount/s (over and above 25%)" ) {
ss.getActiveSheet().showRows(17, 4);
}
else {
ss.getActiveSheet().hideRows(14, 7)
}
}
Your question is a little unclear about some points so I assumed you wanted to track the value of cell C12 when C12 is edited.
function autoHide() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
var cell = ss.getActiveCell();
var value = cell.getValue();
var cellRef = cell.getA1Notation();
if (cellRef !== "C12") return;
// This isn't the cell we are looking for, stop executing the function.
if (value === "Others" ) {
sheet.showRows(14, 3);
} else if (value === "Additional Discount/s (over and above 25%)" ) {
sheet.showRows(17, 4);
} else {
sheet.hideRows(14, 7);
}
}
If you want to run the function anytime any cell is edited, use this code.
Useful if C12 is a formula and is not updated manually.
function autoHide() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
var cellRef = "C12";
var cell = sheet.getRange(cellRef);
var value = cell.getValue();
if (value === "Others" ) {
sheet.showRows(14, 3);
} else if (value === "Additional Discount/s (over and above 25%)" ) {
sheet.showRows(17, 4);
} else {
sheet.hideRows(14, 7);
}
}

Automatically stamp username who made changes to Google Sheet

I have a Google Sheet that I would like to automatically stamp the username of the person who last made a change to a specific row. I currently have this code to work as I like to do the same function with the time:
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
var r = s.getActiveCell();
if( r.getColumn() != 2 ) { //checks the column
var row = r.getRow();
var time = new Date();
time = Utilities.formatDate(time, "GMT-07:00", "yyyy-MM-dd, hh:mm:ss");
SpreadsheetApp.getActiveSheet().getRange('G' + row.toString()).setValue(time);
};
};
Assuming this is somewhat close to what I require for the username, I tried using this:
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
var r = s.getActiveCell();
if( r.getColumn() != 2 ) { //checks the column
var row = r.getRow();
var time = new Date();
time = Utilities.formatDate(time, "GMT-07:00", "yyyy-MM-dd, hh:mm:ss");
SpreadsheetApp.getActiveSheet().getRange('H' + row.toString()).setValue(Session.getActiveUser);
};
};
But that spits out some weird command that is clearly broken.
How can I do this?
This is the sheet I am working on: https://docs.google.com/spreadsheets/d/1VifU8AJWuPn53-_JCQKbPTjHxKDArkyG0G7zt1wzmPg/edit?usp=sharing
Is this what you are trying to do?
function onEdit(e) {
var r = e.range;
if( r.getColumn() != 2 ) { //checks the column
var time = new Date();
time = Utilities.formatDate(time, "GMT-07:00", "yyyy-MM-dd, hh:mm:ss");
SpreadsheetApp.getActiveSheet().getRange(r.getRow(),8).setValue(Session.getActiveUser().getEmail());
}
}

Google Script send email from a sheet in a specific date

follow script is used to get a date from a google sheet and if this date is equal to today or tomorrow generate an automatic email to my address in order to remind me.
function getVal() {
var ss = SpreadsheetApp.getActiveSpreadsheet(),
sheet = ss.getActiveSheet();
sheet.setActiveRange(sheet.getRange("A1"));
var range = sheet.getDataRange(),
formulas = range.getValues();
var toDay = new Date();
for (var r=0; r<formulas.length; r++) {
for (var c=0; c<formulas[r].length; c++) {
//var value = sheet.getRange(r,c).getValue();
var value = range.getCell(r, c).getValue();
Logger.log(value);
if (value == "AAAA")
{
var index = r+2;
value = sheet.getRange(index,c).getValue();
while (value != "" || index >= formulas.length)
{
if (DateDiff.inDays(value,toDay)==1 || DateDiff.inDays(value,toDay)==0)
{
MailApp.sendEmail(myAdress,subject, text);
}
index = index + 3;
value = sheet.getRange(index,c).getValue();
}
}
}
}
}
var DateDiff = {
inDays: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2-t1)/(24*3600*1000));
},
inWeeks: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2-t1)/(24*3600*1000*7));
},
inMonths: function(d1, d2) {
var d1Y = d1.getFullYear();
var d2Y = d2.getFullYear();
var d1M = d1.getMonth();
var d2M = d2.getMonth();
return (d2M+12*d2Y)-(d1M+12*d1Y);
},
inYears: function(d1, d2) {
return d2.getFullYear()-d1.getFullYear();
}
}
when i run the script it fail in the line :
var value = range.getCell(r, c).getValue();
have you any suggestion in order to fix this bug?
thanks
Mario
try to change the line with:
var value = formulas[r][c]
it should also do less request to google spreadsheet and run faster.

Resources