PhpMyCodeGenerator and PhpMyCellScript

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:

NameArgument
ECHOHello World!
PHP code
echo "Hello World!"

Argument types

  1. Text string

    The cell value is passed to the instruction unchanged as a string.

    Example:

    NameText string
    ASSERTisset($argument1)
  2. 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:

    NamePHP string
    ECHOHi, {$username}!
  3. PHP expression

    The cell value is a PHP expression which will be evaluated before passed to the instruction.

    Example:

    NamePHP stringPHP expressionPHP variable
    SELECT_OPTIONPlease select a language: array('Chinese', 'English')$selected_index
  4. PHP template

    The cell value is a PHP template whose output will be captured and passed to the instruction.

    Example:

    NamePHP templatePHP variable
    PHP<?php echo PHP_OS; ?>$os
  5. PHP variable

    The cell value is a PHP variable which can be used by the instruction to return value.

    Example:

    NamePHP stringPHP variable
    PROMPTPlease enter your name: $name
  6. Cell array

    The cell values in the following rows will be read by the instruction.

    Example:

    NamePHP variable
    ARRAY_MERGE$column_info
    usernamevarchar(100)
    passwordvarchar(100)
    Column 1Column 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
ECHOLoaded "{$project_news_page}"!\n
ELSE
ECHOFailed to load "{$project_news_page}"!\n

WHILE-like instructions

WHILE-like instructions support looping of the instructions in the next column.

Example:

COMMENTReplace all bad words in a user comment with good words.
FIND/bad/$user_comment
REPLACEgood

How to add new instructions to PhpMyCellScript?

  1. 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));
     }
    }
    ?>
    
  2. 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);
    }
    ?>
    
  3. Prepare a PhpMyCellScript script in an Excel file, say test.xls:
    test.xls (myscript - test)
    WHILETRUE
    DICE$number
    ECHO$number\n
    IF$number == 6
    BREAK
  4. Run the script by:
    php test.php test.xls
  5. A possible output:
    2
    5
    4
    2
    3
    6