Google apps script onedit even not grabbing column title - google-apps-script

function onEdit(event)
{
var timezone = "GMT-6";
var timestamp_format = "HH:mm:ss"; // Timestamp Format.
var sheet = event.source.getActiveSheet();//Name of the sheet where you want to run this script.
var actRng = event.source.getActiveRange();
var editColumn = actRng.getColumn();
var index = actRng.getRowIndex();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var dateCol = headers[0].indexOf(timeStampColName);
var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
var updateColName = sheet.getRange( 1, editColumn).getValue ();
var timeStampColName = "null";
if (updateColName == "PACK STARTED BY") {timeStampColName = "Pack Start";}
if (dateCol > -1 && index > 1 && editColumn == updateCol) { // only timestamp if 'Last Updated' header exists, but not in the header row itself!
var cell = sheet.getRange(index, dateCol + 1);
var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
cell.setValue(date);
}
}
Above is the code I am running in my sheet. Now This on edit event is supposed to grab the column title from the currently active cell. then using if statements should be putting a static value into the timestamp column name so I can put timestamps in my sheet. I am having a really hard time debugging this since I don't know how to debug on edit in the google scripting screen. So I can't tell what is not right about the code. Right now it does nothing.

Related

Google Script: How to make a cursor move to the next row in column A

I'm trying to make an Entry/Exit logbook, where staff would tag their ID cards to the card-reader at the entrance, which will populate a cell in column A, and this will trigger the script to create a day/time stamp.
{
function activeSheetName()
{
return
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();
}
{
var timezone = "GMT+7";
var timestamp_format = "MM/dd/yyyy HH:mm:ss"; // Timestamp Format.
var updateColName = "CardID";
var timeStampColName = "Date/Time";
var sheet = event.source.getSheetByName('Current'); //Name of the sheet where you want to run this script.
var actRng = event.source.getActiveRange();
var actSheet = event.source.getActiveSheet();
var actSheetName = actSheet.getSheetName();
var updateSheetName = sheet.getSheetName();
var editColumn = actRng.getColumn();
var index = actRng.getRowIndex();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var dateCol = headers[0].indexOf(timeStampColName);
var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
if (dateCol > -1 && index > 1 && editColumn == updateCol && actSheetName == updateSheetName) { // only timestamp if ‘Last Updated’ header exists, but not in the header row itself!
var cell = sheet.getRange(index, dateCol + 1);
var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
cell.setValue(date);
}
}
}
The problem is when the card is tagged, the cursor stays in the cell, so I need to hit enter for it to move down. Can I edit the script above for cursor to move one cell down automatically, as soon as the ID card is read by the reader? Please help, I've tried a separate jump down script and edit this one, but nothing worked so far. Thanks!
Example Logbook
If you mean mouse cursor, then I don't think you can do it via GAS. But if I understand you correctly, you want to activate the next cell in Column A after value was set. To do that, add method sheet.getRange(index+1,1).activate(); after you set value.

clearcontent certain cells /columns on row contains certain tex

