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에 대한 참고 자료 및 가이드 (매우 유용)는 here 및 here입니다. 테스터는 here입니다.