Input datetime when double click on a cell - google-apps-script

I have 6 columns in google sheet doc.
(A) Date (B)Order (C)ID DEPARTMENT (D)Time sent (E)Time received (F)Delivery time
I want each time I double click column a cell under Time Sent or Time received columns - current date and time is inputted.
Date and time should stay forever (like the time of double click).
Can you please help me to create this code?

Explanation:
There is no doubleClick event in Google Apps Script.
However, you can use onSelectionChange to capture single click events.
The following script will set the value of a cell in column D or E with the current datetime upon a single click.
To prevent a cell from being updated after you selected again, you can add an extra condition to set values only when the selected cell is empty. If the cell has already a date in it, then don't update it. In this way, the original/first datetime will remain intact.
Solution:
function onSelectionChange(e) {
const as = e.source.getActiveSheet();
const row = e.range.getRow();
const col = e.range.getColumn();
if (as.getName() == 'Sheet1' && row>1 && [4,5].includes(col) && e.range.getValue()==''){
e.range.setValue(new Date());
}
}
To use this solution, simply copy & paste it to the script editor and save the changes. After that, it will automatically be used upon single click events. In this solution I used Sheet1 as the sheet name. Feel free to change that according to your specific case.

Related

How to round a cell after user input in Google Sheets?

It should be a very simple function but I can't find anything that functions the way I need.
I want to have a cell where a number can be entered, then after its entered it is replaced by that number rounded to its nearest 0.25
For example:
I enter 5.26 into cell A1, after i press enter the cell now says 5.25
This should only happen with cell A1, so if i enter 5.26 elsewhere it will stay as 5.26
Any help? Thanks in advance and sorry if this a common question.
This can be achieve by using Google Apps Script and onEdit Trigger.
Follow these steps to achieve the desired goal.
In your Google Sheet, go to Tools -> Script editor.
Delete any code in the script editor and paste code provided below.
Click Save.
Go back to your Google Sheet and type numbers in cell A1.
Press enter and wait the value to change.
Code:
function onEdit(e) {
var range = e.range;
var input = e.value;
if(range.getA1Notation() == "A1" && !isNaN(input)){
var number = (Math.round(input * 4) / 4).toFixed(2);
range.setValue(number);
}
}
Demo:
Triggers let Apps Script run a function automatically when a certain
event, like opening a document, occurs. In your case, we need to use
onEdit since it runs automatically when a user changes the value of
any cell in a spreadsheet. Apps Script passes the
triggered function an event object that contains information about the
context in which the event occurred (usually named e).
In the demo above, I created a condition that will check if the edited cell is A1 and if the inputted value is number. If both satisfy the condition, the script will calculate the value and use range.setValue() to change the value of A1.
References:
Class Range
Range.setValue(value)
Simple Triggers
Event Object

Google spreadsheet and conditional checkbox

