S
S
SergGavr2020-07-07 21:03:51
Google Apps Script
SergGavr, 2020-07-07 21:03:51

How to finalize the script for the correct filling of the sheet in Google App Script?

It is necessary to transfer the data from one sheet to another pre-sorted. Feed the following script:

function format() {
  var sourseList = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Исходные данные");
  var bdList = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("БД");
  var lr1 = sourseList.getLastRow();
  var lr2 = bdList.getLastRow();
  for(var i = 4;i<=lr1;i++){
    var colB = sourseList.getRange(i, 3).getValue();
    var num = sourseList.getRange(i, 2).getValue();
    var balance = sourseList.getRange(i, 6).getValue();
    if(colB === "0шт."){
      continue;
    }
    bdList.getRange(i, 1).setValue(num);
    bdList.getRange(i, 2).setValue(balance);
  }
}


Table:
5f04b85f84ad4948674339.png
The loop goes through three columns in the "Input data" sheet, then selects them according to the condition in column 3 and overwrites them in the "DB" sheet. But the data is written to the "DB" sheet with gaps, and I cannot figure out how to implement their correct sequential filling (one more cycle) into the "DB" sheet, the data is filled with empty fields as the cycle bypasses them. Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Stoyanov, 2020-09-09
@stomaks

Something like this:
(the code was not tested, there may be typos, but the logic is like this)

function format() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();

  const sourseList = ss.getSheetByName("Исходные данные");
  const bdList = ss.getSheetByName("БД");

  const values = sourseList.getDataRange()
  .getValues()
  .filter(item => item[4] === "0шт.")
  .map(item => [item[3], item[7]]);

  bdList.getRange(bdList.getLastRow() + 1, 1, values.length, 2).setValues(values);
}

---
Maxim Stoyanov (stomaks), developer of Google Apps Script .
g-apps-script.com
stomaks.me

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question