Answer the question
In order to leave comments, you need to log in
How to compare two ranges and select the right data?
Hello, I can't figure out where I made a mistake. The challenge is to take data from one page and compare it with data from another page. If there are no matches, then write data from one page to another. I wrote a small script, but when executed, it writes the last element of the array, and should, in theory, go from the first occurrence to the next. I am attaching a more accurate specification and code. Explain to the fool what I'm doing wrong?
function getList(){
var list = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("список сайтов").getDataRange().getValues();
return list;
}
function getData(){
var data = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("данные").getDataRange().getValues();
return data;
}
function main(){
var listArray = getList();
var dataArray = getData();
var dataSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("данные");
var listSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("список сайтов");
for (var i=1;i<dataArray.length;i++) {
Logger.log("Цикл 1: " + "итерация " + i);
for (var j=1;j<listArray.length;j++) {
if (dataArray[i][0] === listArray[j][0]) {
if (listArray[j][3] === '')
listSheet.getRange("B1:D13").setValue(dataArray[i][1]);
Logger.log("Цикл2: " + "записано " + dataArray[i][1]);
}
if (listArray[j][5] === ''){
listSheet.getRange("F13").setValue(dataArray[i][2]);
}
}
break;
}
}
Answer the question
In order to leave comments, you need to log in
You need to build an index like databases, otherwise things will be very complicated in cycles.
function main() {
var dataSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(
'данные'
);
var dataArray = dataSheet.getDataRange().getValues();
var listSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(
'список сайтов'
);
var listArray = listSheet.getDataRange().getValues();
// Строим индекс
var listIndex = listArray.map(function(r){
return r[0];
});
for (var j = 1; j < dataArray.length; j++) {
if(dataArray[j][0] === '') continue;
var pos = listIndex.indexOf(dataArray[j][0]);
if (pos >= 0) {
// Обновляем
listSheet.getRange(pos + 1, dataArray[j].length + 1).setValue(new Date());
} else {
// Добавляем
listSheet.appendRow([].concat(dataArray[j], new Date()));
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question