Auto Timestamping a cell when certain cells have been editted - scripting

I am trying to make it so if any cells between I4:AI503 on the spreadsheet listed below gets edited, it will update the timestamp in column AK.
The following code is used in this spreadsheet:
function onEdit() {
var ss = SpreadsheetApp.getActiveSheet();
if (ss.getName() == "RS3 Points") {
var range = ss.getActiveCell();
var col = range.getColumn();
var row = range.getRow();
if ((col >= 9) && (col <= 35) && (row > 3)) {
var ActiveCell = ss.getRange().getRow()
var TimestampCol = ss.getLastColumn()
var formattedDate = Utilities.formatDate(new Date(), "GMT", "dd-MM-yyyy");
var GetRange = ss.getRangeByName(ActiveCell, TimestampCol)
GetRange.setValue(formattedDate)
}
}
}

Make sure column AK is formatted as Date. Then, try this code:
function onEdit(e) {
if (e.source.getActiveSheet().getName() !== "RS3 Points" ||
e.range.columnStart < 9 || e.range.columnStart > 35 ||
e.range.rowStart < 4 || e.range
.columnStart < e.range.columnEnd) return;
e.range.offset(0, 37 - e.range.columnStart).setValue(new Date());
}

Related

GSheets Multi-Select Dropdown affects entire worksheet instead of a specific sheet

I am new to this and I found an old post with this code. I am currently using it but it affects all tabs of my spreadsheet. Is there a way for it to work on a specific tab only? Thank you.
`const separator = ', ';
const dvType = SpreadsheetApp.DataValidationCriteria.VALUE_IN_RANGE;
function onEdit(e) {
if (e.value != null && e.oldValue != null && e.value !== "") {
var dataValidation = e.range.getDataValidation();
if(dataValidation != null && dataValidation.getCriteriaType() == dvType &&
e.value.indexOf(separator) < 0 && e.oldValue.indexOf(e.value) < 0) {
e.range.setValue(e.oldValue + separator + e.value);
}
}
}`
I tried to change the criteria but since I am new to coding, I always get error.

Google script to copy specific cell values

I need a little help with a google spreadsheet script. Currently I'm using a script to copy entire row if contition is met(checkbox TRUE/FALSE). If different condition is met(different checkbox), i need to copy that entire row to "Completed" AND the tricky part for me, I need to ONLY copy cells in A,B,P columns in that specific row to sheet "Flash" to position A,B,C.. Could someone help me? Thanks!
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Orders" && r.getColumn() == 9 && 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() == "Orders" && r.getColumn() == 15 && 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).copyTo(target);
var targetSheet = ss.getSheetByName("Flash");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
s.deleteRow(row);
}
}

How to calculate age of users when they are entered their date of birth in react-native?

