Tips for running two edit functions simultaneously within a single Google Sheets script

My goal is to have Google Sheets automatically add a timestamp beside the edited SKU, and it's working perfectly on the "Sales" tab.

The issue arises when trying to implement this on the "In Log" tab. I've searched for other solutions but haven't been able to figure it out. If someone could help me understand the problem, I would greatly appreciate it!

Here is the script:

function onEdit1(e) {
  var s = SpreadsheetApp.getActiveSheet();
  if (s.getName() == "In Log") { 
    var r = s.getActiveCell();
    if (r.getColumn() == 1) { 
      var nextCell = r.offset(0, 4);
      if (nextCell.getValue() === '') 
        nextCell.setValue(new Date());
    }
  }
}

function onEdit2(e) {
  var s = SpreadsheetApp.getActiveSheet();
  if (s.getName() == "Sales") { 
    var r = s.getActiveCell();
    if (r.getColumn() == 1) { 
      var nextCell = r.offset(0, 5);
      if (nextCell.getValue() === '') 
        nextCell.setValue(new Date());
    }
  }
}

I apologize for the messy code due to copying and pasting.

Here is a link to a copy of the Google Sheet: https://docs.google.com/spreadsheets/d/1nGPIKCjE6c_xJe7bDwhGIoFz5fJ-cpbTtavsUelxSW0/edit?usp=sharing

Answer №1

Combine the onEdit functions into one with conditional statements

function onEdit(e){
  
    var sheet = SpreadsheetApp.getActiveSheet();
    if( sheet.getName() == "In Log" ) { //checks if we're on the correct sheet
    var cell = sheet.getActiveCell();
    if( cell.getColumn() == 1 ) { //checks if the edited cell is in column A
    var adjacentCell = cell.offset(0, 4);
    if( adjacentCell.getValue() === '' ) //checks if the adjacent cell is empty
    adjacentCell.setValue(new Date());
  }
  }
    else if( sheet.getName() == "Sales" ) { 
    var cell = sheet.getActiveCell();
    if( cell.getColumn() == 1 ) {
    var adjacentCell = cell.offset(0, 5);
    if( adjacentCell.getValue() === '' )
    adjacentCell.setValue(new Date());
  }
  }
  }
  }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Filtering Data with React and Highcharts

My goal is to implement the functionality of Highstock rangeSelector for all highcharts modules, such as word cloud and pie charts. When I select a time range like 1m in Highstock, I want it to filter the data in other charts accordingly. Since I couldn&a ...

Is it possible to retrieve the key value of the array element that triggered a function call?

I'm looking to streamline my webpage's code by reading an XML file that contains a variable number of objects. In my JavaScript code, I create an array to store each object and loop through the XML data to generate them. As I iterate through the ...

Populate List Items until Maximum Capacity is Reached and Increase the

Is there a way to make a ListItem fill the list vertically while also being able to overflow it if needed? I'm looking for a solution that would allow me to access the height of the empty space in the list, which I believe would be the minHeight. Som ...

Kendo UI Scheduler: The system encountered an overflow while converting to a date and time format

Working on a project using .NET MVC and the Kendo UI Scheduler, an open-source tool. The goal is to save, read, update, and delete events from the scheduler into the database using JavaScript. Encountering some challenges in the process - when attempting ...

Leveraging the power of HTML 5 to dynamically insert content into a jQuery table

I am experiencing an issue with my jquery stock ticker. It displays the latest stocks including Company Name, stock price, and percentage changed. The stock price can be in GBP, USD, or YEN, but the current ticker does not indicate the currency. I attemp ...

What are some ways to improve the efficiency of my login system?

I have attempted two different methods. (1) User inputs information -> Ajax call to verify credentials -> Server sends a header, which is captured on the client side and written using document.write(). (2) User enters information -> Ajax call to ...

The variable "randomString" has not been declared within the HTMLInputElement.onclick function in Types

I need a function that can generate a random string or number for me. Initially, my function in TypeScript looked like this: randomString() { let chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz"; let string_length = 8; ...

Choose from various choices, organize them into an array, and extract information from the database seamlessly without refreshing the page

I am facing an issue with retrieving multiple select data from the database without refreshing the page. I need to be able to select more than one option and retrieve data based on my selections. Below is the code for my select tag: <select name="c ...

I require JSON formatting containing the necessary values for integration with the Flot Chart plugin

I need assistance with retrieving data from a mysql database to populate a chart using the Flot plugin. According to the documentation, the data format should be an array of dots: [ [x1, y1], [x2, y2], ... ]. In my scenario, I am looking to extract date an ...

Photo captured by camera is not stored in photo gallery

I am currently working on a basic image upload form that allows users to take photos using their phone camera and upload them. However, I have noticed that the pictures taken this way are not being saved to the gallery. Is there something missing in the H ...

Guide on how to activate a hyperlink with a specified target using JavaScript

I have a URL structured like this: <a id="preview" ng-href="/preview/{{accountId}}/app/{{app.id}}" target="preview" class="btn btn-default" style="margin-left: 20px;" ng-hide="isJobMode">Preview</a> This is part of an Angular app, and I am tr ...

Building an integrated Monaco editor using Electron and AngularJS

Struggling to integrate the Monaco editor into my Electron app. Despite following electron examples, encountering persistent errors in my application. The errors I'm facing include: "loader.js:1817 Uncaught Error: Unrecognized require call" "angula ...

Having trouble retrieving PHP variable values using JavaScript?

<div id="div01">01</div> <div id="div02">02</div> <img src="../img/logo.png" onclick="blueSky()"/> js function blueSky() { $.ajax({ type:'GET', url: 'test.php', success: function(respond) { document.ge ...

Passing component properties using spaces in VueJS is a simple and effective

I am encountering an issue where I want to pass component props from my view, but I am facing a challenge due to the presence of a space in the value. This causes Vue to return the following error: vendor.js:695 [Vue warn]: Error compiling template: - inva ...

`Uniform background color across all pages`

My goal is to allow customers to select a color that will persist across different pages on the website. The code below shows how users can choose a color on the first page: <select id="color" style="width: 5%; height: 10%" size="5"> ...

AngularJSError

I am completely new to AngularJS and I've been tasked with debugging someone else's code. While debugging in Google Chrome, I encountered the following error: TypeError: accountService.logIn(...).success is not a function. The issue lies with ...

Showing Data from JSON on an HTML Page

My project in play framework requires displaying Strings from a Json (containing a list of Strings) in a table on an html page. I attempted to use ajax and jquery for this purpose, but the implementation is not functioning correctly. Here is my javascript ...

Is there an rxjs operator that includes both on-next and on-error callbacks?

Is there a similar function to promise.then(onNextCallback,onErrorCallback) in rxjs that I can use? I've already tried alternatives like pipe(concatMap(),catchError) but they are not what I am looking for. ...

What is the best way to prevent event propagation in d3 with TypeScript?

When working with JavaScript, I often use the following code to prevent event propagation when dragging something. var drag = d3.behavior.drag() .origin(function(d) { return d; }) .on('dragstart', function(e) { d3.event.sourceEvent ...

superagent is not providing any response within the specified time frame

My attempt to send a request with a query string is not producing any log in the console. I'm expecting some result back but both res and err turn out to be empty. superagent .get('http://localhost:4000/v1/process/web') .que ...