Google Sheets - date auto populate when column in specific sheet is edited - date

Hey I need some help with Google Sheets
In sheet 1 "Inventory" I am trying to have the date auto populate in column 8 or I when data is entered in any cell in column 7 or G below row 3
this is what I have tried/as far as I have gotten
function onEdit(e) {
if (e.range.columnStart == 7 || e.range.rowStart < 4) return;
e.source.getActiveSheet().getRange(e.range.rowStart, 9)
.setValue(Utilities.formatDate(new Date(), "GMT", "dd/MM/yyyy"));
}
Thank you for any help!!

This should fix it:
function onEdit(e) {
if (e.range.columnStart == 7 || e.range.rowStart < 4){
e.source.getActiveSheet().getRange(e.range.rowStart, 9)
.setValue(Utilities.formatDate(new Date(), "GMT", "dd/MM/yyyy"));
}}

Ended up using this:
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Inventory" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 7 ) { //checks the column
var nextCell = r.offset(0, +2);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setNumberFormat("MMM dd/YY")
nextCell.setValue(new Date());
};
};
}

Related

Comparing Dates in Google Scripts with Sheets Input

I am trying to create a pop up to warn a user they must update a part in inventory if the part date is more than 90 days old. The date on the sheet (Cell Q5) is autofilled from another sheet, but that shouldn't matter. The value for the cell on the spreadsheet is 9/2/2021. I've tried many things, but currently I am getting the value for Q5 showing up as NaN .
function CheckInvDate() {
var ss = SpreadsheetApp.getActive().getId();
var partsrange = Sheets.Spreadsheets.Values.get(ss, "BOM!A5:Q5");
var currentDate = new Date();
var parthist = new Date();
parthist.setDate(currentDate.getDate() -90);
for (var i = 0; i < partsrange.values.length; i++){
var name = partsrange.values [i][1]
var partdate = partsrange.values [i][16]
var parthisttime = new Date(parthist).getTime();
var partdatetime = new Date(partdate).getTime();
Logger.log("History " + parthisttime)
Logger.log("Current " + partdatetime)
SpreadsheetApp.flush()
// if (parthist > partdate == "TRUE") {
// SpreadsheetApp.getUi().alert('The price on '+ name + ' is out of date. Please update price and try again.')
// }
}
}
My last log was
[22-07-06 11:50:55:851 EDT] History 1649346655850
[22-07-06 11:50:55:853 EDT] Current NaN
I've seen a number of responses on Stack Overflow, but I can't understand them. They seem to refer to variables that I don't see in code, or commands I haven't seen before and I'm not sure if they are in date.
Try this:
function CheckInvDate() {
const ss = SpreadsheetApp.getActive();
const vs = Sheets.Spreadsheets.Values.get(ss.getId(), "BOM!A5:Q5").values;
let d = new Date();
d.setDate(d.getDate() - 90)
const dv = d.valueOf();
const oldthan5 = vs.map(r => {
if (new Date(r[16]).valueOf() < dv) {
return r;
}
}).filter(e => e);
SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(`<textarea rows="12" cols="100">${JSON.stringify(oldthan5)}</textarea>`).setWidth(1000), "Older Than 90 Days");
}
This outputs a dialog with the rows older than 90 days
I went to try this on my script again after lunch, and for whatever reason I am no longer getting the NaN value. I made one change on the if statement to fix the logic, and now it is working correctly. Not sure what I did, but apparently the coding gods were unhappy with me before.
The only part I changed was
if (parthisttime > partdatetime) {
SpreadsheetApp.getUi().alert('The price on '+ name + ' is out of date. Please update price and try again.')
}

Inserting Date In Cell Upon Checkbox True

