2012-11-12 1 views
0

Google은 매우 많은 사용자가보고하기 위해 Google 스프레드 시트를 사용합니다. 나는 현재 사용자에 따라 특정 시트를 여는 기본 스크립트를 작성했습니다 :Google 문서 도구의 다른 시트에서 사용자 데이터 가져 오기 및 사용

var CurrentUser = Session.getUser().getEmail() 
var ss = SpreadsheetApp.getActive(); 
    switch(CurrentUser){ 
    case "[email protected]": 
     SpreadsheetApp.setActiveSheet(ss.getSheetByName("sheet1")); 
     break; 
    case "[email protected]": 
     SpreadsheetApp.setActiveSheet(ss.getSheetByName("sheet2")); 
     break; 
    case "[email protected]": 
     SpreadsheetApp.setActiveSheet(ss.getSheetByName("sheet3")); 
     break; 
    etc... 

내가 외부 테이블에 유저 데이터 및 sheetnames를 넣고 해당 테이블에 따라이 데이터를 좀하고 싶습니다 그것을 너무 전자 메일 및 사용자 목록을 유지 관리하기가 쉽습니다. 특정 Google 스프레드 시트에서 데이터를 가져와 그에 따라 스크립트를 작동 시키려면 어떻게해야합니까?

답변

0

시도해 볼 수 있습니다. 다른 시트에 VLOOKUP을 시뮬레이트하고 현재 통합 문서의 '매칭'시트로 전환합니다. 이것은 일치하지 않는 항목을 처리하지는 않지만 귀하의 경우에 맞게 추가하는 것이 상대적으로 간단해야합니다.

function switchSheets() { 
    // Current sheet 
    var ss = SpreadsheetApp.getActive(); 
    // Target sheet (using the key found in the URL) 
    var target = SpreadsheetApp.openById("my_other_sheet_id"); 
    var rows = target.getDataRange(); 
    var values = rows.getValues(); 

    // Get the current user 
    var CurrentUser = Session.getUser().getEmail(); 

    // Now iterate through all of the rows in your target sheet, 
    // looking for a row that matches your current user (this assumes that 
    // it will find a match - you'll want to handle non-matches as well) 
    for (var i = 1; i <= rows.getNumRows() - 1; i++) { 
    // Match the leftmost column 
    var row = values[i][0]; 
    if (row == CurrentUser) { 
     // If we match, grab the corresponding sheet (one column to the right) 
     target_sheet = values[i][1]; 
     break; 
    } 
    } 
    // Now switch to the matched sheet (rememeber to handle non-matches) 
    ss.setActiveSheet(ss.getSheetByName(target_sheet)); 
}; 
관련 문제