PhpMyCellScript
PhpMyCellScript is a scripting language which allows you to write scripts in Excel/Calc, where each cell may contain an instruction or an instruction argument. As PHP code can be used directly, so it is called PhpMyCellScript.
Some special features of PhpMyCellScript are:
- Due to the use of spreadsheet, PhpMyCellScript has fewer syntactic constructs than other scripting languages.
- While control flow in other languages is supported by some control flow statements only, any instructions can alter the program flow in PhpMyCellScript. See IF-like instructions and WHILE-like instructions below.
- The PHP support of PhpMyCellScript allows you to use any PHP functions. See Basic instruction components below.
- The PhpMyCellScript interpreter is implemented as a code generator in PhpMyCodeGenerator, and you can customize the PhpMyCellScript interpreter via addons to add new instructions or override existing instructions. See How to add new instructions to PhpMyCellScript? below.
Basic instruction components
Each instruction consists of a name and its arguments. If a string is not a valid instruction name, then the string will be interpreted as PHP code (the trailing semicolon is not required).
Example:
Name | Argument |
---|---|
ECHO | Hello World! |
PHP code | |
echo "Hello World!" |
Argument types
Text string
The cell value is passed to the instruction unchanged as a string.
Example:
Name Text string ASSERT isset($argument1) PHP string where variables will be expanded
The cell value is a PHP heredoc string where variables will be expanded before passed to the instruction.
Example:
Name PHP string ECHO Hi, {$username}! PHP expression
The cell value is a PHP expression which will be evaluated before passed to the instruction.
Example:
Name PHP string PHP expression PHP variable SELECT_OPTION Please select a language: array('Chinese', 'English') $selected_index PHP template
The cell value is a PHP template whose output will be captured and passed to the instruction.
Example:
Name PHP template PHP variable PHP <?php echo PHP_OS; ?> $os PHP variable
The cell value is a PHP variable which can be used by the instruction to return value.
Example:
Name PHP string PHP variable PROMPT Please enter your name: $name Cell array
The cell values in the following rows will be read by the instruction.
Example:
Name PHP variable ARRAY_MERGE $column_info username varchar(100) password varchar(100) Column 1 Column 2
IF-like instructions
IF-like instructions can alter the program flow to process the instructions in the next column conditionally. Alternative branches are supported by ELSEIF and ELSE.
Example:
FILE_LOAD | $project_news_page | $project_news_html |
ECHO | Loaded "{$project_news_page}"!\n | |
ELSE | ||
ECHO | Failed to load "{$project_news_page}"!\n |
WHILE-like instructions
WHILE-like instructions support looping of the instructions in the next column.
Example:
COMMENT | Replace all bad words in a user comment with good words. | |
FIND | /bad/ | $user_comment |
REPLACE | good |
How to add new instructions to PhpMyCellScript?
- Prepare a PhpMyCellScript addon class, say in PhpMyCellScript/Addons/Test.php. An instruction is defined by a public method whose name starts with "cmd_". (See Instruction method for details.) Example:
<?php namespace PhpMyCellScript\Addons; use PhpMyCellScript\OutputVariable; class Test { function cmd_DICE(OutputVariable $output_variable) { $output_variable->setValue(mt_rand(1, 6)); } } ?>
- Prepare a PhpMyCodeGenerator script with a custom PhpMyCellScript interpreter, say test.php:
<?php include_once 'PhpMyCodeGenerator.php'; include_once 'PhpMyCellScript.php'; PhpMyCodeGenerator::main($argc, $argv); function gen_myscript($excel, $worksheet, $argv) { $interpreter = new PhpMyCellScript(); $interpreter->registerAddonByName('Example'); $interpreter->registerAddonByName('Test'); $interpreter->run($excel, $worksheet, $argv); } ?>
- Prepare a PhpMyCellScript script in an Excel file, say test.xls:
test.xls (myscript - test)
WHILE TRUE DICE $number ECHO $number\n IF $number == 6 BREAK - Run the script by:
php test.php test.xls
- A possible output:
2 5 4 2 3 6