P
P
pteslenko72019-12-05 20:28:07
Google Apps Script
pteslenko7, 2019-12-05 20:28:07

How to validate on a specific column?

I have a google spreadsheet. It has 2 sheets. The data in the first sheet changes regularly. There is a list of sites (this is immutable data), then a person can be attached to the site, and there is a start date and a completion date (this is mutable data). The site can be worked out by different people on different dates. Therefore, there is a need to keep a kind of log book, when and by whom a certain area was taken. The second sheet should serve as a journal entry.
It is necessary to make sure that when a person is added for a certain area + the start and completion date of work is added, then this data (together with the number of the area) is transferred to the second sheet. And inserted into the last empty cell. And so on ad infinitum. Just a journal entry.
With this script, I got access to sheet 1 and 2, got the data from the active cells of sheet 1. And transferred them to sheet 2.

function addToDatabase(){
  var list1 = SpreadsheetApp.getActiveSpreadsheet(); //лист откуда берем данные
  var activCell = list1.getActiveSheet().getActiveCell(); //определение ячейки на которой сейчас находится курсор   

  var activNum = activCell.getRow(); //номер активной ячейки
  
  var number = list1.getActiveSheet().getRange(activNum, 1).getValue(); // номер участка
  var name = list1.getActiveSheet().getRange(activNum, 2).getValue(); // имя человека
  var date1 = list1.getActiveSheet().getRange(activNum, 4).getValue(); // дата начала работ
  var date2 = list1.getActiveSheet().getRange(activNum, 6).getValue(); // дата завершения работ
  
  var activColumn = list1.getActiveSheet().getActiveCell().getColumn(); //номер активного столбца
  
  var list2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Журнал вода данных"); //лист куда вставляем данные
  
  var lastEmptyRow = list2.getLastRow() + 1; //разбивка куда какие данные вставлять 
  list2.getRange(lastEmptyRow, 1).setValue(number); // вставить номер
  list2.getRange(lastEmptyRow, 2).setValue(name); // вставить имя
  var lastEmptyRow = list2.getLastRow() + 1; //разбивка куда какие данные вставлять
  list2.getRange(lastEmptyRow, 1).setValue(number); // вставить номер
  list2.getRange(lastEmptyRow, 2).setValue(date1); // вставить дату начала работ
  list2.getRange(lastEmptyRow, 3).setValue(date2); // вставить дату завершения работ
      
}

Next, I want to put the launch of this script on change. But so that it does not transfer data every time each cell is changed, you need to make sure that the data is transferred only if all fields for transfer are filled.
That is, if both the number and the name, the start date and the end date are filled in line 2, then the script transfers the data from this line to sheet 2. The same is
true for other sections. If line 3 contains both the number and name, start date and end date, then the script transfers the data from this line to sheet 2, etc.
If any of the cells (number, name, start date, end date) is not filled, then the data from this row is not transferred.
I was advised to check for a non-empty cell.
Can you please tell me how to write such a check?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ivanov, 2019-12-06
@pteslenko7

If without code explanations, then

function onEdit() {
   run2();
}

function run2() {
  /* Remove dash */
  var sheet = SpreadsheetApp.getActiveSheet();
  if (sheet.getName() === 'Журнал вода данных') return;
  var archive = SpreadsheetApp.getActive().getSheetByName('Журнал вода данных');

  var action = function(values, i, i2) {
    var data = values.slice(i, i + i2);
    archive
      .getRange(archive.getLastRow() + 1, 1, data.length, data[0].length)
      .setValues(data);
  };

  var condition = function(values, i) {
    var row = values[i];
    return (
      i > 0 && row[0] !== '' && row[1] !== '' && row[3] !== '' && row[5] !== ''
    );
  };

  deleteRowsByConditional_(sheet, condition, action);
}

function deleteRowsByConditional_(sheet, condition, action) {
  sheet
    .getDataRange()
    .getValues()
    .forEach(
      function(_, i, arr) {
        var j = arr.length - i - 1;
        if (this.condition.apply(null, [arr, j])) {
          this.isContinue++;
          if (j > 0) return;
        }
        if (this.isContinue > 0) {
          var prevPos = j + 1; // It's reversed
          if (action) action(arr, prevPos, this.isContinue);
          this.sheet.deleteRows(prevPos + 1, this.isContinue);
          this.isContinue = 0;
          return;
        }
        return;
      },
      { sheet: sheet, condition: condition, isContinue: 0 }
    );
}

Will work on any sheet of your spreadsheet. Can be connected run2()to the menu.
Link to video https://www.facebook.com/oshliaer/videos/258791787...
Link to snippet https://github.com/contributorpw/google-apps-script...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question