Automatically populating date in google sheets - google-apps-script

I am trying to get the date to populate in a column AC (29) of the google sheet, when I enter deployed in Column Y (25). Here is the script I've currently created, and can't figure out why it's not working.
function onEdit(e) {
var colToWatch = 25, colToStamp = 29;
var valueToWatch = "Deployed";
if (e.range.columnStart === colToWatch && (e.value === valueToWatch || typeof e.value == 'object'))
e.source.getActiveSheet()
.getRange(e.range.rowStart, colToStamp)
.setValue(typeof e.value === 'object' ? null : new Date());
}

columnStart() and rowStart() are not methods available to the Range object in Google Apps Script. You need to use getColumn() and getRow() instead. Checking the value type multiple times seems unnecessary given that you're already using ===, and it's not listed in your problem description, so I'm removing it. Moreover, the typeof value is necessarily going to be a string–it will never be an object.
function onEdit(e) {
var colToWatch = 25, colToStamp = 29;
var valueToWatch = "Deployed";
if (e.range.getColumn() === colToWatch && e.value === valueToWatch) {
e.range.getSheet().getRange(e.range.getRow(), colToStamp).setValue(new Date());
}
}

Related

Problem with script not unchecking boxes when checking another one in google sheet

Trying to set up an onedit script for google sheet that unchecks all tickboxes in one column when a new one in the same column is ticked in.
function onEdit(e){
if(e.range.columnStart != 1 || e.value == "FALSE") return;
for(var i=1;i<100;i++){
if(i == e.range.rowStart) continue;
// Here I am trying to check if the cell has the value TRUE and if so change it to FALSE.
if(e.source.getActiveSheet().getRange(i,1).value == "TRUE"){
e.source.getActiveSheet().getRange(i,1).setValue("FALSE");
}
}
}
I'm going through the cells with an FOR statement and try to check if they have the value TRUE and if so change it to FALSE. It seems to not detect any TRUE though. If I remove the IF statement it does keep the newly checked box checked but fills the whole column with the FALSE value.
What is it that I am missing or have misunderstood trying to use the IF statement to detect TRUE and change it to FALSE?
Managed to solve it. Instead of checking for the value TRUE I tried to use the isChecked() function and it works.
function onEdit(e){
if(e.range.columnStart != 1 || e.value == "FALSE") return;
for(var i=1;i<100;i++){
if(i == e.range.rowStart) continue;
if(e.source.getActiveSheet().getRange(i,1).isChecked() == true){
e.source.getActiveSheet().getRange(i,1).setValue("FALSE");
}
}
}
It should be a bit faster this way:
function onEdit(e) {
const sh = e.range.getSheet();
if (sh.getName() != "Your Sheetname" || e.range.columnStart != 1 || e.value == "FALSE") return;
let vs = sh.getRange(1, 1, sh.getLastRow(), 1).getValues()
vs.forEach((r, i) => {if (i + 1 != e.range.rowStart) { vs[i][0] = "FALSE" }});
sh.getRange(1, 1, vs.length, vs[0].length).setValues(vs);
}
If you your using its in a bunch of sheets, you could use:
if(!~['Sheet1','Sheet2].indexOf(sh.getName()) || e.range.columnStart != 1 || e.value == "FALSE") return;

conditional formatting with checkboxes

Is there a way to format a checkbox so that when it is "checked", The data in the column beside it will be moved to another column?
function onEdit(e) {
const sh = e.range.getSheet();
if (sh.getName() == 'Sheet1' && e.range.columnStart == 1 && e.value == 'TRUE') {
e.range.offset(0, 5).setValue(e.range.offset(0, 1).getValue());
e.range.offset(0, 1).setValue('');
e.range.setValue('FALSE');
}
}
As player0 mentioned, for this to work, you need Google Apps Script's Spreadsheet Service with the help of Event Objects.
The code below should work on the sample data below.
Sample data:
Code:
function onEdit(e) {
var sheet = e.source.getActiveSheet();
var range = e.range;
var column = range.getColumn();
// if edited data is in Sheet1 Column A and having value TRUE (checkbox ticked)
if (sheet.getName() == "Sheet1" && column == 1 && e.value == "TRUE") {
// get 1 column to the right of checkbox and assign to dataValue
var dataRange = range.offset(0, 1);
var dataValue = dataRange.getValue();
// get 3 columns to the right of dataValue and assign to transferToRange
var transferToRange = dataRange.offset(0, 3);
// set dataValue as value to transferToRange
transferToRange.setValue(dataValue);
// remove data from dataRange after
dataRange.setValue("");
}
}
Output:
Note:
Above code is just one of the many possible solutions you can create via Apps Script.
Read Apps Script for more information.
References:
Spreadsheet Service
Event Objects

How to make onEdit scripts that can set values in protected range, so that I can share my spreadsheet with other users?

I'm writing a simple onEdit scripts to set time on column A, when users modify a specific column. It works just fine, if I share the spreadsheet with edit permission. However, I don't want the users to edit the recorded time on Column A.
Thus, I protect the time recording range in column A. The onEdit function doesn't work for users only have view permission on the protected Range. It will be a great help, if anyone can provide a guide how to make the onEdit function set value on protected range for users with view permission. I've read some article on using Oath2.0, but I'm not sure how to implement this.
I post the code below for your reference:
function onEdit(e) {
var s = SpreadsheetApp.getActiveSheet();
var sheet_name = s.getSheetName();
var added_date = new Date()
var added_time = Utilities.formatDate(added_date, SpreadsheetApp.getActive().getSpreadsheetTimeZone(), "hh:mm a")
if (sheet_name == "Monday" || sheet_name == "Tuesday" || sheet_name == "Wednesday" || sheet_name == "Thursday"
|| sheet_name == "Friday" || sheet_name == "Saturday" || sheet_name == "Sunday") {
rng = s.getRange("B7:B100");
var edit_row = e.range.getRow();
var edit_col = e.range.getColumn();
if (edit_col == rng.getColumn() && edit_row >= rng.getRow() && edit_row <= rng.getLastRow()) {
s.getRange(edit_row, edit_col - 1).setValue(added_time);
}
else {
return;
}
}
}

How to use onEdit trigger to produce timestamp in a dynamic cell using vlookup?

I'm trying to build a glorified to-do list for posting to various job boards. I have a sheet with all website properties, including a unique ID for each property, as well as columns for when the most recent post was submitted (where the timestamp will land). The to-do list is a tab called Posting Tasks, and includes the same unique ID, as well as a checkbox to mark that post as complete.
Database tab
I'm using the onEdit trigger to produce a timestamp on the Database sheet (column K) when cells in Column A of Posting Tasks sheet are 'true'. However, the timestamps are fixed to the row of the corresponding trigger. I'd like them to be relative based on a matching (vlookup) of the unique ID (column C in the trigger sheet, and column B in the Database sheet), since the Posting Tasks sheet changes daily.
Posting tasks
I'm using the below script, but don't know how to change the e.range.rowStart to be relative and lookup corresponding values. Any ideas?
function onEdit(e) {
var sheetsToWatch= ['Posting Tasks', 'Database'
],
columnToWatch = 1,
columnToStamp = 11;
if (e.range.columnStart !== columnToWatch
|| sheetsToWatch.indexOf( e.source.getActiveSheet().getName() ) === -1
|| !e.value)
return;
e.source.getSheetByName('Database')
.getRange(e.range.rowStart, columnToStamp)
.setValue(new Date());
}
Try this:
function onEdit(e) {
e.source.toast('flag1');
var sh=e.range.getSheet();
var name=sh.getName();
if(name=='Posting Tasks' && e.range.columnStart==1 && e.value) {
e.source.toast('flag2');
var id=e.range.offset(0,2).getValue();
var tsh=e.source.getSheetByName('Database');
var idA=tsh.getRange(3,2,tsh.getLastRow()-2,1).getValues().map(function(r){return r[0]});
var row=idA.indexOf(id)+3;
var tsh=e.source.getSheetByName('Database').getRange(row,11).setValue(Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "dd/MM/yyyy HH:mm:ss"));
}
}
Andrew Karp Modification:
var sh = e.source.getActiveSheet();
if (sh.getName() !== 'Posting Tasks' || e.range.columnStart !== 1 || e.range.rowStart < 3 || e.value !== 'TRUE') return;
e.range.offset(0, 0).clearContent() } }

Google Sheets, Moving from one Tab to another via input

I am trying to get this script working where it will work when I put a "yes" or "no" for a choice it will move based on the "yes" to the targeted sheet "Completed Dispatch"
The original sheet is called "Tracker" and the column that I need it to check the "Yes or No" is called "Dispatched"
It doesn't work and I am not that great in GAS
The actual problem I am having is when I make the decision from no to Yes it does not move the row and does not do anything at all.
function onEdit(e) {
var ss = e.source,
sheet = ss.getActiveSheet(),
range = e.range,
targetSheet,
columnNumberToWatch = 9; // column A = 1, B = 2, etc
if (sheet.getName() === "Tracker" && e.value === "Dispatched" && e.range.columnStart === columnNumberToWatch) {
targetSheet = "Completed Dispatch"
} else if (sheet.getName() === "Completed Dispatch" && e.value === "Incomplete" && e.range.columnStart === columnNumberToWatch) {
targetSheet = "Tracker"
}
ss.getSheetByName(targetSheet)
.appendRow(sheet.getRange(e.range.rowStart, 1, 1, sheet.getLastColumn())
.getValues()[0])
sheet.deleteRow(e.range.rowStart);
}