2016-08-26 2 views
0

스프레드 시트 내에 9 개의 탭이 있으며이 탭 중 6 개에만 정렬 스크립트를 적용하고 싶습니다. 나는 그 일이 어떻게 일어날지를 알 수 없다. 형식 오류 : 나는이 얻을 내가 'getActiveCell'를 제거하면 객체의 1 단계, 2 단계, 3 단계, 4 단계, 5 단계, 6 단계Google 스프레드 시트 스크립트 - 시트 내의 특정 탭에 적용

에 기능 getActiveCell를 찾을 수 없습니다

///////Auto Sort Phase Sheets 
    //get sheet 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var sheet = ['Phase 1','Phase 2', 'Phase 3', 'Phase 4', 'Phase 5', 'Phase 6']; 
    //get active cell 
    var editedCell = sheet.getActiveCell(); 
    //get range 
    var range = sheet.getRange("A2:Z"); 
    //sort by: status, priority, estimated time 
    range.sort([{column: 2, ascending: true}, {column: 1, ascending: true}, {column:3}]); 

나는이 오류 오류 : TypeError : 객체 Phase 1, Phase 2, Phase 3, Phase 4, Phase 5, Phase 6에서 함수 getRange를 찾을 수 없습니다.

'getRange'를 제거하면 스크립트는 아무 것도하지 않습니다.

답변

1

필자는 'sheet'배열에 지정된 시트 이름 목록을 반복해야한다고 생각합니다. 여기

///////Auto Sort Phase Sheets 
 
function sortSheets(){ 
 
    //get sheet 
 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
 
    //Specify sheets to be sorted 
 
    var sheet_name = ['Phase 1','Phase 2', 'Phase 3', 'Phase 4', 'Phase 5', 'Phase 6']; 
 
    for (s=0; s<sheet_name.length; s++) { 
 
    var sheet = ss.getSheetByName(sheet_name[s]); 
 
    //get range 
 
    var range = sheet.getRange("A2:Z"); 
 
    //sort by: status, priority, estimated time 
 
    range.sort([{column: 2, ascending: true}, {column: 1, ascending: true}, {column:3}]); 
 
    } 
 
}

: 그 이외에, 당신이 공유 코드를 실행하기 위해 함수에 포함되어야한다 (이 중복 때문에 그런데, 나는 editedCell 변수를 제거) 내가 테스트 한 시트는 다음과 같습니다. https://docs.google.com/spreadsheets/d/1VyhzrYwOWgGWszrRoUrQU2ECGqwN_lfb99fqw7x9P1k/edit#gid=0

0

GEOWill의 대답을 확장하려면 배열 작업을해야합니다. GAS (및 JavaScript)는 사용자가 말하지 않아도 배열을 자동으로 루프하지 않으며 2D는 포함됩니다. GAS (Javascript) 메소드는 배열 전체 또는 어레이의 단일 객체에서 작동하도록 설계되었습니다. 둘 다 아닙니다. 배열에서 객체 메쏘드 사용 시도로 인해 오류가 발생했습니다.

.getSheets() 메서드를 사용하려면 모든 시트 이름의 배열을 사용해야합니다. 이것은 반복 할 때 각 요소에 대해 메서드를 수행 할 수있는 객체의 배열입니다. 나는 작업을 필요로하는 각 시트를 반복하는 여러 가지 대체 방법을 포함시켰다. 실행 된 코드를 필요로하는 시트가있는 경우

첫 번째는 6 첫째 :

function phaseFunction() { 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var sheets = ss.getSheets(); 
    for (i = 0; i < 6; i++) { 
    var sheet = ss.getSheetByName(sheets[i]); 
    //further variable definition as usual 
    //we are now working with a singular sheet object (sheets[i]) 
    //code to perform 
    } 
} 