I own two columns A contains numbers and B contains checkboxes.
If A = 0 I would like to auto-check the checkbox else I would like to let the user checks it (or not). But when I use a IF statement in the checkbox cell I can't check it myself (I lose its behavior).
I need something like :
=IF(A1=0;TRUE;#keep checkbox classical behavior#)
How could I do that ?
Explanation:
I don’t think you can accomplish this behaviour with a google sheet formula without compromises. This auto-check behaviour you describe can only be achieved with a trigger; activated when you edit a cell in column A.
I believe you need to take advantage of Google Apps Script and in particular use an onEdit trigger.
Solution:
Click on Tools => Script editor on the top menu of the spreadsheet file, copy & paste the below code into a blank script and click on save. After that, when you put 0 in column A, the corresponding row in column B will be checked. This behaviour will be applied to a sheet named Sheet1. Change that part of the code to apply it to a sheet with a different name.
function onEdit(e) {
const sheet= `Sheet1`; // change Sheet1 to the name of your sheet
const as = e.source.getActiveSheet();
const col = e.range.getColumn();
const row = e.range.getRow();
if(as.getName()==sheet && col==1 && e.value==0){
as.getRange(row,2).setValue(true);
}
}
Unfortunately you can't do what you wish in one formula. As you discovered, interacting with a checkbox in a cell with a formula is essentially like typing over that formula and would overwrite it.
My suggestion to accomplish this is to add an extra column and use the formula you suggested as the final check to what the state of that row should be (true or false).
=if(A2=0,True,B2)
Where A is your values and B is your user entered checkbox.
Here's a sample sheet with it loaded as an array formula. https://docs.google.com/spreadsheets/d/1z8A4WqVppcxGlGiM3l-kW6q88iYBE6LGw5PyVfRh9Js/edit#gid=0
You could then format that column as checkboxes, but if you use an array formula it will get tripped up when it thinks the checkbox is blocking the formula for each new row.

Trigger for Google Sheets

I have a Google Spreadsheet that will send me an email once a figure in a cell reaches a certain number. I tried setting up triggers, but it doesn't work for what I need.
I need some code to open up the spreadsheet every 2 hours and then check the cell. If the number is greater than what I want, then it emails me.
Does anyone have their own code that I can add to my email function that will do this?
Any help would be appreciated.
The following code will check the value inside a cell and if the number inside the cell is greater than your specified limit, it will send an alert to the given email address.
function sendEmail() {
// get the number in the specified cell. Here I used the cell B2 for reference
var sheet = SpreadsheetApp.getActiveSheet();
var rowNum = sheet.getRange('B2').getValue(); //take the value to rowNum
if(rowNum > your_number_limit ){
var message = 'This is your Alert email!'; // Second column
var subject = 'Your Google Spreadsheet Alert';
MailApp.sendEmail("youremailaddress", subject, message);
}
// Send Alert Email.
}
To check the value every 2 hours do the following
From the Script Editor,
choose Resources > Current project's triggers. You see a panel with the message No triggers set up. Click here to add one now.
Click the link that says No triggers set up. Click here to add one now.
Under Run, select the function you want executed on schedule.
Under Events, select Time-driven.
On the first drop-down list that appears, select Hour timer
set interval of hours for 2 hours.
Click Save.
To ensure that the script runs at the correct time for a particular time zone, click File > Properties, select a time zone and click Save.

How can I link two columns so that an edit in either is reflected in the other in a Google Spreadsheet

I have a monthly schedule in a Google Spreadsheet with some contact info in "A:D", two columns for the current date in "E:F", and then the dates for the current month in the rest of the columns. Each day has two columns associated to it, one for the schedule and one for notes, and has the date at the top of the schedule column. What I would like to do is have the current date's schedule and notes reflected in columns "E:F" such that editing cells in either the original column, or in "E:F", is reflected in the other, and have columns "E:F" update to the appropriate date each day. From what I can gather I'll need a script to do this, but I don't have enough knowledge of JavaScript to know how to do so.
Here is a link to an example of this spreadsheet.
Example Schedule
Thanks for any help.
Edit: Rows "1:3" are static and don't need to be linked between columns.
Place something like this into the script that is attached to the sheet you want to work with
function onEdit(e) {
var range = e.range;
if (range.getColumn() == '6') {
var startingCell = SpreadsheetApp.getActiveSheet().getActiveCell();
var secondCell = startingCell.offset(0,1);
var userInput = startingCell.getValue();
secondCell.setValue(userInput);
}
}
This will take the input from the column 'E' or column '6' and paste the input directly into the following column when it is edited.

Script suggestion to insert a timestamp when button is pressed

I am new to coding and I would like your help in developing a simple script for Google Sheets. What I would like it to do is (see example):
when I press the "START" button the current time automatically gets inserted in the next available empty cell in column A in HH:MM:SS. So every time I press the button a new current time stamp gets added in the next empty cell on column A. The "STOP" button would work exactly the same just that it would and the information in column B.
I have found a code for Visual Basic that does in Excel exactly what I would like to do, I just don't know how to do this is Google Sheets. The code for the "Start" button in Visual Basic for Excel is the following:
Sub StartTime()
nr = ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUP).Row + 1
Cells(nr, 1) = Time
End Sub
This simple solution assumes that the list of start stop times will always be the furthest down of everything in that Sheet (or ideally the only things).
If that is not the case you'll need to change it to find the last row of the range you need when inserting the data..
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
function startTime() {
ss.getRange(ss.getLastRow() + 1, 1).setValue(new Date());
}
function stopTime() {
ss.getRange(ss.getLastRow(), 2).setValue(new Date());
}
The spreadsheet tab Sheet1 will have the same layout as yours with two pictures inserted and Columns A and B formatted as Times.
Now, if you select the picture of the button that you inserted there's a little arrow in the top right hand corner.
Here you click "Assign Script" and type the name of the function (startTime and stopTime respectively).