How to calculate age of users when they are entered their date of birth in react-native?
I want to check user is more then 18 year or not.When they are entered date of birth .
I am using react-native-datepicker for take user's date of birth.
I am trying to calculate age of user using below code but it not work properly .So please help me .How i can achieve this functionality.
calculate_age = (date) => {
var today = new Date();
var birthDate = new Date(date);
console.log("get bod-->",birthDate) // create a date object directly from `dob1` argument
var age_now = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age_now--;
}
console.log('my age', age_now);
return age_now;
}
onDateChange = (date) => {
this.setState({ date: date }, () => {
console.log(date)
if (this.calculate_age(date) < 18) {
alert("You Are Not Eligable")
} else {
}
})
}
Using 'npm i moment' package.I solved this problem.
onDateChange = (date) => {
this.setState({ date: date }, () => {
if (this.calculate_age(Moment(date,"DD-MM-YYYY").format("YYYY-MM-DD")) <= 17 ) {
this.setState({errmsg:"You must be atleast 18 years of old to join."})
}else{
this.setState({errmsg:" "})
}
})
}
You can make a custom function like this:
const getAge = (dateString)=>{
var today = new Date();
var birthDate = new Date(dateString);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
console.log('age: ' + getAge("1980/08/10"));

Report from Invoice template

I created an invoice template in the spreadsheet and used the attached VBA code to create a report from it (essentially it stores certain cell values into another sheet as a tabular report).I would like to port this over to Google Spreadsheet and need help in converting the VBA to corresponding JavaScript. Can you help?
Thanks
Sub InvoiceReport()
Dim myFile As String, lastRow As Long
myFile = “C: \invoices\” & Sheets(“Sheet1”).Range(“B5”) & “_” & Sheets(“Sheet1”).Range(“F1”) & Format(Now(), “yyyy - mm - dd”) & “.pdf”
lastRow = Sheets(“Sheet2”).UsedRange.SpecialCells(xlCellTypeLastCell).Row + 1
‘ Transfer data to sheet2
Sheets(“Sheet2”).Cells(lastRow, 1) = Sheets(“Sheet1”).Range(“B5”)
Sheets(“Sheet2”).Cells(lastRow, 2) = Sheets(“Sheet1”).Range(“F1”)
Sheets(“Sheet2”).Cells(lastRow, 3) = Sheets(“sheet1”).Range(“I36”)
Sheets(“Sheet2”).Cells(lastRow, 4) = Now
Sheets(“Sheet2”).Hyperlinks.Add Anchor: = Sheets(“Sheet2”).Cells(lastRow, 5), Address: = myFile, TextToDisplay: = myFile‘ Create invoice in PDF format
Sheets(“sheet1”).ExportAsFixedFormat Type: = xlTypePDF, Filename: = myFile
Application.DisplayAlerts = False
‘ create invoice in XLSX format
ActiveWorkbook.SaveAs“ C: \invoices\” & Sheets(“Sheet1”).Range(“B5”) & “_” & Sheets(“Sheet1”).Range(“F1”) & “_” & Format(Now(), “yyyy - mm - dd”) & “.xlsx”, FileFormat: = 51‘ ActiveWorkbook.Close
Application.DisplayAlerts = True
End Sub
Figured this out, thankfully I've had to do this before.
This moves the data by getting the invoice information via a map that I made as an object so it's easily looped through. The tricky part was getting the PDF of the sheet.
I accomplished this by first scoping the function with Driveapp.getRootFolder() so I can get an oAuth token later. I utilized the URLFetchApp use the spreadsheets export functionality to retrieve a PDF blob. I then take this blob, name it, convert it to a file, and insert it into your drives root folder.
//Data mapping for the invoice itself
var invoiceDetailsMap = {
'Buyer Name': {
rowIndex: 4,
columnIndex: 1
},
'Invoice Number': {
rowIndex: 0,
columnIndex: 5
},
'Total Amount': {
rowIndex: 35,
columnIndex: 7
},
'Date & Time': {
rowIndex: 0,
columnIndex: 1
}
}
//Entry point for script
function EntryPoint() {
var spreadsheet = SpreadsheetApp.getActive()
var sheet = spreadsheet.getSheetByName('Sheet1');
var dataRange = sheet.getDataRange();
var valuesRange = dataRange.getValues();
var invoiceData = GetInvoiceDetails(valuesRange);
WriteInviceDetailsToSheet(invoiceData);
GetPDF(spreadsheet, invoiceData['File Name']);
}
//Writes the invoice details to the 2nd sheet
function WriteInviceDetailsToSheet(invoiceData){
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet2');
var dataRange = sheet.getDataRange();
var valuesRange = dataRange.getValues();
var columns = GetColumns(valuesRange, dataRange.getNumColumns(), 0);
var arrayToWrite = [[]];
for(var column in columns.columns){
if(typeof invoiceData[column] !== 'undefined'){
arrayToWrite[0].push(invoiceData[column]);
}
}
sheet.insertRowAfter(dataRange.getLastRow());
sheet.getRange(dataRange.getLastRow() + 1, 1, 1, arrayToWrite[0].length).setValues(arrayToWrite);
}
//Gets the invoice details absed on the mappings
function GetInvoiceDetails(valuesRange) {
var output = {};
for(var value in invoiceDetailsMap){
output[value] = valuesRange[invoiceDetailsMap[value].rowIndex][invoiceDetailsMap[value].columnIndex];
}
output['File Name'] = 'Invoice' + output['Invoice Number'] + '_' + FormatDate(output['Date & Time'], 'MM.dd.yyyy');
return output;
}
//Gets the PDF and inserts it into drive
function GetPDF(spreadsheet, fileName){
DriveApp.getRootFolder(); //Scoping
var urlParameters = 'export?exportFormat=pdf&format=pdf&size=letter&portrait=true&fitw=true&source=labnol&sheetnames=false&printtitle=false&pagenumbers=false&gridlines=false&fzr=false&gid=0';
var baseURL = spreadsheet.getUrl();
baseURL = baseURL.replace(/edit$/,'');
var response = UrlFetchApp.fetch(baseURL + urlParameters, {
headers: {'Authorization': 'Bearer ' + ScriptApp.getOAuthToken() }
});
var pdfBlob = response.getBlob().setName(fileName + '.pdf');
var file = DriveApp.createFile(pdfBlob);
DriveApp.addFile(file);
}
//Reformts a date
function FormatDate(date, format)
{
var temp = new Date(date);
var output = Utilities.formatDate(temp, "PST", format);
return output;
}
//Gets a columns object for the sheet for easy indexing
function GetColumns(valuesRange, columnCount, rowIndex)
{
var columns = {
columns: {},
length: 0
}
Logger.log("Populating columns...");
for(var i = 0; i < columnCount; i++)
{
if(valuesRange[0][i] !== ''){
columns.columns[valuesRange[0][i]] = {index: i ,value: valuesRange[0][i]};
columns.length++;
}
}
return columns;
}

Reference entire column, not just one cell Google Sheets

The following will send email notification if F6 is edited on a google sheet. I need help making it send a notification if any cell in column F is edited, not just one cell. I tried ('F6,F7, etc) and I tried F:F, those don't work.
function emailNotification(e) {
var sheet = e.source.getActiveSheet();
if (sheet.getName() !== 'Sheet1' || e.range.getA1Notation() !== ('F6')) return;
return;
var recipient = "mail#google.com";
var subject = 'SUBJECT';
var body = ' BODY OF MAIL ';
MailApp.sendEmail(recipient, subject, body);
};
Please help, I don't know where I'm going wrong. Otherwise the script works like a charm for a single cell. Just not the whole column
This will run code if any cell in column F of Sheet1 is edited.
function onEdit(e)
{
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var sheetName= sheet.getSheetName();
if(sheetName=="Sheet1"){
var editRange = sheet.getActiveRange();
var editRow = editRange.getRow();
var editCol = editRange.getColumn();
var lr = sheet.getLastRow()
var range = sheet.getRange("F1:F"+lr);
var rangeRowStart = range.getRow();
var rangeRowEnd = rangeRowStart + range.getHeight()-1;
var rangeColStart = range.getColumn();
var rangeColEnd = rangeColStart + range.getWidth()-1;
if (editRow >= rangeRowStart && editRow <= rangeRowEnd
&& editCol >= rangeColStart && editCol <= rangeColEnd)
{
Logger.log("Run Code")
//YOUR CODE HERE
}}}