이 GEOWill의 전략과 유사하지만 첫 번째 6. 변경하려는 경우에 유용 할 수 있습니다을 항상 것이다 시트가 사용되고 항상 일 것임을 알고 있습니다. 구체적으로 처음 6입니다.

아래의 구성은 GEOWill이 이름을 기준으로 시트를 선택하는 방법을 작동합니다. 같은 시트를 사용하고 의 이름이이 아닌 경우 유용합니다.

function phaseFunction() { 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var sheets = ['Phase 1','Phase 2', 'Phase 3', 'Phase 4', 'Phase 5', 'Phase 6']; 
    for (i = 0; i < sheets.length; i++) { 
    var sheet = ss.getSheetByName(sheets[i]); 
    //further variable definition as usual 
    //code to perform 
    } 
} 

다음 구성은 순서에 관계없이 모든 시트를 반복하고 이름이 일치하면 코드를 수행합니다. 당신은 항상 (이보다 조건을 충족하지 않는 것이 매 이내) 9 장을 경우

function phaseFunction() { 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var sheets = ss.getSheets(); 
    for (i = 0; i < sheets.length; i++) { 
    var sheet = ss.getSheetByName(sheets[i]); 
    if (sheet == "Name 1" || sheet == "Name 2" || ...) { 
    //further variable definition as usual 
    //the variables defined will only be defined on the desired sheets 
    //code to perform 
    } else { 
     continue; 
     //skip over all those that don't meet the condition 
    } 
    } 
} 

당신은 건너 뛸 수있는 사람을 확인하기 위해 if() 문을 변경할 수 있습니다. 이렇게하면 조건문이 짧아지고 처리하려는 시트를 더 추가 할 수있게되어 항상 시트를 혼자두고 떠날 수있게됩니다.

이 마지막 두 가지 구성에는 표준 명명 규칙이 적용됩니다. 귀하의 질문에 귀하의 시트 이름을 어떻게 표준화하고 시트를 혼자 떠나면이 기준과 일치하지 않는다는 것을 암시, 당신은 내가 아래에 쓴 것을 사용할 수 있습니다. 이름을 확인하여 설정 한 규칙과 유사한 지 확인한 후 규칙을 준수하는지 확인합니다. 예를 들어, "단계"로 시작하는 경우 코드를 수행하고 그렇지 않은 경우 ("데이터", "로그", "수식"등) 코드를 수행하지 마십시오.

function phaseFunction() { 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var sheets = ss.getSheets(); 
    for (i = 0; i < sheets.length; i++) { 
    var sheet = ss.getSheetByName(sheets[i]); 
    var name = sheet.getName(); 
    if (name.includes("Phase")) { //this returns a boolean 
    //further variable definition as usual 
    //the variables defined will only be defined on the desired sheets 
    //code to perform 
    } else { 
     continue; 
     //skip over all those that don't meet the condition 
    } 
    } 
} 

다음은 정규 표현식을 사용

이 첫 번째 기본 문자열 방법 .includes()을 사용합니다.

function phaseFunction() { 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var sheets = ss.getSheets(); 
    for (i = 0; i < sheets.length; i++) { 
    var sheet = ss.getSheetByName(sheets[i]); 
    var sheetName = sheet.getName(); 
    var nameMatch = sheetName.match(/\b\Phase\b/g); 
    if (nameMatch[0] != null) { 
    //further variable definition as usual 
    //the variables defined will only be defined on the desired sheets 
    //code to perform 
    } else { 
     continue; 
     //skip over all those that don't meet the condition 
    } 
    } 
} 

이 특정 RegEx는 "단계"구를 찾습니다. 대소 문자를 구분합니다. 그런 다음 .match()이 리턴 한 배열을 점검하고 널이 아닌 경우 코드를 수행합니다. 이 매칭 및 체크 과정에 관해서는 수백 가지의 방법이 있습니다. RegEx에 대한 참고 자료 및 가이드 (매우 유용)는 herehere입니다. 테스터는 here입니다.