D
D
delongeroman2020-08-25 21:31:02
Google Apps Script
delongeroman, 2020-08-25 21:31:02

Why are lines replaced and duplicated when sorting in Google Spreadsheets?

5f455816e62f8225416144.png
There is such a table, at the top is the order entry form, at the bottom are orders. Orders themselves are protected and orders are entered from the form by the "when editing" trigger. When there are a lot of orders, about 25-30, either replacement of overlapping orders during sorting, or duplication can happen, this happens extremely rarely, but I still need to fix this problem and I can’t understand what is the reason

function vnestizakaz() {
  var ss = SpreadsheetApp.getActiveSpreadsheet()
  var as = ss.getActiveSheet()
  if (as.getRange(3, 14).isChecked() === true){
  var vvod = as.getRange(2, 1, 1, 10).getValues()
  var itog = 0
  var raschet = as.getRange(2, 12).getValue()
  var lr = as.getLastRow()
  var lc = as.getLastColumn()
    var clear = as.getRange(2, 3, 1, 7).clear({formatOnly: false, contentsOnly: true}) 
    var clear2 = as.getRange(2, 12).clear({formatOnly: false, contentsOnly: true}) 
    if(raschet != ""){
   as.getRange(lr+1, 1, 1, 10).setValues(vvod)
   as.getRange(lr+1, 11).setFormulaR1C1("=R[0]C[-2] + R[0]C[-1]")
   as.getRange(lr+1, 12).setValue(raschet)
   as.getRange(lr+1, 13).setValue("0")
   as.getRange('A7:P').sort([{column: 2, ascending: true}]); 
      }
   as.getRange(3, 14).uncheck()
  }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
Grigory Boev, 2020-08-25
@ProgrammerForever

It is likely that one onEdit crawls onto another, look towards LockService to overcome parallel script launches

M
Maxim Stoyanov, 2020-08-26
@stomaks

Most likely, duplicates occur due to the simultaneous launch of the script.
In solidarity with Gregory , try LockService for collision prevention.
As an option, you can take sorting into a separate function and hang it on a minute trigger .
---
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