V
V
Vladimir Zuev2020-06-24 11:49:44
Google Apps Script
Vladimir Zuev, 2020-06-24 11:49:44

Where do the repetitions in the table come from?

Hello.
I use this code

async function CitiesNovaPoshta2 () {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Города");
  let x = 0;
  var payload = {
    'apiKey': '********************************************',
    'modelName': 'Address',
    'calledMethod': 'getCities',
  }
  var options = {
    'method' : 'post',
    'headers': {
      'content-type': 'application/json',
    },
    'payload': JSON.stringify(payload),
  };
  while (x < 10) { 
    var result = await UrlFetchApp.fetch('https://api.novaposhta.ua/v2.0/json/', options);
    if (result) {break;}        
    x++
  }; 
  var dataCities =  JSON.parse(result.getContentText());
  //var orderList1 = JSON.stringify(dataCities, null, 2);
  //Logger.log(orderList1);
  const  lengthCities =  dataCities["data"].length;
  const array = [];
  //for (let i = 0; i < lengthCities; i=i+10) {
   for (let i = 0; i < 200; i=i+10) { 
    array[i] = []; // создаем подмассив
    for (let j= i; j<i+10; j++){
      let buffer = [];
      
      buffer.push(dataCities["data"][j]["Description"]);
      buffer.push(dataCities["data"][j]["DescriptionRu"]);
      buffer.push(dataCities["data"][j]["Ref"]);
      buffer.push(dataCities["data"][j]["Delivery1"]);
      buffer.push(dataCities["data"][j]["Delivery2"]);
      buffer.push(dataCities["data"][j]["Delivery3"]);
      buffer.push(dataCities["data"][j]["Delivery4"]);
      buffer.push(dataCities["data"][j]["Delivery5"]);
      buffer.push(dataCities["data"][j]["Delivery6"]);
      buffer.push(dataCities["data"][j]["Delivery7"]);
      buffer.push(dataCities["data"][j]["Area"]);
      buffer.push(dataCities["data"][j]["SettlementType"]);
      buffer.push(dataCities["data"][j]["IsBranch"]);
      buffer.push(dataCities["data"][j]["CityID"]);
      buffer.push(dataCities["data"][j]["SettlementTypeDescription"]);
      buffer.push(dataCities["data"][j]["SettlementTypeDescriptionRu"]);
      buffer.push(dataCities["data"][j]["SpecialCashCheck"]);
      buffer.push(dataCities["data"][j]["Postomat"]);
      buffer.push(dataCities["data"][j]["AreaDescription"]);
      buffer.push(dataCities["data"][j]["AreaDescriptionRu"]);
      buffer = buffer.map(x=>[x])
       array.push(buffer);
    }
   Logger.log(array);
   await sheet.getRange(sheet.getLastRow() + 1,1,array.length, array[0].length).setValues(array);
   // SpreadsheetApp.flush()
  }
}

There are a lot of duplicate cities in the Google spreadsheet. Although there are no repetitions in the original array. I suppose that it is necessary to number the creating arrays somehow and put them in the table in order. Can anyone help me how to do this correctly so that there are no repetitions?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question