T
T
tatarrr952021-06-21 15:15:21
Google Apps Script
tatarrr95, 2021-06-21 15:15:21

How to quickly insert an array of objects into Google Sheets using Google Scripts?

I have an array with the following objects

statistic = [
{fio=Сватеев Сергей Николаевич, town=Пенза, finishedAt=1.624035046342E12, tagName=Получение времени, callJobId=902671334, callDuration=58.0, tagPayload={}, attemptsCount=1.0, createdAt=1.624034966959E12, phone=11111111111, redialNumber=null, reportData=null, jobStatus=longCallWithNoResult}, 
{attemptsCount=1.0, jobStatus=longCallWithNoResult, callDuration=16.0, reportData=null, tagName=Оффер, finishedAt=1.624035006952E12, createdAt=1.624034966959E12, callJobId=902671302, phone=2222222222, redialNumber=null, town=Пенза, tagPayload={}, fio=Мартынов Сергей Николаевич}
]

I insert the following code into the table:
for(var i = 0; i < statistic.length; i++){
    phone = (statistic[i].phone != null) ? statistic[i].phone : "";
    finishedAt = (statistic[i].finishedAt != null) ? new Date(statistic[i].finishedAt).toLocaleString("ru-RU", {timeZone: "Europe/Moscow"}) : "";
    attemptsCount = (statistic[i].attemptsCount != null) ? statistic[i].attemptsCount : "";
    tagName = (statistic[i].tagName != null) ? statistic[i].tagName : "";
    callJobId = (statistic[i].callJobId != null) ? statistic[i].callJobId : "";
    jobStatus = (statistic[i].jobStatus != null) ? statistic[i].jobStatus : "";
    callDuration = (statistic[i].callDuration != null) ? statistic[i].callDuration : "";
    town = (statistic[i].town != null) ? statistic[i].town : "";
    fio = (statistic[i].fio != null) ? statistic[i].fio : "";
    sheet.appendRow([phone, finishedAt, attemptsCount, tagName, jobStatus, callDuration, callJobId, town, fio]);
  }

But appendRow works slowly, especially on large volumes the table is filled slowly.
How to fill it up quickly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Grigory Boev, 2021-06-21
@tatarrr95

First, form the data (an array of arrays), and then display it at a time through Range.setValues(firstRow, firstColumn, countRows, countColumns)

statistic = statistic.map(x=>{	
return [x.phone || "",
  (x.finishedAt != null) ? new Date(x.finishedAt).toLocaleString("ru-RU", {timeZone: "Europe/Moscow"}) : "",
    x.attemptsCount || "",
  x.tagName || "",
  x.jobStatus || "",
  x.callDuration || "",
  x.callJobId || "",
  x.town || "",
  x.fio || ""
  ];
})

SpreadsheetApp
  .getActiveSpreadsheet()
  .getSheetByName("Лист1") // Имя листа вывода
  .getRange(1,1, statistic.length, statistic[0].length) // 1,1 - строка и столбец ячейки вывода
  .setValues(statistic);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question