简体   繁体   中英

How do I run two separate functions in google sheets

A table example of what I want to happen:

在此处输入图像描述

The idea is that in the first column, one could write down the name of the item when it arrives, which would automatically put the date it arrived in the second column. Then when that item is sold, that would be recorded in the third column, which would automatically add the sell date into the fourth column. However, only the third column is working while the first does not input a date anymore

Here is my code:

function DateColumnOne() {
  var s = SpreadsheetApp.getActiveSheet();
  if (s.getName() == "Sheet1") { //checks that we're on Sheet1 or not
    var r = s.getActiveCell();
    if (r.getColumn() == 1) { //checks that the cell being edited is in column A
      var nextCell = r.offset(0, 1);
      if (nextCell.getValue() === '') //checks if the adjacent cell is empty or not?
        nextCell.setValue(new Date());
    }
  }
}

function DateColumnTwo() {
  var s = SpreadsheetApp.getActiveSheet();
  if (s.getName() == "Sheet1") { //checks that we're on Sheet1 or not
    var r = s.getActiveCell();
    if (r.getColumn() == 3) { //checks that the cell being edited is in column C
      var nextCell = r.offset(0, 1);
      if (nextCell.getValue() === '') //checks if the adjacent cell is empty or not?
        nextCell.setValue(new Date());
    }
  }
}

If this question has already been answered, could you please tell me what keywords I should be searching for, as I have tried for the past two days to solve this on my own to no avail

I think you need something like this:

function onEdit(e) {
  var s = e.source.getActiveSheet();
  if (s.getName() != "Sheet1") return;
  DateColumnOne(s);
  DateColumnTwo(s);
}

function DateColumnOne(s) {
  var r = s.getActiveCell();
  if (r.getColumn() == 1) { //checks that the cell being edited is in column A
    var nextCell = r.offset(0, 1);
    if (nextCell.getValue() === '') //checks if the adjacent cell is empty or not?
      nextCell.setValue(new Date());
  }
}

function DateColumnTwo(s) {
  var r = s.getActiveCell();
  if (r.getColumn() == 3) { //checks that the cell being edited is in column C
    var nextCell = r.offset(0, 1);
    if (nextCell.getValue() === '') //checks if the adjacent cell is empty or not?
        nextCell.setValue(new Date());
  }
}

The first function onEdit() is a special inner function that fires every time you edit the Spreadsheet manually.

It gets object e (the name can be anything). The object e contains information about where the edit happens. You can check this information (name of sheet, etc) and to run another functions.

Actually you can do it much shorter. Just with one function:

function onEdit(e) {
  var s = e.source.getActiveSheet();    // get the sheet
  if (s.getName() != "Sheet1") return;  // check a name of the sheet

  var col = e.range.columnStart;    // get current column
  if (col != 1 && col != 3) return; // check if it's col 1 or 3

  var next_cell = s.getActiveCell().offset(0,1); // get next cell
  if (next_cell.getValue() != '') return;        // check is the cell is empty

  next_cell.setValue(new Date());  // fill the cell with current date
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM