L
L
lifeo3112020-02-28 15:19:45
Google Sheets
lifeo311, 2020-02-28 15:19:45

How to set up substitution of the current date when filling an adjacent cell into several columns?

There is a macro that enters the current date in column 6 when something is entered in column 5. How to make the same work separately for columns 5-6, 7-8, 10-11, 13-14, 16-17

5 - we enter some data, at 6 the date at the time of editing appears
7 - we enter some data, at 8 the date at the time of editing appears,

and so on

function onEdit(e) {
  var sheet = e.source.getActiveSheet();
  var idCol = e.range.getColumn();
  var idRow = e.range.getRow();
  if (idCol == 5 && sheet.getName() == 'Лист1') {
    var Value = e.range.offset(0, +2).getValues();
    if (Value == "") {
      var vartoday = getDate();
      sheet.getRange(idRow, 6).setValue(vartoday);
    }
  }
}

// Returns YYYYMMDD-formatted date.
function getDate() {
  var today = new Date();
  today.setDate(today.getDate());
  //return Utilities.formatDate(today, 'PST', 'dd.MM.yyyy');
  return Utilities.formatDate(today, 'GMT+03:00', 'dd.MM.yyyy');
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ivanov, 2020-02-28
@lifeo311

Unfortunately, I couldn't understand your code. Try applying this to your condition. Those. you need to apply your if ( Value == "" ) {to below solution

var COLUMNS = [5,7,10,13,16]; // Колонки, которые редактируются

function onEdit(e) {
  var sheet = e.range.getSheet();
  var idCol = e.range.getColumn();
  // Мы на нужном листе и на нужной колонке?
  if ( COLUMNS.indexOf(idCol) > -1 && sheet.getName() =='Лист1' ) {
    e.range.offset(0, 1).setValue( new Date() );
  }
}

Some parts of the code are completely incomprehensible, for example,
  • .offset(0, +2)???
  • getDate();- Why is this at all?
  • sheet.getRange(idRow, 6).setValue( vartoday );What offsetdid you not like here?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question