V
V
Vladimir Zuev2020-06-24 07:53:35
Google Apps Script
Vladimir Zuev, 2020-06-24 07:53:35

How to insert an array into a table?

Hello.
I'm trying to insert an array into a table. Here is the code.

async function CitiesNovaPoshta2 () {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Города");
  let x = 0;
  var payload = {
    'apiKey': '6bde07fde09a9f1d11b034df69403d73',
    '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;
 
  //lr = sheet.getLastRow()-1;
  let array = [];
  for (let i = 0; i < lengthCities; i += 10) {
    
    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]["PreventEntryNewStreetsUser"]);
    buffer.push(dataCities["data"][j]["Conglomerates"])
    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"]);
     
      array.push(buffer);
         }
   
   sheet.getRange(sheet.getLastRow() + 1,1,array.length, array[0].length).setValues(array);
  
   // SpreadsheetApp.flush()
  }
}

Doesn't scold. But it doesn't embed. What am I doing wrong? How to correctly add an array to a table? Help who can.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Grigory Boev, 2020-06-24
@vladd56

You have an array of type
[el1, el2, el3...]
A, you need this:
[[el1], [el2], [el3]...]
You don’t have to redo the code, but before inserting it into the table, just do this:
buffer = buffer.map(x=>[x]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question