대부분의 필드 값은 즉시 코드 문자열에 연결할 수 있습니다.
하지만 일부 필드 값은 사용할 수 있기 전에 추가 작업이 필요합니다.
문자열
문자열을 연결하려면 먼저 quote_ 또는 multiline_quote_로 따옴표로 묶어야 합니다. 이러한 함수는 JavaScript에서 '을 \'로 바꾸는 것과 같이 언어별 문자 이스케이프를 실행합니다.
// For a single line text field.conststr=generator.quote_(block.getFieldValue('STR'));// For a multiline text field.conststr=generator.multiline_quote_(block.getFieldValue('STR'));
변수
내장 변수 필드의 경우 getFieldValue는 변수 이름이 아닌 변수 ID를 반환합니다. 실제 변수 이름을 가져오려면 코드 생성기에서 getVariableName를 호출합니다. 이렇게 하면 고유하고 유효한 이름이 반환됩니다. getVariableName는 다음과 같은 작업을 실행합니다.
비ASCII 문자를 ASCII로 변환합니다. 이는 사용자가 자신의 언어로 변수 이름을 입력할 수 있기 때문에 필요합니다. 예를 들어 'متغير'를 '_D9_85_D8_AA_D8_BA_D9_8A_D8_B1'로 변환합니다.
변수 이름이 프로그래밍 언어에 명시된 규칙을 따르는지 확인합니다.
예를 들어 공백을 밑줄로 변환하고 숫자로 시작하는 변수 이름에 접두사 my_를 추가합니다.
예약된 단어 또는 다른 변수 또는 함수 이름과의 충돌을 해결합니다.
예를 들어 for을 for2로 변환합니다.
내장 드롭다운 필드의 경우 getFieldValue은 코드에서 직접 사용할 수 없는 언어 중립적인 문자열을 반환합니다. 예를 들어 비교 연산자가 포함된 드롭다운은 'EQUALS', 'LESS' 또는 'GREATER'을 반환할 수 있습니다. 이를 사용하여 코드에서 사용되는 문자열을 조회할 수 있습니다.
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["필요한 정보가 없음","missingTheInformationINeed","thumb-down"],["너무 복잡함/단계 수가 너무 많음","tooComplicatedTooManySteps","thumb-down"],["오래됨","outOfDate","thumb-down"],["번역 문제","translationIssue","thumb-down"],["샘플/코드 문제","samplesCodeIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-04-14(UTC)"],[[["Fields in Blockly define user-editable values (strings, numbers, colors, etc.) for code generation."],["Access field values using `getFieldValue`, transform them into usable strings (e.g., quoting strings, scrubbing variable names), and concatenate them into the code."],["Use `quote_` or `multiline_quote_` to properly format string values for code generation."],["Use `getVariableName` to ensure variable names are valid and avoid conflicts in the generated code."],["Refer to specific block type documentation for details on returning the generated code."]]],["Code generation with fields involves retrieving user-inputted values, such as strings or numbers, from fields. `getFieldValue` accesses these values, which vary by field type. Strings need quoting via `quote_` or `multiline_quote_`, while variables require scrubbing with `getVariableName` to ensure they are ASCII and don't clash with reserved words. Finally, the processed field value is inserted into the code string. The completed code string is returned depending on the specific type of block.\n"]]