i found this great formula that works so great on my need, sorry i forgot it original source
The Original Code is move the entire Row if it found "X" on Columns 21 and copy the entire row to sheet with same name as col 22 and delete the Entire row
1st Question,
instead the delete entire row, i need it only to clear certains Columns, for example it only clear content Columns 3, 4, 10 and 12 only << Solved thanks to idfurw
2nd Question,
how to lock this script to only a certain sheet?
here the new script base on update from idfurw
Main Script
function onEdit(e) {
// see Sheet event objects docs
// https://developers.google.com/apps-script/guides/triggers/events#google_sheets_events
var ss = e.source;
var s = e.range.getSheet();
if (s.getName() == 'Form');
var r = e.range;
// to let you modify where the action and move columns are in the form responses sheet
var actionCol = 21;
var nameCol = 22;
// Get the row and column of the active cell.
var rowIndex = r.getRowIndex();
var colIndex = r.getColumnIndex();
// Get the number of columns in the active sheet.
// -1 to drop our action/status column
var colNumber = s.getLastColumn()-1;
// if our action/status col is changed to ok do stuff
if (e.value == "XX" && colIndex == actionCol) {
// get our target sheet name - in this example we are using the priority column
var targetSheet = s.getRange(rowIndex, nameCol).getValue();
// if the sheet exists do more stuff
if (ss.getSheetByName(targetSheet)) {
// set our target sheet and target range
var targetSheet = ss.getSheetByName(targetSheet);
var targetRange = targetSheet.getRange(targetSheet.getLastRow()+1, 1, 1, colNumber);
// get our source range/row
var sourceRange = s.getRange(rowIndex, 1, 1, colNumber);
// new sheets says: 'Cannot cut from form data. Use copy instead.'
sourceRange.copyTo(targetRange);
// ..but we can still delete the row after
const cols = [6, 7, 8, 12,14,16,17,19,20,21];
for (const col of cols) {
s.getRange(rowIndex, col).clearContent();
}
// or you might want to keep but note move e.g. r.setValue("moved");
}
}
}
Time Stamp Script
function onEdit(event)
{
var timezone = "GMT+7";
var time_format = "dd-MM-yyyy"; // Timestamp Format.
var updateColName = "Pasien";
var timeStampColName = "Tgl Keluar";
var sheet = event.source.getSheetByName('Form'); //Name of the sheet where you want to run this script.
var actRng = event.source.getActiveRange();
var editColumn = actRng.getColumn();
var index = actRng.getRowIndex();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var dateCol = headers[0].indexOf(timeStampColName);
var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
if (dateCol > -1 && index > 1 && editColumn == updateCol)
{ // only timestamp if 'Last Updated' header exists, but not in the header row itself!
if (sheet.getRange(index, updateCol).isBlank()) {
sheet.getRange(index, dateCol + 1).clearContent();
}
else
var cell = sheet.getRange(index, dateCol + 1);
var date = Utilities.formatDate(new Date(), timezone, time_format);
cell.setValue(date);
}
}
Remove this line to disable deleting row:
s.deleteRow(rowIndex);
Replace with the following to clear content of specified cols:
const cols = [3, 4, 10, 12];
for (const col of cols) {
s.getRange(rowIndex, col).clearContent();
}
To limit the function to a particular sheet:
var s = e.range.getSheet();
/* add it after the above line */
if (s.getName() !== 'Name of sheet') { return; }

How two combine these two onEdit Script