I am having issues making this work. I want to insert todays date in colum 4 upon making checkbox true in colum 1. There are multiple rows. I am not seeing the error of my ways. Any suggestions would be appreciated.
Thanks,
function onEdit(e) {
var ss = e.source.getActiveSheet();
var date = new Date();
if (e.range.columnStart != 1 || e.value != "TRUE") return;
ss.getRange(e.range.rowStart,4).setValue(date);
}
Instead of:
E.value != "TRUE"
Simply use:
!e.value
And then instead of:
ss.getRange(e.range.rowStart,4).setValue(date)
You can simply use:
e.range.offset(0,3).setValue(date)
That allows you to eliminate the need for the ss variable as well.

pass decimal column value to populate results from a table using linq query

I have a product table with 20 column. There is a column 'Width' which is decimal data type. I have been calling particular width value in web api controller to populate results. As an example,width values are like below:
Width values
1 0.0015
2 1.0000
3 0.0063
4 1.0100
5 2.0000
6 2.0630
public HttpResponseMessage GetclassByWidthList(Decimal cWidth)
{
using (CrossReferenceTool1Entities cls1 = new CrossReferenceTool1Entities())
{
**var query = (from u in cls1.Products where (u.PrivateOnly == false && u.SelectionTool == true && u.ProductTypeID == 2 && (u.ProductFamilyID == 11 || u.ProductFamilyID == 12 || u.ProductFamilyID == 58 || u.ProductFamilyID == 59 || u.ProductFamilyID == 92) && **u.Width == cWidth**) select u.Class).Distinct().ToList()**;
HttpResponseMessage res;
res = Request.CreateResponse(HttpStatusCode.OK, query);
return res;
}
}
method is working fine. results are reflected if width value is "1.0000" OR "2.0000" but for other values of width it is not populating any result.
Please help me on above query where other decimal values with precision will populate result.
i have tried in browser to populate result:
1.
http://localhost:55481/api/KendoCascading/GetclassByWidthList/1 - result is poupulating
`http://localhost:55481/api/KendoCascading/GetclassByWidthList/1.01 - http-404 error showing
For debugging, your decimal data of Cwidth is being truncated, Please check
As per Ehasanul's suggestion i Just modified my method with putting extra "/" last of that route path
[Route("**api/KendoCascading/GetclassByWidthList/{cWidth:decimal}/**")]
public HttpResponseMessage GetclassByWidthList(decimal cWidth)
{
using (CrossReferenceTool1Entities cls1 = new CrossReferenceTool1Entities())
{
var query = (from u in cls1.Products where (u.PrivateOnly == false && u.SelectionTool == true && u.ProductTypeID == 2 && (u.ProductFamilyID == 11 || u.ProductFamilyID == 12 || u.ProductFamilyID == 58 || u.ProductFamilyID == 59 || u.ProductFamilyID == 92) && u.Width == cWidth) select u.Class).Distinct().ToList();
HttpResponseMessage res;
res = Request.CreateResponse(HttpStatusCode.OK, query);
return res;
}
}
call that url in my angular service http.get method with adding extra "/" in last.
this.getclsWidthList3 = function (cWidth) {
var res;
if (cWidth !== 0.0000) {
res = $http.get("/api/KendoCascading/GetclassByWidthList"+ "/"+ cWidth+"/");
return res;
}
};
and its working great.

Why doesn't this form code work? It seems logical

I'm trying to program a simple form that asks for the following inputs:
mood, age, and gender
It does not matter what I put in the mood prompt. It always comes out positive. For the age, I would like the tab to close if they are under 18 years old. For gender, it goes the same with the mood prompt. Any help would be appreciated.
var userMood = prompt("Hey! How's it going?");
if (userMood = "good" || "Good") {
alert("Awesome! We just need you to fill out a form for security reasons.");
} else if (userMood != "good" || "Good") {
alert("Oh, I'm sorry to hear that... We just need you to fill out a form for security reasons.");
}
var userAge = prompt("What is your current age?");
if (userAge >= "18" ) {
alert("Great! You are the sufficient age to enter this webpage!");
userAge = true;
} else if ( userAge <= "17"){
alert("Sorry, you are too young to view this content...");
window.close();
}
var userGender = prompt ("What is your gender?");
if (userGender = "male" || "Male" || "female" || "Female"){
alert("Great! You're human!");
userGender = true;
} else {
alert("The webpage has perdicted non-human cyber activity, you can no longer continue.");
window.close();
}
i'm going to make my answer verbose to help you learn. for starters, you are using = when you mean to use == or even ===. this is why the mood is always registering as good, because userMood = "good" sets userMood to 'good' and so the expression evaluates to "good", which validates the if statement because FALSE for strings is "" and true is every other string. also userMood = "good" || "Good" doesn't do what you think it does. you need to check both strings like so (userMood === "good" || userMood === "Good").
I tried varying some lines in your code into something below.
var userMood = prompt("Hey! How's it going?");
if (userMood == "good" || userMood == "Good")
alert("Awesome! We just need you to fill out a form for security reasons.");
else
alert("Oh, I'm sorry to hear that... We just need you to fill out a form for security reasons.");
var userAge = prompt("What is your current age?");
if (userAge >= 18 )
{
alert("Great! You are the sufficient age to enter this webpage!");
userAge = true;
}
else if ( userAge < 18 && userAge > 0)
{
alert("Sorry, you are too young to view this content...");
window.close();
}
else if ( userAge < 0 )
{
alert(" ***** prompt message for inputting negative number ****");
window.close();
}
var userGender = prompt ("What is your gender?");
if (userGender == "male" || userGender == "Male" || userGender == "female" || userGender == "Female")
{
alert("Great! You're human!");
userGender = true;
}
else
{
alert("The webpage has perdicted non-human cyber activity, you can no longer continue.");
window.close();
}

Google Script return date when range is edited

I'm currently working on a script for a spreadsheet.
Desired Behavior: When cells F-I are edited, the date of the most recent edit should be returned in cell L of the corresponding row.
I've found two samples of code, the first returns the date in cell L when ANY field in the row is edited. This behavior is acceptable, but not ideal as it applies to any col
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
var r = s.getActiveCell();
if( r.getColumn() != 2 ) {
var row = r.getRow();
var time = new Date();
time = Utilities.formatDate(time, "GMT-05:00", "yyyy-MM-dd");
SpreadsheetApp.getActiveSheet().getRange('L'+row.toString()).setValue(time);
};
};
The second should return the date one cell to the right of the edited column. This behavior is not desired as it may overwrite other important data.
function onEdit(e) {
var s = SpreadsheetApp.getActiveSheet();
var cols = [1, 7, 13, 19, 25, 31]
if (s.getName() !== "Sheet1" || cols.indexOf(e.range.columnStart) == -1) return;
s.getRange(e.range.rowStart, e.range.columnStart + 1)
.setValue(new Date());
};
Any help or advice on editing these to behave more closely to the desired results would be much appreciated. Thanks!
Something like this should work:
function onEdit(e) {
var ss = e.source.getActiveSheet();
var watchedCols = [6, 7, 8, 9]
if (watchedCols.indexOf(e.range.columnStart) === -1) return;
ss.getRange(e.range.rowStart, 12)
.setValue(e.value ? Utilities.formatDate(new Date(), "GMT-05:00", "yyyy-MM-dd") : null)
}
Note that the script will work on every sheet of your google spreadsheet. If you want to limit it to only one sheet (and also exclude edits in the first row (headers ?)), you could try something like this:
function onEdit(e) {
var ss = e.source.getActiveSheet();
var watchedCols = [6, 7, 8, 9];
var watchedSheet = 'Sheet1'; //change name if needed
if (watchedCols.indexOf(e.range.columnStart) === -1 || ss.getName() !== watchedSheet || e.range.rowStart < 2) return;
ss.getRange(e.range.rowStart, 12)
.setValue(e.value ? Utilities.formatDate(new Date(), "GMT-05:00", "yyyy-MM-dd") : null)
}
Hope that helps ?