lr = sheet1.getLastRow();
lc = sheet1.getLastColumn();
sheet1.getRange(lr, 1, lr, lc).setBackground('#ffff00')
// Продажи на Prom ua
async function getResponse() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName('Продажи на Prom ua');
var spreadsheet = SpreadsheetApp.getActive();
var summaa =0;
var key;
var id;
let orderList;
let lngth;
let x = 0;
let v=0;
let responseCode=0;
let result ;
let storeSt = {};
let data = [];
let str= "";
let strprice = '';
sheet1.getRange("A1:F100").setHorizontalAlignment('center');
var headers = {
'Authorization': **********************************************,
'contentType': 'application/json; charset=utf-8'
};
var options = {
'method' : 'get',
'headers': headers
};
while (x < 10) {
result = await UrlFetchApp.fetch('https://my.prom.ua/api/v1/orders/list', options);
if (result) {break;}
x++
};
orderList = JSON.parse(result.getContentText());
var orderList1 = JSON.stringify(orderList, null, 2);
//Logger.log(orderList1);
if(orderList){
let value = sheet1.getRange('B2').getValue();
sheet1.getRange('R1').setValue(value);
lngth = orderList['orders'].length;
for (let i = 0; i<lngth; i++ ){
if(orderList['orders']){
id = orderList['orders'][i]['id'];
//Ищем такой же id в старых записях
lr = sheet1.getLastRow();
//Создаём массив из id со второго стобца
if(id){
if(lr>1){
let ran = sheet1.getRange(1,2,lr,1);
data = ran.getValues();
for (let t = lr; t>0; t--){
let key = data[t];
storeSt[key] = t+1;
}; };
if(storeSt[id]==null){
value = sheet1.getRange('B2').getValue()
let value1 = sheet1.getRange('R1').getValue()
Logger.log('value =',value);
Logger.log('value1 =',value1);
if (value == value1&&value>1){
var lr1 = sheet1.getLastRow();
let lc = sheet1.getLastColumn();
sheet1.getRange(2, 1, lr1, lc).activate();
spreadsheet.getActiveRangeList().setBackground('#fff2cc')
.setFontWeight('bold');
};
//Заполняем первый запрос для таблицы
var cell = sheet1.getRange(lr+1,2);
cell.setValue(id);
var stringg = orderList["orders"][i]["date_created"];
stringg = stringg.replace("T"," \nT ");
stringg = stringg.replace("."," \n. ");
cell = sheet1.getRange(lr+1,1);
cell.setValue(stringg);
var firstname = orderList["orders"][i]['client_first_name'];
var secondname = orderList["orders"][i]['client_second_name'];
var lastname = orderList["orders"][i]['client_last_name'];
if (firstname == null){firstname = ' '};
if (secondname == null){secondname = ' '};
if (lastname == null){lastname = ' '};
var name = firstname + "\n" + secondname + "\n"+ lastname;
cell = sheet1.getRange(lr+1,3);
cell.setValue(name);
cell = sheet1.getRange(lr+1,4);
cell.setValue(orderList["orders"][i]['phone']);
//****************************************************************************************************************************************************************
for ( key in orderList["orders"][i]['products']){
var nproducts = orderList["orders"][i]['products'].length;
};
cell = sheet1.getRange(lr+1,6);
cell.setValue(nproducts);
if(nproducts>1){
str = ' ';
for (var j = 0; j<nproducts; j++ ){
var prname = [];
prname[j] = orderList["orders"][i]['products'][j]['name'];
/* var ProductName=prname[j];
var result1 = check('Прейскурант товаров', 'A:A', ProductName) // Проверка товара на наличие в листе Прейскурант товаров
if (result1 && nproducts ==1){
cell = sheet1.getRange(lr+1,getRange(lr+1,17));
cell.setValue('Прейскурант');
}*/
str +=(j+1)+'.'+prname[j]+'\n';
}}
else{
str = orderList["orders"][i]['products'][0]['name']};
n = 30;
while(n < str.length) {
while(str[n] !== " " && n < str.length) {
n++
}
str = str[n] ? str.slice(0, n) + str[n].replace(" ", '\r\n') + str.slice(n+1) : str;n += (30 + 1);
}
cell = sheet1.getRange(lr+1,5);
cell.setValue(str);
//****************************************************************************************************************************************************************************
// Общая сумма за товар
var temp = orderList["orders"][i]['price'];
price = parseInt(temp.match(/^(.*?)\sгрн./));
temp = Math.round(price);
cell = sheet1.getRange(lr+1,7);
cell.setValue(temp);
var summa = orderList["orders"][i]['products'][0]['quantity'] * temp;
var lngth1 = orderList["orders"][i]['products'].length;
if (lngth1>1){
for ( v = 0; v<lngth1; v++ ){
var price = orderList["orders"][i]['products'][v]['price'];
price = parseInt(price.match(/^(.*?)\sгрн./));
price = Math.round(price);
if(v<lngth1-1){
strprice= strprice + price +'грн. '+ '+';
}
else {strprice= strprice + price+'грн. '}
}
var sum = summa+"грн."+" "+'='+ strprice
n = 4;
while(n < sum.length) {
while(sum[n] !== " " && n < sum.length) {n++}
sum = sum[n] ? sum.slice(0, n) + sum[n].replace(" ", '\r\n\n') + sum.slice(n+1) : sum;n += (4 + 1);
}
cell = sheet1.getRange(lr+1,8);
cell.setValue(sum);
strprice= "";
}
else {
cell = sheet1.getRange(lr+1,8)
cell.setValue(summa+"грн.")};
if(orderList["orders"][i]['payment_option']!=null){
var paymentt = orderList["orders"][i]['payment_option']['name'];
k = Math.round(paymentt.length/4);
n = k;
while(n < paymentt.length) {
while(paymentt[n] !== " " && n < paymentt.length) {n++}
paymentt = paymentt[n] ? paymentt.slice(0, n) + paymentt[n].replace(" ", '\r\n') + paymentt.slice(n+1) : paymentt;n += (k + 1);
}
cell = sheet1.getRange(lr+1,9);
cell.setValue(paymentt);//Вид оплаты
};
const address = orderList["orders"][i]['delivery_address'];
// Logger.log('address=',address);
Address (address);
var stats = '';
stats = orderList["orders"][i]['status'];
if (stats == 'received'){stats = 'Принят'};
if (stats == 'delivered'){stats = 'доставлен'};
if (stats == 'canceled'){stats = 'отменён'};
if (stats == 'paid'){stats = 'оплаченный'};
if (stats == 'pending'){stats = 'Новый'};
Logger.log('stats =', stats );
cell = sheet1.getRange(lr+1,14);
cell.setValue(stats);
lr = sheet1.getLastRow();
lc = sheet1.getLastColumn();
sheet1.getRange(lr, 1, lr, lc).setBackground('#ffff00')
spreadsheet.getRange('B:B').activate();
spreadsheet.getActiveSheet().sort(2, true)
.sort(2, false);
}; };} }; };
sheet1.getRange('R1').clear({contentsOnly: true});
var lr = sheet1.getLastRow();
var lc = sheet1.getLastColumn();
sheet1.getRange(lr+1, 1, lr+24, lc).setBackground(null)
.setBorder(false, false, false, false, false, false);
sheet1.getRange(lr, 1, lr, lc).setBorder(true, true, true, true, true, true, '#000000', SpreadsheetApp.BorderStyle.SOLID);
sheet1.getRange('A1').activate();
};
function StringColor() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName('Тест');
var spreadsheet = SpreadsheetApp.getActive();
for (var i = 2; i<10; i++ ){
cell = sheet1.getRange(i,5);
cell.setValue("тест");
var lr = sheet1.getLastRow();
var lc = sheet1.getLastColumn();
sheet1.getRange(lr, 1, lr, lc).setBackground('#ffff00');
}};