A
A
Andmakssc2020-09-08 14:28:58
Google Sheets
Andmakssc, 2020-09-08 14:28:58

How to write a script for multiple columns?

At me it turned out to register a script on one column. there he is:

function onEdit(e) {
var sheet = e.source.getActiveSheet();
var idCol = e.range.getColumn();
var idRow = e.range.getRow();
if ( idCol == 2 && sheet.getName() =='Лист1' ) {
var Value = e.range.offset(1, +5).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', 'yyyy.MM.dd');
return Utilities.formatDate(today, 'GMT+03:00', 'yyyy.MM.dd');
}

I need auto-completion of dates and a reserve in several different columns (I enter data on different days). That is, let's say, I enter in 1 column (cell) - the date appears in the 2nd column (cell). And I also enter in the 10th column (cell) - the date appears in the 13th column (cell). Etc. Tell me, is it possible to somehow duplicate this script in different columns in one table, simply replacing the value of the columns?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Grigory Boev, 2020-09-08
@Andmakssc

I didn’t quite understand what needs to be done on the issue, but try this code - there are options for columns (1.10, etc.) and options for their corresponding columns (2.13, etc.)

function onEdit(e) {
    var sheet = e.source.getActiveSheet();
    var idCol = e.range.getColumn();
    var idRow = e.range.getRow();
  const colsSource = [1,10];// Столбцы исходные
  const colsDestination = [2,13];// Столбцы назначения
    if (sheet.getName() == 'Лист1' && (colsSource.indexOf(idCol)!=-1)) { //Лист1 и idCol есть в colsSource
        var Value = e.range.offset(1, +5).getValues();
        if (Value == "") {
            var vartoday = getDate();
      const destinationCol = colsDestination[colsSource.indexOf(idCol)];
            sheet.getRange(idRow, destinationCol).setValue(vartoday);
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question