調査アンケート: Blockly のご利用体験についてお聞かせください
アンケートを開始
コードの生成
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
コード生成は、ワークスペースのブロックを実行可能なコード文字列に変換するプロセスです。
コード生成は非常に重要です。ブロックが実際に実行できるようにするためです。たとえば、ブロックは算術式の評価、迷路内のキャラクターの移動、オンライン ショップの構成などを実行できます。
Blockly はブロックを直接「実行」しません。代わりに、コード文字列を生成し、それを実行します。
コード生成ツール
コードを生成するには、コード ジェネレーター インスタンスを使用します。
次のコード スニペットは、ワークスペース内のブロックの JavaScript コードを生成する方法を示しています。
// javascriptGenerator is a code generator that makes JavaScript strings.
import {javascriptGenerator} from 'blockly/javascript';
const code = javascriptGenerator.workspaceToCode(myWorkspace);
Blockly が提供するさまざまなコード生成ツールとそれらへのアクセス方法について詳しくは、言語コード生成ツールをご覧ください。
ブロックコード生成ツール
各ブロックには、生成するコードを定義するブロックコード生成ツールが関連付けられています。生成する言語ごとにブロックコード ジェネレータを定義する必要があります。
このコード スニペットは、「前方に移動」ブロックの JavaScript ブロックコード ジェネレータを定義します。
javascriptGenerator.forBlock['my_custom_block'] = function(block, generator) {
const steps = block.getFieldValue('FIELD_NAME');
// moveForward is a function you would have to define yourself and provide
// within your execution context.
return `moveForward(${steps});\n`;
}
ブロックコード生成ツールを定義する方法については、ブロックコード生成ツールをご覧ください。
実行
コードを生成したら、そのコードを実行する方法を見つける必要があります。実行方法の決定はアプリケーション固有であり、Blockly の範囲外です。
コードを実行する方法の詳細については、コードを生成して実行するをご覧ください。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-07-25 UTC。
[null,null,["最終更新日 2025-07-25 UTC。"],[[["\u003cp\u003eCode generation transforms visual blocks into executable code strings, enabling actions like calculations and character movements within applications.\u003c/p\u003e\n"],["\u003cp\u003eBlockly utilizes code generators to produce code in various languages like JavaScript, requiring separate generators for each target language.\u003c/p\u003e\n"],["\u003cp\u003eEvery block possesses a block-code generator that dictates the specific code it produces for a given language, customizable to your application's needs.\u003c/p\u003e\n"],["\u003cp\u003eWhile Blockly handles code generation, executing the generated code is application-specific and necessitates your own implementation based on the environment.\u003c/p\u003e\n"]]],["Code generation converts blocks into executable code strings. Blockly utilizes code generator instances, like `javascriptGenerator`, to transform a workspace's blocks into code. Each block requires a block-code generator, defining its code output, demonstrated in the example with a \"move forward\" block. After code generation, the code must be executed, but this execution process is application-specific and not part of Blockly's core functionality.\n"],null,["# Generate code\n\nCode generation is the process of turning the blocks on a workspace into a\nstring of code that can be executed.\n\nCode generation is extremely important, because it is what allows your blocks to\nactually *do* things, like evaluate arithmetic expressions, move a character\nthrough a maze, or configure an online shop!\n\nBlockly doesn't \"run\" blocks directly. Instead you generate code strings, and\nthen execute those.\n\nCode generators\n---------------\n\nTo generate code, you use a code generator instance.\n\nThis code snippet shows how to generate JavaScript code for the blocks in a\nworkspace: \n\n // javascriptGenerator is a code generator that makes JavaScript strings.\n import {javascriptGenerator} from 'blockly/javascript';\n\n const code = javascriptGenerator.workspaceToCode(myWorkspace);\n\nFor more information about the different code generators that Blockly provides\nand how to access them, see [Language code generators](/blockly/guides/create-custom-blocks/code-generation/overview#language_code_generators).\n\nBlock-code generators\n---------------------\n\nEach block has an associated block-code generator that defines what code it\ngenerates. A block-code generator has to be defined for each individual language\nyou want to generate.\n\nThis code snippets defines a JavaScript block-code generator for a \"move\nforward\" block: \n\n javascriptGenerator.forBlock['my_custom_block'] = function(block, generator) {\n const steps = block.getFieldValue('FIELD_NAME');\n // moveForward is a function you would have to define yourself and provide\n // within your execution context.\n return `moveForward(${steps});\\n`;\n }\n\nFor more information about how to define your block-code generators, see\n[Block-code generators](/blockly/guides/create-custom-blocks/code-generation/overview#block-code_generators).\n\nExecution\n---------\n\nAfter you've generated the code, you need to figure out how to execute it.\nDeciding how to execute it is very application-specific, and outside the scope\nof Blockly.\n\nFor more information about ways to execute code, see\n[Generate and run code](/blockly/guides/create-custom-blocks/code-generation/overview#generate_and_run_code)."]]