2017-12-24 1 views
0

매일 새로 고치는 API에서 상위 30 개 순위의 새 이름을 확인한 다음 새 이름을 다른 열에 추가합니다 (아직없는 경우).새 값이 열에 있는지 여부를 확인하는 방법?

for-loop는 해결책이라고 생각합니다. 이것이 내가 지금까지 얻은 것입니다.

function appendValues(){ 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var top30Names = ss.getRange("A4:A33").getValues(); 
    var eligibleNames = ss.getRange("P4:P300").getValues(); 
    for (i = 0; i < 30; i++){ 
    var searchKey = top30Names[i]; // search if the eligible name is in the top30names 
    if (isInArray(searchKey, eligibleNames)){ 
     // do nothing 
    } 
    else{ 
     getFirstEmptyRow(); 
     ss.getActiveCell().setValue(searchKey); 
    } 
    } 
} 

function isInArray(value, array) { 
    return array.indexOf(value) > -1; 
} 

function getFirstEmptyRow() { 
    var sheet = SpreadsheetApp.getActiveSheet(), 
     values = sheet.getRange("P4:P300") // the range to search for the first blank cell 
      .getValues(), 
     row = 0; //start with the first array element in the 2D array retrieved by getValues() 
    for (row; row < values.length; row++) { 
     if (!values[row].join("")) break; 
    } 
    return sheet.setActiveSelection("P" + (row + 4)).getRow();//.getLastRow() // column between "" and row + starting_row in range 

}

이 전체 30 대 때마다 추가,하지만 난 단지 새 값이 필요합니다.

답변

0

setFormula을 사용하여 해결 된 것을 발견했습니다. 누구든지 좀 더 우아한 해결책을 가지고 있다면 행복하게 배울 수 있습니다.

function appendNewName(){ 
    setFormula(); 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var newNames = ss.getRangeByName("newEntries").getValues(); 
    for (i = 0; i < 30; i++){ 
    getFirstEmptyRow(); 
    var x = newNames[i][0]; 
    // Logger.log(x); // What does this do??? 
    if (x.length > 1) { 
     ss.getActiveCell().setValue(newNames[i]); 
    } 
    } 
} 
function setFormula(){ 
    // first run this 
    clearFormula(); 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    ss.getRangeByName("newEntries").setFormula("=IF(ISNUMBER(MATCH(A4,AppendNew,0)),\"\",A4)"); // Sets a formula to the range that will show the new daily entries in top 30 
} 

function clearFormula(){ 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    ss.getRangeByName("newEntries").clear(); 
} 

function getFirstEmptyRow() { 
    var sheet = SpreadsheetApp.getActiveSheet(), 
     values = sheet.getRange("appendNew") // the range to search for the first blank cell 
      .getValues(), 
     row = 0; //start with the first array element in the 2D array retrieved by getValues() 
    for (row; row < values.length; row++) { 
     if (!values[row].join("")) break; 
    } 
    return sheet.setActiveSelection("P" + (row + 4)).getRow(); // column between "" and row + starting_row in range 
} 
관련 문제