Need help on combining these two script. Im totally newbie in this thing. They are
Main.gs
whenput 'XX' on U Column (21st col) then it copy the whole row to a sheet with same name as V Column (22nd col). Then it delete certain columns (for example 6, 7, 8, 12,14,16,17,19,20,21)
function onEdit(e) {
var ss = e.source;
var s = e.range.getSheet();;
var r = e.range;
// to let you modify where the action and move columns are in the form responses sheet
var actionCol = 21;
var nameCol = 22;
// Get the row and column of the active cell.
var rowIndex = r.getRowIndex();
var colIndex = r.getColumnIndex();
// Get the number of columns in the active sheet.
// -1 to drop our action/status column
var colNumber = s.getLastColumn()-1;
// if our action/status col is changed to ok do stuff
if (e.value == "XX" && colIndex == actionCol) {
// get our target sheet name - in this example we are using the priority column
var targetSheet = s.getRange(rowIndex, nameCol).getValue();
// if the sheet exists do more stuff
if (ss.getSheetByName(targetSheet)) {
// set our target sheet and target range
var targetSheet = ss.getSheetByName(targetSheet);
var targetRange = targetSheet.getRange(targetSheet.getLastRow()+1, 1, 1, colNumber);
// get our source range/row
var sourceRange = s.getRange(rowIndex, 1, 1, colNumber);
// new sheets says: 'Cannot cut from form data. Use copy instead.'
sourceRange.copyTo(targetRange);
// ..but we can still delete the row after
const cols = [6, 7, 8, 12,14,16,17,19,20,21];
for (const col of cols) {
s.getRange(rowIndex, col).clearContent();
}
// or you might want to keep but note move e.g. r.setValue("moved");
}
}
}
and
TimeStamp.gs
when type a word at "Pasien" Col, at the same row it will add date to "Tgl Keluar" col and when delete that word, it also clear content
function onEdit(event)
{
var timezone = "GMT+7";
var time_format = "dd-MM-yyyy"; // Timestamp Format.
var updateColName = "Pasien";
var timeStampColName = "Tgl Keluar";
var sheet = event.source.getSheetByName('Form'); //Name of the sheet where you want to run this script.
var actRng = event.source.getActiveRange();
var editColumn = actRng.getColumn();
var index = actRng.getRowIndex();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var dateCol = headers[0].indexOf(timeStampColName);
var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
if (dateCol > -1 && index > 1 && editColumn == updateCol)
{ // only timestamp if 'Last Updated' header exists, but not in the header row itself!
if (sheet.getRange(index, updateCol).isBlank()) {
sheet.getRange(index, dateCol + 1).clearContent();
}
else
var cell = sheet.getRange(index, dateCol + 1);
var date = Utilities.formatDate(new Date(), timezone, time_format);
cell.setValue(date);
}
}
This my combined onEdit
but i still dont understand about nested
function onEdit(event) {
first(event);
second(event);
}
function first(event)
{
var ss = e.source;
var s = e.range.getSheet();;
if (s.getName() !== 'Form')
var r = e.range;
// to let you modify where the action and move columns are in the form responses sheet
var actionCol = 21;
var nameCol = 22;
// Get the row and column of the active cell.
var rowIndex = r.getRowIndex();
var colIndex = r.getColumnIndex();
// Get the number of columns in the active sheet.
// -1 to drop our action/status column
var colNumber = s.getLastColumn()-1;
// if our action/status col is changed to ok do stuff
if (e.value == "XX" && colIndex == actionCol) {
// get our target sheet name - in this example we are using the priority column
var targetSheet = s.getRange(rowIndex, nameCol).getValue();
// if the sheet exists do more stuff
if (ss.getSheetByName(targetSheet)) {
// set our target sheet and target range
var targetSheet = ss.getSheetByName(targetSheet);
var targetRange = targetSheet.getRange(targetSheet.getLastRow()+1, 1, 1, colNumber);
// get our source range/row
var sourceRange = s.getRange(rowIndex, 1, 1, colNumber);
// new sheets says: 'Cannot cut from form data. Use copy instead.'
sourceRange.copyTo(targetRange);
// ..but we can still delete the row after
const cols = [6, 7, 8, 12,14,16,17,19,20,21];
for (const col of cols) {
s.getRange(rowIndex, col).clearContent();
}
// or you might want to keep but note move e.g. r.setValue("moved");
}
}
}
function second(event)
{
var timezone = "GMT+7";
var time_format = "dd-MM-yyyy"; // Timestamp Format.
var updateColName = "Pasien";
var timeStampColName = "Tgl Keluar";
var sheet = event.source.getSheetByName('Form'); //Name of the sheet where you want to run this script.
var actRng = event.source.getActiveRange();
var editColumn = actRng.getColumn();
var index = actRng.getRowIndex();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var dateCol = headers[0].indexOf(timeStampColName);
var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
if (dateCol > -1 && index > 1 && editColumn == updateCol)
{ // only timestamp if 'Last Updated' header exists, but not in the header row itself!
if (sheet.getRange(index, updateCol).isBlank()) {
sheet.getRange(index, dateCol + 1).clearContent();
}
else
var cell = sheet.getRange(index, dateCol + 1);
var date = Utilities.formatDate(new Date(), timezone, time_format);
cell.setValue(date);
}
}
The third code would be the simplest solution in case it complete within the time limit.
Otherwise, you need to optimize the code by:
replacing call of SpreadsheetApp by event object
removing duplicates among the two child functions
using if, boolean (flag), return to minimize executions
You need to know you code well in other to do either of these.
I dare not rewrite the code entirely. Just in order to make it a bit faster I'd propose to change this lines (in the third example):
const cols = [6, 7, 8, 12,14,16,17,19,20,21];
for (const col of cols) {
s.getRange(rowIndex, col).clearContent();
}
to this:
// cols to clean
const cols = [6, 7, 8, 12, 14, 16, 17, 19, 20, 21];
// get an array from the row
var row_to_clean = s.getRange(rowIndex, 6, rowIndex, 21).getValues();
// clean the array
for (let c of cols) row_to_clean[0][c-6] = ''; // col 6 is array[0][0]
// set values of the array back on the row
s.getRange(rowIndex, 6, rowIndex, 21).setValues(row_to_clean);
It will reduce calls to the server from 10 clearContents() to 2 getValues() + setValues() for every edited row.

Timestamp script copying onto same cell of different sheet

I have a strange script issue I was hoping someone could help me with -- I have 2 different sheets with timestamps that update when the neighboring cell is initialed. The scripts for both sheets are exactly the same (with different sheet names of course) and are not intended to interact, but one of them keeps copying over to the other.
Whenever someone initials/timestamps sheet 1, the script runs successfully on sheet 1 but also updates the same cell on sheet 2. If the cell doesn't exist, extra rows get added to create it. This copying doesn't happen the other way around.
Is there anything I can do to stop this?
Code for sheet 1:
function onEdit(event)
{
var timezone = "PST";
var timestamp_format = "MM-dd-yyyy hh:mm:ss"; // Timestamp Format.
var updateColName = "Initials";
var timeStampColName = "Last Updated";
var sheet = event.source.getSheetByName('WC Labs'); //Name of the sheet where you want to run this script.
var actRng = event.source.getActiveRange();
var editColumn = actRng.getColumn();
var index = actRng.getRowIndex();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var dateCol = headers[0].indexOf(timeStampColName);
var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
if (dateCol > -1 && index > 1 && editColumn == updateCol) { // only timestamp if 'Last Updated' header exists, but not in the header row itself!
var cell = sheet.getRange(index, dateCol + 1);
var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
cell.setValue(date);
}
}
Code for sheet 2:
function onEdit(event)
{
var timezone = "EST";
var timestamp_format = "MM-dd-yyyy hh:mm:ss"; // Timestamp Format.
var updateColName = "Initials";
var timeStampColName = "Last Updated";
var sheet = event.source.getSheetByName('EC Labs'); //Name of the sheet where you want to run this script.
var actRng = event.source.getActiveRange();
var editColumn = actRng.getColumn();
var index = actRng.getRowIndex();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var dateCol = headers[0].indexOf(timeStampColName);
var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
if (dateCol > -1 && index > 1 && editColumn == updateCol) { // only timestamp if 'Last Updated' header exists, but not in the header row itself!
var cell = sheet.getRange(index, dateCol + 1);
var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
cell.setValue(date);
}
}
Keep things DRY: if there are two functions that do the same thing, you only need one.
getSheet method allows you to get the sheet that contains the currently edited Range - use it to acquire the reference to the currently edited sheet.
Use strict equality comparison (it will not change anything in your case, but builds a useful habit that will save you debugging time).
Exit as early as possible: if statements should handle edge cases, not main logic.
Use the newer V8 runtime (the snippet below uses ES6 syntax).
/**
* #param {{
* range : GoogleAppsScript.Spreadsheet.Range,
* timezone : (string|"PST")
* }} param0
*/
function onEdit({ range, timezone = "PST" }) {
const sheet = range.getSheet();
const timestamp_format = "MM-dd-yyyy hh:mm:ss";
const updateColName = "Initials";
const timeStampColName = "Last Updated";
const editColumn = range.getColumn();
const editIndex = range.getRowIndex();
const [header] = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
const dateCol = header.indexOf(timeStampColName);
const updateCol = header.indexOf(updateColName) + 1;
if (dateCol < 0 && editIndex <= 1 && editColumn !== updateCol) { return; }
const cell = sheet.getRange(editIndex, dateCol + 1);
const date = Utilities.formatDate(new Date(), timezone, timestamp_format);
cell.setValue(date);
}
References
onEdit event object structure
The onEdit() function in both files is getting event.source.getActiveRange(); as the active range. Since you don't specify a particular sheet, whichever cell you are editing becomes active regardless of which sheet/tab it belongs.
Try to replace this line:
var actRng = event.source.getActiveRange();
in both files with the following:
var actRng = event.source.getSheetByName('WC Labs').getActiveRange();
and
var actRng = event.source.getSheetByName('EC Labs').getActiveRange();
respectively.
Code for sheet 1:
function onEdit(event)
{
var timezone = "PST";
var timestamp_format = "MM-dd-yyyy hh:mm:ss"; // Timestamp Format.
var updateColName = "Initials";
var timeStampColName = "Last Updated";
var sheet = event.source.getSheetByName('WC Labs'); //Name of the sheet where you want to run this script.
var actRng = event.source.getSheetByName('WC Labs').getActiveRange();
var editColumn = actRng.getColumn();
var index = actRng.getRowIndex();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var dateCol = headers[0].indexOf(timeStampColName);
var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
if (dateCol > -1 && index > 1 && editColumn == updateCol) { // only timestamp if 'Last Updated' header exists, but not in the header row itself!
var cell = sheet.getRange(index, dateCol + 1);
var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
cell.setValue(date);
}
}
Code for sheet 2:
function onEdit(event)
{
var timezone = "EST";
var timestamp_format = "MM-dd-yyyy hh:mm:ss"; // Timestamp Format.
var updateColName = "Initials";
var timeStampColName = "Last Updated";
var sheet = event.source.getSheetByName('EC Labs'); //Name of the sheet where you want to run this script.
var actRng = event.source.getSheetByName('EC Labs').getActiveRange();
var editColumn = actRng.getColumn();
var index = actRng.getRowIndex();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var dateCol = headers[0].indexOf(timeStampColName);
var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
if (dateCol > -1 && index > 1 && editColumn == updateCol) { // only timestamp if 'Last Updated' header exists, but not in the header row itself!
var cell = sheet.getRange(index, dateCol + 1);
var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
cell.setValue(date);
}
}

