首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >是什么原因导致我的Groovy脚本中的for循环中出现了意外的标记"=“?

是什么原因导致我的Groovy脚本中的for循环中出现了意外的标记"=“?
EN

Stack Overflow用户
提问于 2019-06-07 01:47:17
回答 1查看 214关注 0票数 0

我一直在尝试从this site自定义代码,以便将excel工作簿中的表格转换为csv文件。我在上面的片段中做了一些小的修改,并且只给这个片段添加了一个标签,因为它抛出了一个不同的错误。现在,我在for循环中的第一个"=“处得到了一个意外的令牌错误:

代码语言:javascript
运行
复制
.groovy: 21: unexpected token: = @ line 21, column 16.
       for (int r = 0, rn = sheet.getLastRowNum() ; r <= rn ; r++) {
              ^

1 error

整个脚本包含在下面。我尝试重新输入代码以查找任何语法错误,但我担心这可能只是groovy解释这段代码的方式上的一种误解。

代码语言:javascript
运行
复制
#!/usr/bin/env groovy

@Grab(group='org.apache.poi', module='poi', version='4.1.0')

import org.apache.poi.xssf.usermodel.XSSFWorkbook
import org.apache.poi.ss.usermodel.DataFormatter

Workbook wb = new XSSFWorkbook(new File(this.args));
int sheetNo = Integer.parseInt(args[index++]);
FormulaEvaluator fe = null;
if ( index < args.length ) {
    fe = wb.getCreationHelper().createFormulaEvaluator();
}
DataFormatter formatter = new DataFormatter();
PrintStream out = new PrintStream(new FileOutputStream(csvFile), true, "UTF-8");

byte[] bom = [(byte)0xEF, (byte)0xBB, (byte)0xBF];
out.write(bom);
label:{
    Sheet sheet = wb.getSheetAt(sheetNo);
    for (int r = 0, rn = sheet.getLastRowNum() ; r <= rn ; r++) {
        Row row = sheet.getRow(r);
        if ( row == null ) { out.println(','); continue; }
        boolean firstCell = true;
        for (int c = 0, cn = row.getLastCellNum() ; c < cn ; c++) {
            Cell cell = row.getCell(c,    Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
            if ( ! firstCell ) out.print(',');
            if ( cell != null ) {
                if ( fe != null ) cell = fe.evaluateInCell(cell);
                String value = formatter.formatCellValue(cell);
                if ( cell.getCellTypeEnum() == CellType.FORMULA ) {
                    value = "=" + value;
                }
                out.print(encodeValue(value));
            }
            firstCell = false;
        }
        out.println();
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-07 02:22:23

只需将其分成两个语句:

代码语言:javascript
运行
复制
int cn = row.getLastCellNum()
for (int c = 0 ; c < cn ; c++) {
  ...
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56482688

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档