메뉴 건너뛰기

app

[GAS] 다른 시트의 내용 가져오기

lispro062014.07.24 01:30조회 수 1866댓글 0

    • 글자 크기


namedRange로 내용을 가져오는 방법을 사용했었는데, 이렇게 되면, 범위를 정의해야하는 번거로움이 있다.


  var range = ss.getSheets()[0].getRange("A1:F100"); 로 하면, 범위 정의 없이, 정해진 범위의 내용을 다 가져올 수 있다.


var ret_arr=selected_getOuterRangeValues(row,"sms");

  ral=ret_arr.length;


게 하여 호출하면, 내용이 있는 만큼만 배열로 가져와 범위가 100개라도 내용이 존재하는 배열 크기로 한정한다.

아래는 여러 시트를 참조하고자할 때, 정의한 switch 문이 사용된 함수이다.


  var ss_y=SpreadsheetApp.openById("시트iD");

  var ss_r=SpreadsheetApp.openById("시트iD");

  var ss_s=SpreadsheetApp.openById("시트iD");

  var ss_c=SpreadsheetApp.openById("시트iD");



function selected_getOuterRangeValues(k,s) {


  switch(s){

    case "reserve":

      var range_values = getOuterRangeValues(ss_y);

      break;

    case "remedy":

      var range_values = getOuterRangeValues(ss_r);

      break;

    case "sms":

      var range_values = getOuterRangeValues(ss_s);

      break;

    case "cosm":

      var range_values = getOuterRangeValues(ss_c);

      break;

  }

  if(k==null)

    k=1;

    var rl=range_values.length;

    var rv = new Array;

    var active_spreadsheet = SpreadsheetApp.getActiveSheet();

    var vl=active_spreadsheet.getRange(1, 1, 6, 4).getValues();

    var j=0;

  for(i=1;i<rl;i++){

    if(range_values[i][0]==vl[k][0] && range_values[i][1]==vl[k][1]){

      if(range_values[i][0])

        rv[j++]=range_values[i]; //존재하는 row 만큼 배열로 저장

    }

  }

      Logger.log(rv);

      return(rv);

}


// Return an array containing the values in the range name argument.

// Passing a non-existent range name raises the following error:

// TypeError: Cannot call method "getValues" of null. (line 0).

function getOuterRangeValues ( ss ) {

    // Create an spreadsheet object.

    // Create the range object.

    var range = ss.getSheets()[0].getRange("A1:F100");

  

    // Call the getValues() range object method to create an array 

    // containing the values for the cells in that named range.

    var range_values = range.getValues();

    Logger.log(ss.getName());

    // Return the array

    return range_values;

  

}

lispro06 (비회원)
    • 글자 크기

댓글 달기

suritam9
2013.04.25 조회 6129
suritam9
2013.04.04 조회 2208
suritam9
2012.09.14 조회 2524
suritam9
2012.06.24 조회 2371
suritam9
2012.06.24 조회 2788
suritam9
2012.06.24 조회 2559
suritam9
2012.06.22 조회 2588
박영식
2011.09.22 조회 2552
박영식
2011.09.21 조회 2495
박영식
2011.02.18 조회 2974
박영식
2010.09.29 조회 4766
첨부 (0)
위로