Google Sheet Auto time stamp

I'm trying to auto-populate a cells time ( cell B2 in sheet "leads")
But doesn't seem to run the script successfully. Gives me the error "TypeError: Cannot read property "source" from undefined. (line 6, file "Code"
function onEdit(event) {
var timezone = "GMT-2";
var timestamp_format = "MM-dd-yyyy"; // Timestamp Format.
var updateColName = "Time";
var sheet = event.source.getSheetByName('Leads'); //Name of the sheet where you want to run this script.
var responseArray = ["Time"];
var questionArray = ["Time"];
var actRng = event.source.getActiveRange();
var editColumn = actRng.getColumn();
var index = actRng.getRow();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues(); //get values and place them inside array
var dateCol = headers[0].indexOf(timeStampColName); //get index position inside the array
for (var i = 0; i < questionArray.length; i++) {
if (headers[0].indexOf(responseArray[1]) > -1 && index > 1 && editColumn == (headers[0].indexOf(questionArray[i]) + 1)) { // only timestamp if 'Last Updated' header exists, but not in the header row itself!
var cell = sheet.getRange(index, (headers[0].indexOf(responseArray[i]) + 1));
var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
cell.setValue(date);
}
}
}
I've tried all solutions but nothing seems to give me a successful run.
I want cell B2 to automatically add the time when someone starts to enter data into cell A1
That code is very over complicated for what you're trying to achieve to be honest.
Try using this:
function onEdit(e) {
var TIME_ZONE = "GMT-2"; //enter desired timezone
var date = Utilities.formatDate(new Date(), TIME_ZONE, 'MM-dd-yyyy'); //enter desired date format
//if column A has been edited, set date in same row in column B
if (e.range.getColumn() == 1) {
var row = e.range.getRow();
e.source.getSheetByName('Leads').getRange(row, 2).setValue(date);
}
}
Put simply, this code will check that the user has edited column A, if so then it'll put a date value into the same row in column B.