@tatarrr95

Как быстро вставить массив с объектами в Google Sheets через Google Scripts?

У меня есть массив с объектами следующего вида
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=Мартынов Сергей Николаевич}
]

Вставляю в таблицу следующим кодом:
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]);
  }

Но appendRow работает медленно, особенно на больших объемах таблица наполняется медленно.
Как ее наполнить быстро?
  • Вопрос задан
  • 493 просмотра
Решения вопроса 1
ProgrammerForever
@ProgrammerForever Куратор тега Google Sheets
Учитель, автоэлектрик, программист, музыкант
Сначала формировать данные (массив массивов), а потом уже выводить за раз через 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);
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы