Apache OpenOffice Basic 程序概述

From Apache OpenOffice Wiki
Jump to: navigation, search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
doc OOo
Book.png

Apache OpenOffice Basic 是一种解释程序语言。与 C++ 或 Delphi 不同,Apache OpenOffice Basic 编译器不创建可自动运行的可执行文件或自解压文件。相反,您可以在 Apache OpenOffice 中执行 Apache OpenOffice Basic 程序。将先检查代码是否有明显错误,然后逐行执行代码。

程序行

Basic 解释程序面向行的执行方式是 Basic 和其他编程语言的主要区别之一。在 Java、C++ 或 Delphi 程序的源代码中,硬换行符的位置无关紧要,而 Basic 程序中的每一行都形成一个自包含单元。函数调用、数学表达式以及其他语言元素(如函数和循环标头)必须在各自开始的那一行中完成。

如果没有足够的空间,或者这样会导致行过长,可通过添加下划线 _ 将几个行链接起来。以下示例说明了如何将数学表达式中的四行链接起来:

LongExpression = (Expression1 * Expression2) + _
(Expression3 * Expression4) + _ 
(Expression5 * Expression6) + _
(Expression7 * Expression8)
Documentation note.png 下划线必须始终是链接行的最后一个字符,后面不能带有空格或制表符,否则,代码将会产生错误。

在 Apache OpenOffice Basic 中,除了将各行链接起来以外,您还可以使用冒号将一行拆分为几段,以便有足够的空间容纳多个表达式。赋值

a = 1 
a = a + 1 
a = a + 1 

可以写成:

a = 1  :  a = a + 1  :  a = a + 1

标注

除了要执行的程序代码以外,Apache OpenOffice Basic 程序还可以包含注释,以解释程序的各个部分并提供以后可用的重要信息。

Apache OpenOffice Basic 提供了两种可在程序代码中插入注释的方法:

  • 撇号后面的所有字符都被视为注释:
    Dim A    ' This is a comment for variable A
    
  • 关键字 Rem 后跟注释:
    Rem This comment is introduced by the keyword Rem.
    

注释通常包含一直到行末的所有字符。Apache OpenOffice Basic 接着解释后面的行(像常规指令一样)。如果注释占据几行,则必须将每一行都标识为注释:

Dim B     ' This comment for variable B is relatively long
          ' and stretches over several lines. The
          ' comment character must therefore be repeated 
          ' in each line.

标记

一个 Apache OpenOffice Basic 程序可以包含几十个、数百个甚至数千个标记,这些标记是变量、常量和函数等的名称。在选择标记名称时,以下规则适用:

  • 标记只能包含拉丁字母、数字和下划线 (_)。
  • 标记的第一个字符必须是字母或下划线。
  • 标记不能包含特殊字符,例如 ä â î ß。
  • 标记的最大长度为 255 个字符。
  • 不区分大小写字符。例如,OneTestVariable 标记定义的变量与 onetestVariableONETESTVARIABLE 定义的变量相同。
    不过,此规则有一个例外情况:UNO-API 常量区分大小写字符。Apache OpenOffice API 简介提供了有关 UNO 的详细信息。
Documentation note.png Apache OpenOffice Basic 中构造标记的规则与 VBA 中不同。例如,在使用 Option Compatible 时,Apache OpenOffice Basic 才允许在标记中使用特殊字符,因为特殊字符可能会导致国际项目出现问题。

下面是几个正确标记和错误标记的示例:

Surname      ' Correct 
Surname5     ' Correct (number 5 is not the first digit)
First Name   ' Incorrect (spaces are not permitted)
DéjàVu       ' Incorrect (letters such as é, à are not permitted)
5Surnames    ' Incorrect (the first character must not be a number)
First,Name   ' Incorrect (commas and full stops are not permitted)
Content on this page is licensed under the Public Documentation License (PDL).


Personal tools